将 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

csdnceshi76
斗士狗 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 年多之前 回复
csdnceshi61
derek5. You might want to first double check you have it installed with brew apple.stackexchange.com/questions/101090/…
接近 3 年之前 回复
csdnceshi67
bug^君 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 年之前 回复
csdnceshi59
ℙℕℤℝ Not sure on an answer, but for clarification is a Node process running when you update it?
大约 8 年之前 回复
csdnceshi67
bug^君 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_41568184
叼花硬汉 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 年之前 回复
csdnceshi75
衫裤跑路 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 年之前 回复
csdnceshi50
三生石@ I didn't need to clear my cache, but maybe I made a mistake in not doing so
大约 2 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 This hasn't worked for me. Am I doing something wrong?
大约 2 年之前 回复
csdnceshi58
Didn"t forge you made my day
大约 2 年之前 回复
csdnceshi77
狐狸.fox Thanks Mate....
大约 2 年之前 回复
csdnceshi79
python小菜 Worked great..thanks
2 年多之前 回复
csdnceshi59
ℙℕℤℝ Thanks, It helped as brew with sudo is no longer supported.
2 年多之前 回复
csdnceshi53
Lotus@ Perfect @osx node upgraded to 9.8.0
2 年多之前 回复
csdnceshi55
~Onlooker sudo n lts as the last step gets you a Long Term Support version
2 年多之前 回复
csdnceshi54
hurriedly% the easiest way to do it ! thanks
2 年多之前 回复
csdnceshi51
旧行李 This should be the marked answer. All three lines are working.
大约 3 年之前 回复
weixin_41568183
零零乙 you can do sudo n latest as the final step also
3 年多之前 回复
weixin_41568127
?yb? Brew is occurring error. This is the correct answer, currently.
3 年多之前 回复
weixin_41568110
七度&光 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 年多之前 回复
csdnceshi61
derek5. 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 年多之前 回复
weixin_41568196
撒拉嘿哟木头 Please mark this as answer! Brew didn't work.
大约 5 年之前 回复
csdnceshi74
7*4 worked to update node from v0.10 to v0.12 in macos x snow leopard. Thank you.
大约 5 年之前 回复

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

csdnceshi55
~Onlooker Can I do sudo n latest
接近 3 年之前 回复
weixin_41568110
七度&光 nice answer. Worked fine.
大约 3 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 Noted. Will make an edit.
4 年多之前 回复
weixin_41568126
乱世@小熊 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 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 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_41568126
乱世@小熊 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 年多之前 回复
csdnceshi64
游.程 Because you advice to use sudo.
接近 5 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 Why would you downvote an answer and not add a comment explaining why?
接近 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.

csdnceshi57
perhaps? thank you for your valuable information
2 年多之前 回复
csdnceshi67
bug^君 You can also simply run: npm i -g npm.
接近 3 年之前 回复
csdnceshi55
~Onlooker Run npm install -g node after following above instructions if your node version is still not updated.
接近 3 年之前 回复
weixin_41568174
from.. 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 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 I hate update packages with brew by symlinks problems, for this time i used the node dpkginstaller.
大约 4 年之前 回复
csdnceshi61
derek5. they've since fixed the npm update -g npm command. github.com/Homebrew/homebrew/commit/…
接近 5 年之前 回复
csdnceshi79
python小菜 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 年多之前 回复
csdnceshi52
妄徒之命 Yes It says that my system is ready to brew.
大约 6 年之前 回复
csdnceshi62
csdnceshi62 have you run brew doctor and brew update recently?
大约 6 年之前 回复
csdnceshi52
妄徒之命 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 年之前 回复
csdnceshi65
larry*wei yes, you're right! thanks, i'll remove port later.
7 年多之前 回复
csdnceshi62
csdnceshi62 It's recommended not to install more than one of Homebrew, MacPorts, or Fink.
7 年多之前 回复
csdnceshi65
larry*wei 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 年多之前 回复
csdnceshi63
elliott.david 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 年之前 回复
csdnceshi62
csdnceshi62 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 年之前 回复
csdnceshi63
elliott.david 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 年之前 回复
csdnceshi63
elliott.david Yeah. It seems to be the only way currently to do it. I will try and see if it works.
大约 8 年之前 回复
csdnceshi62
csdnceshi62 You might try uninstalling the old node before reinstalling the new one: stackoverflow.com/questions/9044788/…
大约 8 年之前 回复
csdnceshi63
elliott.david 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 年之前 回复

I am able to upgrade the node using following command

nvm install node --reinstall-packages-from=node
weixin_41568110
七度&光 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.

csdnceshi54
hurriedly% By far the easiest, if you initially installed it via de the website.
4 年多之前 回复

I think the simplest way to use the newest version of Node.js is to get the newest Node.js pkg file in the website https://nodejs.org/en/download/current/ if you want to use different version of Node.js you can use nvm or n to manage it.

On macOS the homebrew recommended way is to run

brew install node
npm install -g npm@latest

Screenshot of Terminal Commands

csdnceshi68
local-host It will install the truly latest version, not just the most current brewed version.
5 年多之前 回复
csdnceshi79
python小菜 why is this the correct way?
5 年多之前 回复

Use nvm to upgrade node as per the project requirement..

install nvm through homebrew.. brew update brew install nvm mkdir ~/.nvm nano ~/.bash_profile

In your .bash_profile file (you may be using an other file, according to your shell), add the following :

export NVM_DIR=~/.nvm source $(brew --prefix nvm)/nvm.sh

source ~/.bash_profile echo $NVM_DIR

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
csdnceshi73
喵-见缝插针 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 年之前 回复
weixin_41568208
北城已荒凉 brew upgrade node is a breeze
大约 2 年之前 回复
共20条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐