如何更新 NodeJS 和 NPM 到下一个版本?

I just installed Node.js and npm (for additional modules).

How can I update Node.js and the modules which I'm using to the latest versions?

Can npm do it, or do I have to remove and reinstall Node.js and npm to get the next versions?

I followed this steps in the npm section.

转载于:https://stackoverflow.com/questions/6237295/how-can-i-update-nodejs-and-npm-to-the-next-versions

csdnceshi54
hurriedly% Things have changed for the better since this was first asked (if you don't need version managemenent). See Update Node (Windows, Mac & Linux).
2 年多之前 回复
weixin_41568126
乱世@小熊 there is the tool nvm wich gives you the option to have more than one running versions of node+npm
2 年多之前 回复
weixin_41568184
叼花硬汉 Look at the dates, other question is duplicated of this one...
3 年多之前 回复
csdnceshi61
derek5. Possible duplicate of How to update npm
接近 4 年之前 回复
csdnceshi71
Memor.の I was point out that even a more correct way of updating npm is to use nodesource.com See stackoverflow.com/a/24760147/544463
5 年多之前 回复

30个回答

As you may know, NPM is currently bundled with Node.js, it means that if you have installed node you already have installed npm. There are several approaches to keep up to date the Node.js and NPM, you need to use one of the following version managers:

Homebrew

If yo are on Mac, you can use Homebrew. To install NodeJS and NPM using brew:

$ brew install node

later you will be able to update it using

$ brew update && brew upgrade node

NPM will be updated as well.

You also will be able to switch to the one of the previous versions if you need, for example:

$ brew switch node 0.10.26

To install brew to your Mac:

$ ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

N

n is most likely to rvm (Ruby Version Manager), and can be used to manage and update node/npm versions.

Install Node.js versions easily:

$ n 0.10.26
$ n 0.8.17
$ n 0.9.6

Use (and install if missing) the latest official release:

$ n latest

Use/install the stable official release:

$ n stable

Switch to the previous version you were using:

$ n prev

If you want to see the list of installed nodes, just run n from your command line, the output will be something like the following:

$ n

  0.10.26
• 0.8.17
  0.9.6

The dot (•) means that it's a currently active version. To select a node version from the list use up and down arrows and activate using enter.

The n package is written on pure linux shell and available as a npm module (contains package.json), so if you have any Node.js installed, you can install/update the n through the npm:

$ npm install -g n

NVM

nvm is also like RVM, even the command names and usage are very similar.

To download, compile, and install the latest v0.10.x release of the Node.js using nvm:

$ nvm install 0.10

And then you can switch to the installed version:

$ nvm use 0.10

You can create an .nvmrc file containing version number in the project root folder; then run the following command to switch to the specified version:

$ nvm use

Or you can just run it:

$ nvm run 0.10

If you want to see which versions are already installed, use:

$ nvm ls

To install nvm itself you can use the install script (requires git) using cURL:

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

or wget:

$ wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh

P.S.

All these approaches I've used on MacOSX and Linux, I don't have any experience on how to manage Node.js versions on Windows, I can only suppose that the n (the second one) will work for Microsoft's OS (at least from the cygwin).

weixin_41568127
?yb? with command "brew update && brew upgrade node" , npm doesn't get updated to latest version as mentioned "...NPM will be updated as well.."
3 年多之前 回复
csdnceshi56
lrony* I think there is no big difference between this three approaches, just the n and nvm knows Node.js better as they focuses on it. In practice I'm using n locally and nvm in our Continuous Integration Server.
大约 4 年之前 回复
csdnceshi58
Didn"t forge I like this answer. +1 ! But it would have been even more useful to accompany it with some sort of explanation about the different package managers. Ie; why I should use one over the other. I would really help to give context..
4 年多之前 回复

See the docs for the update command:

npm update [<name> [<name> ...]]

This command will update all the packages listed to the latest version (specified by the tag config). It will also install missing packages.

Additionally, see the FAQ:

How do I update npm?

npm install -g npm

Please note that this command will remove your current version of npm. Make sure to use sudo npm install -g npm if on a Mac.

You can also update all outdated local packages by doing npm update without any arguments, or global packages by doing npm update -g.

Occasionally, the version of npm will progress such that the current version cannot be properly installed with the version that you have installed already. (Consider, if there is ever a bug in the update command.) In those cases, you can do this:

curl https://www.npmjs.com/install.sh | sh

To update Node.js itself, I recommend you use nvm, the Node Version Manager.

csdnceshi64
游.程 I tried, everything was fine, but after npm--version It shows me 5.6
大约 2 年之前 回复
weixin_41568127
?yb? The update doc is now at docs.npmjs.com/getting-started/installing-node
2 年多之前 回复
csdnceshi73
喵-见缝插针 DON'T use sudo npm install -g npm! This will uninstall it!
3 年多之前 回复
csdnceshi63
elliott.david Please do not sudo when using npm install -g
接近 4 年之前 回复
csdnceshi56
lrony* I would maybe reword the bit about mac to say that they "may" need to use sudo. You can install npm in a way that it doesn't require sudo, and I would argue that's the correct way to install it.
4 年多之前 回复
csdnceshi61
derek5. Using npm to install a new version of itself. I smell things blowing up.
4 年多之前 回复
csdnceshi72
谁还没个明天 Indeed, npm update -g npm just blew up npm on Windows for me. Have to reinstall node now. Ugh.
4 年多之前 回复
csdnceshi74
7*4 And btw: I needed to restart my bash for the update to work. Got confused when the update reported "It worked!" but npm --version still yielded my old version. Restarting my console and npm now reports the correct, up-to-date version :)
大约 5 年之前 回复
csdnceshi54
hurriedly% "Occasionally, the version of npm will progress such that the current version cannot be properly installed with the version that you have installed already." How can you tell when this is the case?
5 年多之前 回复
csdnceshi68
local-host npm install -g npm on mac removes current version of npm. correct command is sudo npm install -g npm
5 年多之前 回复
weixin_41568131
10.24 After running this I needed to run sudo chown -R [username] ~/.npm
5 年多之前 回复
weixin_41568183
零零乙 I tried all this suggestions (I get "It worked" and 2.1.16 during update) but my npv -v still gives me a sad 1.4.28 :(
5 年多之前 回复
csdnceshi50
三生石@ For updating from 1.x to 2.x the npm team recommends instead using npm install npm -g.
5 年多之前 回复
weixin_41568126
乱世@小熊 well I decided to get rid of node and npm installation via npm and I downloaded the binary installer from webpage. (I'm using MacOsX)
接近 6 年之前 回复
csdnceshi62
csdnceshi62 same here. This is ridiculous... how did you fix this?
接近 6 年之前 回复
weixin_41568126
乱世@小熊 I did it like you said npm update npm -g and now I don't have npm as a command.
大约 6 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 also have curl follow redirects with -L curl -L https://npmjs.org/install.sh| sudo sh
6 年多之前 回复
csdnceshi78
程序go This is the path that is working for me (mind the www and the sudo): curl https://www.npmjs.org/install.sh| sudo sh
6 年多之前 回复
csdnceshi53
Lotus@ I kept getting a 301 when I tried the curl command, so I just downloaded the script and ran sh install.sh.
6 年多之前 回复
weixin_41568110
七度&光 This answer's links are both dead, and it doesn't mention how to upgrade node.
6 年多之前 回复
weixin_41568110
七度&光 npm update npm -g didn't work for me on windows - it completed without output but npm remained the same version (1.3.11 when the most recent version is 1.3.14)
6 年多之前 回复
csdnceshi52
妄徒之命 You might need to sudo that install script, like so: curl https://npmjs.org/install.sh| sudo sh
接近 7 年之前 回复
csdnceshi71
Memor.の - it just means you can put any number of package names after the command, eg. npm update package1 package2 package3
7 年多之前 回复
csdnceshi67
bug^君 I dont understand what the [<name> [<name> ...]] suppose to mean ? is there an example make use all of those parameter ?
7 年多之前 回复
csdnceshi60
℡Wang Yan Thanks! I had to use the curl method to update from 1.0.6 to 1.0.23.
接近 9 年之前 回复

Updating npm is easy:

npm install npm@latest -g
csdnceshi69
YaoRaoLov its soo simple thank you. it worked for me straight away :) Many of the others did not work for me as well.
接近 2 年之前 回复
csdnceshi71
Memor.の This is also the official answer: docs.npmjs.com/getting-started/installing-node
3 年多之前 回复
csdnceshi53
Lotus@ This worked for me on Windows 10, thanks.
接近 4 年之前 回复
csdnceshi51
旧行李 Thanks this worked for me, its pretty simple. But i had to sudo npm install npm@latest -g thats all. Upvoted
接近 4 年之前 回复
csdnceshi66
必承其重 | 欲带皇冠 In Laravel homestead this didn't work. I still have the same version, even after provission.
接近 4 年之前 回复
csdnceshi64
游.程 Using windows 10 and node v5.7.0 the command npm install npm@latest -g failed to upgrade from npm 2.7.4 to npm 3.9.3.
大约 4 年之前 回复
csdnceshi55
~Onlooker This causes fatal error, at least for mac users see here
4 年多之前 回复
csdnceshi80
胖鸭 It is indeed this simple. Many of the others did not work for me, this one hit the spot exactly, thanks!
大约 5 年之前 回复

I found this really neat way of updating node on David Walsh's blog, you can do it by installing n:

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

It will install the current stable version of node. However, I recommend using nvm.

csdnceshi76
斗士狗 That blog post now starts with the following message "This method of upgrading node is now unstable and should not be used. The best way to manage Node.js versions is to use NVM: Node Version Management.!"
2 年多之前 回复
weixin_41568196
撒拉嘿哟木头 If you don't want or you can't use sudo, you can set the N_PREFIX envvar, before using n.
接近 3 年之前 回复
weixin_41568126
乱世@小熊 Don't run npm cache clean -f in Windows!
大约 3 年之前 回复
csdnceshi74
7*4 I was looking at this and saying there is No way that this may work and it did work for
3 年多之前 回复
csdnceshi64
游.程 In windows 10 you need to download the installer from nodejs.org/en as this command don't work. I am not talking about the sudo that makes no sense in windows I meant the commands.
4 年多之前 回复
csdnceshi65
larry*wei In Windows, you can simply download the newest version of node and install it. It'll upgrade both node and npm.
4 年多之前 回复
csdnceshi60
℡Wang Yan Not supported for Windows. I realize it would be easier to just use Linux, but at work, I am not free to choose my OS. As @rainabba says, the spirit of the Node.js project is to be cross-platform, and this isn't.
4 年多之前 回复
csdnceshi79
python小菜 Refuses to run for windows x64 (but does with win32 apparently). That's not in keeping with the Node.js project.
5 年多之前 回复
weixin_41568110
七度&光 - why did you need a trick? I'm puzzled.
6 年多之前 回复
csdnceshi80
胖鸭 'sudo n stable' was doing the trick for me
接近 7 年之前 回复

I understand this question is for Linux machine but just in case anybody is looking for a Windows solution, just go to the Node.js site, click the download button on the homepage and execute the installer program.

Thankfully it took care of everything and with a few clicks of 'Next' button I got the latest 0.8.15 Node.js version running on my Windows 7 machine.

csdnceshi79
python小菜 For me, for whatever reason, I needed to first uninstall Node (which didn't work), so I manually removed programfiles\node and c:\users\<myuser>\AppData\Roaming\npm and npm-cache, then did a fresh install of node and then ran npm install npm@latest -g and at that point it finally worked
2 年多之前 回复
csdnceshi54
hurriedly% npm install npm@latest -g worked for me just fine
大约 3 年之前 回复
csdnceshi75
衫裤跑路 This works with Windows 10, and both Node and npm update. Very slick.
大约 4 年之前 回复
csdnceshi57
perhaps? Don't forget to reboot afterwards!
4 年多之前 回复
csdnceshi52
妄徒之命 man... how did I skipped THIS BEAUTIFUL ANSWER LOL! Its amazing.
大约 5 年之前 回复
csdnceshi51
旧行李 if you're updating from npm v1.x to 2.x on Windows, you may have delete ‘npm’, ‘npm.cmd’ files from “C:\Program Files\nodejs” after installing or udpating npm. See: escapologist.wordpress.com/2014/09/29/…
5 年多之前 回复
weixin_41568184
叼花硬汉 I did this, but didn't updated npm, only node itself. Still on the search...
5 年多之前 回复
csdnceshi65
larry*wei Here's a link for the lazy: nodejs.org/download
大约 6 年之前 回复
csdnceshi77
狐狸.fox This is the best and easiest way to upgrade node on windows. worked for me just fine. Thanks anmol
6 年多之前 回复
weixin_41568174
from.. yeah. this worked for me. Im on windows. the n method didnt work. wish there was a better way of doing it.
大约 7 年之前 回复
$ npm install -g npm stable

Worked for me to update from 1.4.28 to 2.1.5

Just listened to an interview with the npm team on the latest episode of nodeup, and they recommended not using update for the update from 1.x to 2.x. Instead, use: npm install npm -g

Use n module from npm in order to upgrade node . n is a node helper package that installs or updates a given node.js version.

sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo ln -sf /usr/local/n/versions/node/<VERSION>/bin/node /usr/bin/nodejs

NOTE that the default installation for nodejs is in the /usr/bin/nodejs and not /usr/bin/node

To upgrade to latest version (and not current stable) version, you can use

sudo n latest

To undo:

sudo apt-get install --reinstall nodejs-legacy     # fix /usr/bin/node
sudo n rm 6.0.0     # replace number with version of Node that was installed
sudo npm uninstall -g n

If you get the following error bash: /usr/bin/node: No such file or directory then the path you have entered at

sudo ln -sf /usr/local/n/versions/node/<VERSION>/bin/node /usr/bin/nodejs

if wrong. so make sure to check if the update nodejs has been installed at the above path and the version you are entered is correct.

I would advise strongly against doing this on a production instance. It can seriously mess stuff up with your global npm packages and your ability to install new one.

Install npm => sudo apt-get install npm

Install n => sudo npm install n -g

latest version of node => sudo n latest

Specific version of node you can

List available node versions => n ls

Install a specific version => sudo n 4.5.0

For Cygwin users:

Installing n (node version manager) in Cygwin doesn't work, instead update node with:

wget https://nodejs.org/download/release/latest/win-x64/node.exe -OutFile 'C:\pathto\nodejs\node.exe'
# Updating npm
npm i -g npm

Yes, you need to install wget first.

共30条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问