将 Node.js 升级到 Mac OS 上的最新版本

Currently I am using Node.js v0.6.16 on mac os x 10.7.4. Now I want to upgrade it to the latest Node.js v0.8.1. But after downloading and installing the latest package file from nodejs.org, I found that system is still using v0.6.16 instead of v0.8.1 when I typed "node -v" in a terminal. Is there any step that I have missed? Or should I thoroughly uninstall the old version before installing the latest one?

BTW, I know that nvm can help to manage the nodejs package

https://github.com/creationix/nvm/

Is there any way to upgrade the Node.js without using it?

I have googled this problem, but it seems to me that there is no very clear answer to this question for the latest Node.js. Many thanks.

转载于:https://stackoverflow.com/questions/11284634/upgrade-node-js-to-the-latest-version-on-mac-os

weixin_41568126
乱世@小熊 Folks - this is a 6 year old question and with some equally old answers. No doubt that they were the best practices back then. But as of 2018, brew upgrade node should work just fine. Please see the answer by wprl below.
2 年多之前 回复
csdnceshi75
衫裤跑路 You might want to first double check you have it installed with brew apple.stackexchange.com/questions/101090/…
接近 3 年之前 回复
csdnceshi77
狐狸.fox No, I don't think there is any Node process running. And after restarting the OS, the "node -v" command still shows v0.6.16 is running.
8 年多之前 回复
csdnceshi57
perhaps? Not sure on an answer, but for clarification is a Node process running when you update it?
8 年多之前 回复
csdnceshi77
狐狸.fox Thanks Bob. I have updated my question.
8 年多之前 回复

20个回答

Here's how I successfully upgraded from v0.8.18 to v0.10.20 without any other requirements like brew etc, (type these commands in terminal):

  1. sudo npm cache clean -f (force) clear you npm cache
  2. sudo npm install -g n install "n" (this might take a while)
  3. sudo n stable upgrade to lastest version

Note that sudo might prompt your password.

If the version number doesn't show up when typing node -v, you might have to reboot.

these instructions are found here as well: davidwalsh.name/upgrade-nodejs

weixin_41568131
10.24 This worked, but no one explains what the commands why installing "n" helps. I just figured out today that "n" is a Node.js version manager, as somewhat explained on the npm js website - npmjs.com/get-npm
接近 2 年之前 回复
csdnceshi50
三生石@ I tried a couple of from this answer thread But Only This Solutions works for me. @Johan Dettmar, Thanks for the answer. I upgraded from 8.x to 10.10.0
大约 2 年之前 回复
csdnceshi56
lrony* I didn't need to clear my cache, but maybe I made a mistake in not doing so
大约 2 年之前 回复
csdnceshi76
斗士狗 This hasn't worked for me. Am I doing something wrong?
2 年多之前 回复
csdnceshi69
YaoRaoLov you made my day
2 年多之前 回复
weixin_41568134
MAO-EYE Thanks Mate....
2 年多之前 回复
weixin_41568196
撒拉嘿哟木头 Worked great..thanks
2 年多之前 回复
csdnceshi77
狐狸.fox Thanks, It helped as brew with sudo is no longer supported.
2 年多之前 回复
weixin_41568110
七度&光 Perfect @osx node upgraded to 9.8.0
2 年多之前 回复
csdnceshi60
℡Wang Yan sudo n lts as the last step gets you a Long Term Support version
接近 3 年之前 回复
csdnceshi80
胖鸭 the easiest way to do it ! thanks
接近 3 年之前 回复
csdnceshi53
Lotus@ This should be the marked answer. All three lines are working.
3 年多之前 回复
csdnceshi65
larry*wei you can do sudo n latest as the final step also
3 年多之前 回复
csdnceshi75
衫裤跑路 Brew is occurring error. This is the correct answer, currently.
3 年多之前 回复
csdnceshi73
喵-见缝插针 Note step 1 force clears your cache. You'll get the warning: npm WARN using --force I sure hope you know what you are doing.
4 年多之前 回复
csdnceshi57
perhaps? FWIW you can use 'n lts' to install the long term support version rather than the current stable version (i.e. 4.4.3 instead of 5.10.1 at the time of writing)
4 年多之前 回复
csdnceshi64
游.程 Please mark this as answer! Brew didn't work.
大约 5 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 worked to update node from v0.10 to v0.12 in macos x snow leopard. Thank you.
大约 5 年之前 回复

If you initially installed Node.js with Homebrew, run:

brew update
brew upgrade node
npm update -g npm

Or as a one-liner:

brew update && brew upgrade node && npm update -g npm

Note: If you have npm --version < 2.7.1, you should replace update (above) with install.


A convenient way to change versions is to use nvm:

brew install nvm

To install the latest version of Node.js with nvm:

nvm install node

If you installed via a package, then download the latest version from nodejs.org. See Installing Node.js and updating npm.

weixin_41568196
撒拉嘿哟木头 thank you for your valuable information
2 年多之前 回复
csdnceshi61
derek5. You can also simply run: npm i -g npm.
接近 3 年之前 回复
weixin_41568134
MAO-EYE Run npm install -g node after following above instructions if your node version is still not updated.
大约 3 年之前 回复
csdnceshi67
bug^君 I used homebrew to remove node, then followed the instructions given. The only thing I needed to do was npm install -g n for and then use n stable to use the most current node version
3 年多之前 回复
csdnceshi62
csdnceshi62 I hate update packages with brew by symlinks problems, for this time i used the node dpkginstaller.
4 年多之前 回复
weixin_41568131
10.24 they've since fixed the npm update -g npm command. github.com/Homebrew/homebrew/commit/…
大约 5 年之前 回复
csdnceshi75
衫裤跑路 in current brew version it states "If you update npm itself, do NOT use the npm update command. The upstream-recommended way to update npm is: npm install -g npm@latest"
5 年多之前 回复
csdnceshi78
程序go Yes It says that my system is ready to brew.
6 年多之前 回复
weixin_41568183
零零乙 have you run brew doctor and brew update recently?
6 年多之前 回复
csdnceshi78
程序go I am using Homebrew and everything works except being able to update npm. I get npm WARN package.json github-url-from-git@1.1.1 No repository field. /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js npm@1.5.0-alpha-1 /usr/local/lib/node_modules/npm
6 年多之前 回复
csdnceshi53
Lotus@ yes, you're right! thanks, i'll remove port later.
7 年多之前 回复
weixin_41568183
零零乙 It's recommended not to install more than one of Homebrew, MacPorts, or Fink.
7 年多之前 回复
csdnceshi53
Lotus@ I just upvoted this answer, but after 7 mins wait, brew told me installation failed... I also tried port upgrade node, but brew already messed up the package and port can't find node now... Finally I went to nodejs.org to download the installation package, and boom! Everything goes well, including the npm!
7 年多之前 回复
csdnceshi69
YaoRaoLov Yeah, it also seems weird to me. Maybe I am quite a newbie to NodeJS. Now I am using nvm to manage my NodeJS version. Nvm downloaded the sourcecode of v0.8.1 and make a couple of links: ln -fs out/Release/node node out/Release/node tools/installer.js install cp -rf src/node.h /Users/user/projects/nvm/v0.8.1/include/node/ ln -sf ../lib/node_modules/npm/bin/npm-cli.js /Users/user/projects/nvm/v0.8.1/bin/npm shebang #!/Users/user/projects/nvm/v0.8.1/bin/node Now using node v0.8.1 There must be some way to configure it in Mac OS
8 年多之前 回复
weixin_41568183
零零乙 Interestingly, the brew formula for node seems to just do a ./configure && make install; I'm not sure what exactly brew does in the upgrade process, but it should be reproducible by hand if necessary. A place to start, anyway :) github.com/mxcl/homebrew/blob/…
8 年多之前 回复
csdnceshi69
YaoRaoLov Thoroughly removing and reinstalling NodeJS works. But I really suspect if it is the proper way to upgrade NodeJS on Mac OS. So as a summary, on Mac OS, we'd better use some tools, such as nvm and brew, to manage the version of NodeJS.
8 年多之前 回复
csdnceshi69
YaoRaoLov Yeah. It seems to be the only way currently to do it. I will try and see if it works.
8 年多之前 回复
weixin_41568183
零零乙 You might try uninstalling the old node before reinstalling the new one: stackoverflow.com/questions/9044788/…
8 年多之前 回复
csdnceshi69
YaoRaoLov Hi SoloBold, thanks for your reply. I didn't use brew. I install the orignal v0.6.16 from the pkg file provided by nodejs.org. So I can't use "brew update node".
8 年多之前 回复

Because this seems to be at the top of Google when searching for how to upgrade nodejs on mac I will offer my tip for anyone coming along in the future despite its age.

Upgrading via NPM
You can use the method described by @Mathias above or choose the following simpler method via the terminal.

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

After which you may opt to confirm the upgrade

node -v

Your nodejs should have upgraded to the latest version. If you wish to upgrade to a specific one say v0.8.19 then instead of

sudo n stable

use

sudo n 0.8.19

EDIT Avoid using sudo unless you need to. Refer to comment by Steve in the comments

weixin_41568208
北城已荒凉 Can I do sudo n latest
接近 3 年之前 回复
weixin_41568184
叼花硬汉 nice answer. Worked fine.
大约 3 年之前 回复
csdnceshi55
~Onlooker Noted. Will make an edit.
4 年多之前 回复
weixin_41568174
from.. Sometimes using sudo when it's not needed actually makes a mess, because you end up with files owned by root, which then can't be modified without sudo.
4 年多之前 回复
csdnceshi55
~Onlooker Thanks @SteveBennett for adding that, I added sudo in order to include newbies who would need it, rather than leave them stranded. Moreover, having sudo will work in both cases.
4 年多之前 回复
weixin_41568174
from.. This works, but: is it really necessary to use the --force flag on cache clean? And I didn't need sudo for the first two commands.
4 年多之前 回复
csdnceshi63
elliott.david Because you advice to use sudo.
接近 5 年之前 回复
csdnceshi55
~Onlooker Why would you downvote an answer and not add a comment explaining why?
大约 5 年之前 回复

Nvm Nvm is a script-based node version manager. You can install it easily with a curl and bash one-liner as described in the documentation. It's also available on Homebrew.

Assuming you have successfully installed nvm. The following will install the latest version of node.

 nvm install node --reinstall-packages-from=node

The last option installs all global npm packages over to your new version. This way packages like mocha and node-inspector keep working.

N N is an npm-based node version manager. You can install it by installing first some version of node and then running npm install -g n.

Assuming you have successfully installed n. The following will install the latest version of node.

sudo n latest

Homebrew Homebrew is one of the two popular package managers for Mac. Assuming you have previously installed node with brew install node. You can get up-to-date with formulae and upgrade to the latest Node.js version with the following.

1 brew update
2 brew upgrade node

MacPorts MacPorts is the another package manager for Mac. The following will update the local ports tree to get access to updated versions. Then it will install the latest version of Node.js. This works even if you have previous version of the package installed.

1 sudo port selfupdate
2 sudo port install nodejs-devel
csdnceshi67
bug^君 This is what worked for me on OSX. I probably had NVM installed because node -v kept giving me 6.11.1 even after commands related to n latest and so on. The brew upgrade one worked. Thanks.
2 年多之前 回复
csdnceshi58
Didn"t forge brew upgrade node is a breeze
2 年多之前 回复

I use Node version manager (called n) for it.

npm install -g n

then

n latest

OR

n stable
csdnceshi74
7*4 LTS version n lts
接近 4 年之前 回复

I am able to upgrade the node using following command

nvm install node --reinstall-packages-from=node
csdnceshi66
必承其重 | 欲带皇冠 This one worked for me. Thanks!
2 年多之前 回复

Go to the website nodejs.org and download the latest pkg then install. it works for me

I used brew to upgrade my node. It has installed but it located in /usr/local/Cellar/node/5.5.0 and there is a default node in /usr/local/bin/node which bothers me. I don't want to make soft link because I don't really know how brew is organized. So I download the pkg file, installed and I got this info:

Node.js was installed at

/usr/local/bin/node

npm was installed at

/usr/local/bin/npm

Make sure that /usr/local/bin is in your $PATH.

Now the upgrade is completed

You can just go to nodejs.org and download the newest package. It will update appropriately for you. NPM will be updated as well.

csdnceshi51
旧行李 By far the easiest, if you initially installed it via de the website.
4 年多之前 回复

Go to http://nodejs.org and download and run the installer. It works now - for me at least.

csdnceshi76
斗士狗 Likely the only thing that works in all cases. Just do it this way. But I somehow had another node installed in /opt/local/bin I had to remove cause it was taking precedence in my PATH.
2 年多之前 回复
csdnceshi75
衫裤跑路 probably the easiest way here.
接近 4 年之前 回复
csdnceshi71
Memor.の makes sense. thanks for this.
大约 5 年之前 回复
csdnceshi54
hurriedly% I don't think it will call home. You will have to check regularly and download the new version from their webpage i think. But im not 100% on this.
接近 8 年之前 回复
csdnceshi61
derek5. Sorry for the confusion (wrote in a hurry). I have installed node.js straight from the site's download link, Current Version: v0.8.14. When say v0.8.15 gets released, will I have to download it again, will it auto-update, will it call-home and inform me? With homebrew for example I was doing a brew upgrade (so it was up to me)
接近 8 年之前 回复
csdnceshi54
hurriedly% Im not sure I understand what you are asking
接近 8 年之前 回复
csdnceshi61
derek5. Do you know by any chance how installed jode.js updates in OS/X?
接近 8 年之前 回复

You could install nvm and have multiple versions of Node.js installed.

curl https://raw.github.com/creationix/nvm/master/install.sh | sh
source ~/.nvm/nvm.sh

and then run:

nvm install 0.8.22  #(or whatever version of Node.js you want)

you can see what versions you have installed with :

nvm list

and you can change between versions with:

nvm use 0.8.22

The great thing about using NVM is that you can test different versions alongside one another. If different apps require different versions of Node.js, you can run them both.

csdnceshi70
笑故挽风 I am using this method now. It seems to me that this is the best way to keep multiple versions running at the same machine.
7 年多之前 回复
共20条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐