如何更新 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
接近 3 年之前 回复
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个回答

Sometimes it's just simpler to download the latest version from http://nodejs.org/

Especially when all other options fail.

http://nodejs.org/ -> click INSTALL -> you'll have the latest node and npm

Simple!

csdnceshi63
elliott.david If you're using OS X or Windows, the best way to install or update Node.js and npm is to use one of the installers from the Node.js download link , when I were downloaded and extracted, it simply updated node and npm version on my macbook pro os x 10.13.
接近 3 年之前 回复
csdnceshi79
python小菜 This comment is a dupe of stackoverflow.com/a/13770464/687549
5 年多之前 回复
weixin_41568196
撒拉嘿哟木头 This worked for me, thanks. I was having issues getting to the latest, and screwed things up when I unknowingly emptied the npm cache, heh. Had to (re)install the msi (on Windows) to get fully updated. Worked perfectly.
5 年多之前 回复
csdnceshi76
斗士狗 I probably installed it normally using brew or the installer. It might have been a specific bug in a specific npm version. I will probably never know and its probably doesn't matter - it was updating perfectly since than using npm update.
接近 6 年之前 回复
csdnceshi68
local-host in that case, you probably installed it in a bad way to begin with. Node/npm is one of those you want to prefix your installation so you don't need sudo for everything.
接近 6 年之前 回复
csdnceshi76
斗士狗 Definitely simpler, but doesn't always work. I've struggled with all of the above suggestion and some more - none worked on my mac. Going to nodejs.org fixed it.
大约 6 年之前 回复
csdnceshi75
衫裤跑路 the command 'npm update' is simpler.
大约 6 年之前 回复

Try the latest stable version of npm

See what version of npm you're running:

npm -v

Upgrading on *nix (OSX, Linux, etc.)

(You may need to prefix these commands with sudo, especially on Linux, or OS X if you installed Node using its default installer.)

You can upgrade to the latest version of npm using:

npm install -g npm@latest

Or upgrade to the most recent release:

npm install -g npm@next

Upgrading on Windows


By default, npm is installed alongside node in

C:\Program Files (x86)\nodejs

npm's globally installed packages (including, potentially, npm itself) are stored separately in a user-specific directory (which is currently

C:\Users\<username>\AppData\Roaming\npm).

Because the installer puts

C:\Program Files (x86)\nodejs

before

C:\Users\<username>\AppData\Roaming\npm

on your PATH, it will always use the version of npm installed with node instead of the version of npm you installed using npm -g install npm@<version>.

To get around this, you can do one of the following:

  • Option 1: edit your Windows installation's PATH to put %appdata%\npm before %ProgramFiles%\nodejs. Remember that you'll need to restart cmd.exe (and potentially restart Windows) when you make changes to PATH or how npm is installed.

  • Option 2: remove both of

    • %ProgramFiles%\nodejs\npm
    • %ProgramFiles%\nodejs\npm.cmd
  • Option 3: Navigate to %ProgramFiles%\nodejs\node_modules\npm and copy the npmrcfile to another folder or the desktop. Then open cmd.exe and run the following commands:

cd %ProgramFiles%\nodejsnpm install npm@latest

If you installed npm with the node.js installer, after doing one of the previous steps, do the following.

  • Option 1 or 2

    • Go into %ProgramFiles%\nodejs\node_modules\npm and copy the file named npmrc in the new npm folder, which should be %appdata%\npm\node_modules\npm. This will tell the new npm where the global installed packages are.
  • Option 3

    • Copy the npmrc file back into %ProgramFiles%\nodejs\node_modules\npm

A brief note on the built-in Windows configuration

The Node installer installs, directly into the npm folder, a special piece of Windows-specific configuration that tells npm where to install global packages. When npm is used to install itself, it is supposed to copy this special builtin configuration into the new install. There was a bug in some versions of npm that kept this from working, so you may need to go in and fix that up by hand. Run the following command to see where npm will install global packages to verify it is correct.

npm config get prefix -g

If it isn't set to <X>:\Users\<user>\AppData\Roaming\npm, you can run the below command to correct it:

npm config set prefix "${APPDATA}/npm" -g

Incidentally, if you would prefer that packages not be installed to your roaming profile (because you have a quota on your shared network, or it makes logging in or out from a domain sluggish), you can put it in your local app data instead:

npm config set prefix "${LOCALAPPDATA}/npm" -g

...as well as copying %APPDATA%\npm to %LOCALAPPDATA%\npm (and updating your %PATH%, of course).

Everyone who works on npm knows that this process is complicated and fraught, and we're working on making it simpler. Stay tuned.

Source: https://docs.npmjs.com/troubleshooting/try-the-latest-stable-version-of-npm

Just run the below scripts on console:

sudo npm i -g n
sudo n stable
sudo npm update -g npm

This will work for Linux and MAC only

Here is a simple fix for those who installed node via Homebrew without npm and later on struggled with npm upgrade/installation using an official script. This approach assumes you have run the node installation as follows:

brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh

If above failed then start from here. Remove npm if any:

rm -rf ~/.npm-packages/lib/node_modules/npm

Download and unpack the latest version of npm, currently at 5.6.0:

cd ~
curl -L https://registry.npmjs.org/npm/-/npm-5.6.0.tgz | tar xz

Move unpacked package into node_modules folder:

mv ~/package ~/.npm-packages/lib/node_modules/npm

Make sure your ~/.bash_profile has following entries:

export NPM_PACKAGES="$HOME/.npm-packages"
export NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
export PATH="$NPM_PACKAGES/bin:$PATH"

Source the file:

source ~/.bash_profile

Verify installation:

npm -v

Upgrading for Windows Users

Windows users should read Troubleshooting > Upgrading on Windows in the npm wiki.

Upgrading on windows 10 using PowerShell (3rd party edit)

The link above Troubleshooting#upgrading-on-windows points to a github page npm-windows-upgrade the lines below are quotes from the readme. I successfully upgraded from npm 2.7.4 to npm 3.9.3 using node v5.7.0 and powershell (presumably powershell version 5.0.10586.122)

First, ensure that you can execute scripts on your system by running the following command from an elevated PowerShell. To run PowerShell as Administrator, click Start, search for PowerShell, right-click PowerShell and select Run as Administrator.

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force    

Then, to install and use this upgrader tool, run (also from an elevated PowerShell or cmd.exe):

npm install --global --production npm-windows-upgrade
npm-windows-upgrade
csdnceshi65
larry*wei No Gal, you are confusing the issue, running npm install -g npm does not work on Windows, have you read the issues Windows users have had (listed above ?). Use the Powershell method as shown in Surf's answer. Thanks smurf , finally managed to upgrade yeeehaaaa!
大约 4 年之前 回复
weixin_41568208
北城已荒凉 or just run: npm install -g npm
4 年多之前 回复
csdnceshi60
℡Wang Yan github.com/felixrieseberg/npm-windows-upgrade
4 年多之前 回复

I recently stumbled across this article: http://martineau.tv/blog/2013/12/more-efficient-grunt-workflows/ and the author mentions $ npm-check-updates -u && npm install to update all dependencies.

This is a little off the topic but I ended up here on a similar search so thought it was worth the share.

csdnceshi80
胖鸭 david-dm.org And this just appeared as a comment from the post I cited. Seems perfect...
6 年多之前 回复

you should see this blog nodejs install with package-manager

Before you performance this command. you show run sudo apt-get update, make sure result is Reading package lists... Done, no ERROR

Step by Step (Debian):

sudo apt-get update

install 6_x

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

install 7_x

curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs

Just run the following command in terminal as root/administrator:

npm i -g n
n stable
npm update -g npm

It has worked for me on Linux

csdnceshi66
必承其重 | 欲带皇冠 use brew update then brew upgrade node not n
2 年多之前 回复

Just with this code

npm install update

To update npm :

npm install npm@{version} -g

to update npm to the latest version:

npm install npm@latest -g

and to check the version :

npm -v

to update node js :

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

to check :

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