如何将 Node.js 和 npm 更新到最新版本?
我刚刚安装了 Node.js 和 npm
。我安装了 npm
来访问其他 Node.js 模块。安装 Node.js 和 npm
后,我注意到两者都没有可用的最新版本。
所以我的问题是:
- 如何将 Node.js、
npm
和我的 Node.js 模块 升级到最新版本? - 我是否需要卸载 Node.js 和
npm
,然后重新安装最新版本?
I just installed Node.js and npm
. I installed npm
for access to additional Node.js modules. After I installed Node.js and npm
, I noticed that neither were the latest versions available.
So my questions are:
- How do I upgrade Node.js,
npm
and my Node.js modules to their latest versions? - Do I need to uninstall Node.js and
npm
, and reinstall the latest versions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
我在 David Walsh 的博客上发现了这种更新 Node.js 的非常巧妙的方法。您可以通过安装
n
来完成此操作:它将安装当前稳定版本的 Node.js。
更新
但请不要再使用
n
。我建议使用nvm
。您可以按照以下命令简单地安装稳定版:I found this really neat way of updating Node.js on David Walsh's blog. You can do it by installing
n
:It will install the current stable version of Node.js.
UPDATE
But please don't use
n
anymore. I recommend usingnvm
. You can simply install stable by following the commands below:更新 npm 很简单:
Updating npm is easy:
我知道这个问题是针对 Linux 机器的,但以防万一有人正在寻找 Windows 解决方案,只需转到 Node.js站点,点击主页上的下载按钮并执行安装程序。
值得庆幸的是,它处理了一切,只需点击几下 Next 按钮,我就可以在我的 Windows 7 计算机。
I understand this question is for a 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 the Next button, I got the latest 0.8.15 Node.js version running on my Windows 7 machine.
您可能已经知道,npm 目前与 Node.js 捆绑在一起。这意味着如果您安装了 Node.js,那么您也已经安装了 npm。
另外,请注意 Node.js 和 npm 发行版本表,它向我们展示了大致的版本兼容性。有时,版本差异可能会导致不兼容错误。
因此,如果您是一名开发人员,那么使用 Node.js 版本管理器之一来管理您的开发环境是一种“最佳实践”。
以下是一些最流行的列表和使用说明:
Homebrew (macOS)
如果您使用的是 macOS,则可以使用 自制。
实际上,它不仅仅是一个 Node.js 版本管理器。
要将 Homebrew 安装到您的 Mac:
要使用 Homebrew 安装 Node.js 和 npm,请运行:
稍后,您将能够使用以下命令更新它们:
另外,您也可以在 Node.js 版本之间切换:
npm 将自动升级/降级。
n (macOS、Linux)
n 最有可能rvm< /em> (Ruby Version Manager),用于同时管理 Node.js 和 npm 版本。它是在纯 Linux shell 上编写的,并作为 npm 模块 提供。因此,如果您已经安装了任何 Node.js 版本,则可以通过 npm 安装/更新 n 软件包:
下载、安装并切换到 Node.js 和 npm 版本非常简单:
要下载、安装并切换到最新官方版本,请使用:
要下载、安装并切换到最新稳定版
要切换到以前的活动版本(又名
$ cd -
),使用:如果您想查看已安装的 Node.js 版本列表,只需运行
n
从你的命令行。输出将类似于以下内容:输出:
其中点 (•) 表示它是当前活动版本。要从列表中选择另一个 Node.js 版本,请使用 向上/向下 箭头键并使用 Enter 键激活。
要列出可安装的版本:
nvm (macOS、Linux)
nvm is也和rvm一样,连命令名称和用法都非常相似。
要安装 nvm,您可以使用安装脚本(需要 Git)
cURL
:或 Wget:
下载并安装特定的 Node.js和 npm 版本,使用:
然后,您可以切换到已安装的版本,使用:
另外,您可以创建包含版本号的 .nvmrc 文件,然后使用以下命令切换到指定版本:
要查看已安装的 Node.js 版本列表,请使用:
要列出可安装的版本:
nvm-windows (Windows)
nvm-windows 是一个适用于 Windows 的 Node.js 版本管理实用程序,具有讽刺意味的是,它是用 Go。
它与 nvm 不同。然而,作为 Node.js 版本管理器的用法非常相似。
要安装 nvm-windows,需要事先卸载任何现有版本的 Node.js 和 npm。然后,下载并运行版本中的最新安装程序。
要升级 nvm-windows,请运行新的安装程序。它将安全地覆盖需要更新的文件,而不会影响您的 Node.js 安装。
nvm-windows 在管理 shell 中运行。您需要以管理员身份启动 PowerShell 或命令提示符才能使用 nvm-windows。
在使用之前,您可能还需要使用以下命令启用 nvm-windows:
要下载并安装特定的 Node.js 和 npm 版本,请使用:
然后,您可以切换到已安装的版本,使用:
如果您想查看已安装 Node.js 版本的列表,请使用:
要列出可安装的版本:
As you may already know, npm is currently bundled with Node.js. It means that if you have installed Node.js, you've already installed npm as well.
Also, pay attention to the Node.js and npm release versions table that shows us approximate versions compatibility. Sometimes, versions discrepancy may cause incompatibility errors.
So, if you're a developer, it's kind of "best practice" to manage your development environment using one of the Node.js version managers.
Here is a list and usage notes of some of the most popular:
Homebrew (macOS)
If you're on macOS, you can use Homebrew.
Actually, it's not just a Node.js version manager.
To install Homebrew to your Mac:
To install Node.js and npm using Homebrew, run:
Later, you will be able to update them using:
Also, you can switch between Node.js versions as well:
npm will be upgraded/downgraded automatically.
n (macOS, Linux)
n is most likely to rvm (Ruby Version Manager), and is used to manage Node.js and npm versions simultaneously. It is written on pure Linux shell, and available as an npm module. So, if you already have any Node.js version installed, you can install/update the n package through npm:
Downloading, installing and switching to Node.js and npm versions is as easy as:
To download, install, and switch to the latest official release, use:
To download, install, and switch to the latest stable official release, use:
To switch to the previously active version (aka
$ cd -
), use:If you want to see the list of installed Node.js versions, just run
n
from your command line. The output will be something like the following:Output:
Where the dot (•) means that it's a currently active version. To select another Node.js version from the list, use Up/Down arrow keys and activate using the Enter key.
To list the versions available to install:
nvm (macOS, Linux)
nvm is also like rvm, even the command names and usage are very similar.
To install nvm, you can use the installation script (requires Git) using
cURL
:or Wget:
To download and install a specific Node.js and npm version, use:
Then, you can switch to the installed version, using:
Also, you can create the .nvmrc file containing the version number, then switch to the specified version using the following command:
To see the list of installed Node.js versions, use:
To list the versions available to install:
nvm-windows (Windows)
nvm-windows is a Node.js version management utility for Windows, ironically written in Go.
It is not the same thing as nvm. However, the usage as a Node.js version manager is very similar.
To install nvm-windows, it is required to uninstall any existing versions of Node.js and npm beforehand. Then, download and run the latest installer from releases.
To upgrade nvm-windows, run the new installer. It will safely overwrite the files it needs to update without touching your Node.js installations.
nvm-windows runs in an Admin shell. You'll need to start PowerShell or Command Prompt as Administrator to use nvm-windows.
Before using, you may also need to enable nvm-windows with the following command:
To download and install a specific Node.js and npm version, use:
Then, you can switch to the installed version, using:
If you want to see the list of installed Node.js versions, use:
To list the versions available to install:
首先检查您的 NPM 版本
1) 将 NPM 更新到当前版本:
查看当前 NPM 版本:
将 npm 更新到当前版本:
2) 列出所有可用的 NPM 版本并进行自定义安装/更新/回滚
查看所有版本,包括“ alpha”、“beta”和“rc”(候选版本)
将 NPM 重新安装到从版本列表中选择的特定版本 - 例如 5.0.3
安装一个版本将自动删除当前安装的版本。
对于 Linux 和 iOS,使用 sudo 前置命令
First check your NPM version
1) Update NPM to current version:
View curent NPM version:
Update npm to current version:
2) List all available NPM versions and make a custom install/update/roll-back
View all versions including "alpha", "beta" and "rc" (release candidate)
Reinstall NPM to a specific version chosen from the versions list - for example to 5.0.3
Installing one version will automatically remove the one currently installed.
For Linux and iOS prepend commands with sudo
Windows 用户升级
Windows 用户应阅读 故障排除 > npm wiki 中的 Windows 升级。
使用 PowerShell 在 Windows 10 上升级(第 3 方编辑)
上面的链接 Troubleshooting#upgrading-on-windows< /a> 指向 github 页面 npm-windows-upgrade下面几行是自述文件中的引用。
我使用node v5.7.0和powershell(大概是powershell版本5.0.10586.122)成功从npm 2.7.4升级到npm 3.9.3
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)
首先更新
npm
,npm install -g npm@next
然后更新
node
到下一个版本,npm install -g node@next
或npm install -gn@next
或者,对于最新版本,
npm install -g node@latest
或npm install -g node
在版本安装后检查,
node --version
或节点-v
First update
npm
,npm install -g npm@next
Then update
node
to the next version,npm install -g node@next
ornpm install -g n@next
or, to the latest,
npm install -g node@latest
ornpm install -g node
check after version installation,
node --version
ornode -v
只需使用这个
这是当新的更新/错误修复发布时我在控制台上从 npm 得到的提示:
< img src="https://i.sstatic.net/CdKR2.png" alt="在此处输入图像描述">
SIMPLY USE THIS
This is what i get promped on my console from npm when new update/bug-fix are released:
要更新 Node.js,请使用 nvm (或 nvmw(适用于 Windows)。
要更新 npm,
npm update npm -g
命令对我不起作用(在 Windows 上)。有效的方法是根据文档重新安装 npm:“您可以从以下位置下载 ZIP 文件: https://npmjs.org/dist/,并将其解压到node.exe所在的同一文件夹中” 确保如果您这样做,首先要删除以前的安装(尽管覆盖它可能会正常工作...)。要更新模块,请使用 npm update 命令
To update Node.js, use nvm (or nvmw for Windows).
To update npm, the
npm update npm -g
command didn't work for me (on Windows). What did work was reinstalling npm according to the documentation: "You can download a ZIP file from https://npmjs.org/dist/, and unpack it in the same folder where node.exe lives." Make sure if you do this that you get rid of your previous installation first (though overwriting it will probably work ok...).To update your modules, use the npm update command
我认为管理node.js的最佳方法是使用NVM。
NVM 代表 Node Version Manager。
它是 Node.js 开发者必备的工具!
您可以使用以下命令安装 NVM,打开终端并运行以下任意一个以下:-
或
安装后建议关闭当前终端并打开一个新终端,因为 NVM 将添加一些环境变量,因此需要重新启动终端。
我将列出一些使用 NVM 的基本命令。
例如
nvm install v14.18.1
npm 在当前节点版本
npm
包从一个版本安装到另一个版本,而不是手动安装每个版本!I think the best way to manage node.js is to use NVM.
NVM stands for Node Version Manager.
It's a must-have tool for node.js developers!
You can install NVM using the following command, open terminal and run any one of the following:-
or
After installing this it's recommended to close the current terminal and open a new one since NVM will be adding some environment variables so terminal needs to be restarted.
I'll list down some of the basic commands for using NVM.
for example
nvm install v14.18.1
npm
on the current node versionnpm
packages from one version to another instead of manually installing each one of them!尝试最新稳定版本的 npm
查看您正在运行的 npm 版本:
在 *nix(OSX、Linux 等)上升级
(您可能需要在这些命令前加上
sudo
前缀,特别是在 Linux 或 OS X 上,如果您使用默认安装程序安装了 Node。)您可以使用以下方式升级到最新版本的 npm:
或者升级到最新版本:
在Windows 上升级
默认情况下,npm 与 npm 一起安装节点在
C:\Program Files (x86)\nodejs
npm 全局安装的软件包(可能包括 npm 本身)单独存储在用户特定的目录中(当前为
C:\Users\ <用户名>\AppData\Roaming\npm)。
因为安装程序将
C:\Program Files (x86)\nodejs
之前
C:\Users\\AppData\Roaming\npm
放在
PATH 上的
,它将始终使用随 Node 安装的 npm 版本,而不是使用npm -g install npm@
安装的 npm 版本。要解决此问题,您可以执行以下其中一项操作:
选项 1:编辑 Windows 安装的
PATH
,将%appdata%\npm
放在%ProgramFiles%\nodejs
之前。请记住,当您更改
PATH
或 npm 的安装方式时,您需要重新启动cmd.exe
(并可能重新启动 Windows)。选项 2:删除两者
%ProgramFiles%\nodejs\npm
%ProgramFiles%\nodejs\npm.cmd
选项 3:导航到
%ProgramFiles%\nodejs\node_modules\npm
并将npmrc
文件复制到另一个文件夹或桌面。然后打开 cmd.exe 并运行以下命令:
cd %ProgramFiles%\nodejsnpm install npm@latest
如果您使用 Node.js 安装程序安装了 npm,则在执行上述步骤之一后,请执行以下操作。
选项 1 或 2
%ProgramFiles%\nodejs\node_modules\npm
并将名为npmrc
的文件复制到新的 npm 文件夹中,该文件夹应为%appdata%\ npm\node_modules\npm
。这将告诉新的 npm 全局安装的软件包在哪里。选项3
%ProgramFiles%\nodejs\node_modules\npm
关于内置 Windows 配置的简要说明
Node 安装程序将 Windows 的一个特殊部分直接安装到 npm 文件夹中- 特定配置,告诉 npm 在哪里安装全局包。当 npm 用于安装自身时,它应该将这个特殊的内置配置复制到新安装中。 npm 的某些版本中存在一个错误,导致此功能无法正常工作,因此您可能需要手动修复该问题。运行以下命令查看 npm 将在何处安装全局包以验证其是否正确。
如果未设置为
:\Users\\AppData\Roaming\npm
,您可以运行以下命令来更正它:顺便说一句,如果您愿意的话软件包不会安装到您的漫游配置文件中(因为您的共享网络有配额,或者它会使域登录或注销变得缓慢),您可以将其放入本地应用程序数据中:
...以及复制
%APPDATA%\npm
到%LOCALAPPDATA%\npm
(当然,还要更新您的%PATH%
)。每个使用 npm 的人都知道这个过程既复杂又令人担忧,我们正在努力使其变得更简单。敬请关注。
来源: https://docs.npmjs.com/troubleshooting /尝试npm的最新稳定版本
Try the latest stable version of npm
See what version of npm you're running:
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:
Or upgrade to the most recent release:
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 usingnpm -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 toPATH
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 thenpmrc
file 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
%ProgramFiles%\nodejs\node_modules\npm
and copy the file namednpmrc
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
%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.If it isn't set to
<X>:\Users\<user>\AppData\Roaming\npm
, you can run the below command to correct it: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:
...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
帮我从 1.4.28 更新到 2.1.5
Worked for me to update from 1.4.28 to 2.1.5
安装 npm =>
sudo apt-get install npm
安装 n =>
sudo npm install n -g
最新版本的节点 =>
sudo nlatest
您可以使用的特定节点版本
列出可用的节点版本 =>
n ls
安装特定版本 =>
sudo n 4.5.0
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
使用 npm 安装最新版本的 npm:
我在 Linux 上运行它,所以我不确定其他操作系统。
在 Linux 上,您还可以运行:
这将告诉 apt-get 包管理器更新和升级所有包。
To install the latest version of npm using npm:
I run this on Linux so I am not sure about other operating systems.
On Linux you can also run:
This will tell the
apt-get
package manager to update and upgrade all packages.我刚刚在一台新的 Windows 7 机器上安装了 Node.js,结果如下:
然后我执行了上述过程:
并且它升级到了 v2.7.3。除了执行
npm -v
仍然给出 2.5.1。我进入系统配置面板,高级设置,环境变量。除了全局 Path 变量之外,我还看到了特定于我的用户帐户的 PATH 变量。
前者指向新的npm:
C:\Users\PhiLho\AppData\Roaming\npm
后者包括节点的路径:
C:\PrgCmdLine\nodejs\
(现在,我避免在程序文件和派生文件中安装东西。避免路径中的空格和嘈杂无用的保护是更明智的...... )如果我执行
which npm.cmd
(我安装了 Unix 实用程序...),它会指向 Node.js 中的那个。不管怎样,修复很简单:我只是在主全局 Path 变量中的节点路径之前复制了第一个路径(到 npm),现在它选择了最新版本。
<之前的一些东西>;C:\Users\PhiLho\AppData\Roaming\npm;C:\PrgCmdLine\nodejs\
享受吧。 :-)
I just installed Node.js on a new Windows 7 machine, with the following results:
I then did the above described procedure:
and it upgraded to v2.7.3. Except than doing
npm -v
still gave 2.5.1.I went to the System configuration panel, advanced settings, environment variables. I saw a PATH variable specific to my user account, in addition to the global Path variable.
The former pointed to new npm:
C:\Users\PhiLho\AppData\Roaming\npm
The latter includes the path to node:
C:\PrgCmdLine\nodejs\
(Nowadays, I avoid to install stuff in Program Files and derivates. Avoiding spaces in paths, and noisy useless protections is saner...)If I do
which npm.cmd
(I have Unix utilities installed...), it points to the one in Node.Anyway, the fix is simple: I just copied the first path (to npm) just before the path to node in the main, global Path variable, and now it picks up the latest version.
<some stuff before>;C:\Users\PhiLho\AppData\Roaming\npm;C:\PrgCmdLine\nodejs\
Enjoy. :-)
适用于 Linux、OSX 等。
安装最新版本的 NPM
npm install -g npm@latest
或者安装最新版本
npm install -g npm@next
其他:检查您的 npm 版本
npm -v
如果您使用的是 Windows 计算机,我建议您转到 npm 网站
For Linux, OSX, etc..
To install the latest version of NPM
npm install -g npm@latest
Or To Install the most recent release
npm install -g npm@next
Additional : To check your npm version
npm -v
If you are in a Windows Machine, I suggest going to the npm website
只需以 root/管理员身份在终端中运行以下命令:
它在 Linux 上对我有用
Just run the following command in terminal as root/administrator:
It has worked for me on Linux
你可以尝试这个
npm install -g npm@latest
You could try this
npm install -g npm@latest
我最近偶然发现了这篇文章: http://martineau.tv/ blog/2013/12/more-efficient-grunt-workflows/ 并且作者提到了
npm-check-updates -u && npm install
来更新所有依赖项。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.您可以使用以下命令升级到 npm 的最新版本:
You can upgrade to the latest version of npm using:
更新 npm :
将 npm 更新到最新版本:
并检查版本:
更新 Node js :
检查:
To update npm :
to update npm to the latest version:
and to check the version :
to update node js :
to check :
有时,从 http://nodejs.org/ 下载最新版本会更简单,
尤其是当所有其他选项都失败时。
http://nodejs.org/ ->点击安装->您将拥有最新的节点和 npm
简单!
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!
刚刚听了 npm 团队关于最新一集 nodeup 的采访,他们建议不要使用
update
用于从1.x
更新到2.x
。相反,请使用:<代码>npm 安装 npm -g
Just listened to an interview with the npm team on the latest episode of nodeup, and they recommended not using
update
for the update from1.x
to2.x
. Instead, use:npm install npm -g
只需使用这段代码
Just with this code
当谈到
Linux
时,我建议使用包管理器更新节点:Node 预装了 npm ,但是 manager 的更新比 Node 更频繁。运行 npm -v 查看您拥有的版本,然后
npm install npm@latest -g
安装最新的 npm 更新。如果您想确保 npm 正确更新,请再次运行npm -v
。要更新 NodeJS,您需要 npm 方便的 n 模块。运行此代码以清除 npm 的缓存,安装 n,并安装
Node
的最新稳定版本:要安装最新版本,请使用
nlatest
。或者,您可以运行 n #.#.# 来获取特定的Node
版本。对于
Windows/ macOS
,我建议使用 Nodejs.org 上的安装程序Node.js 下载页面包括适用于 Windows 和 macOS 的二进制包 — 但为什么会让你的生活变得更加困难呢?预制安装程序(适用于 Windows 的 .msi 和适用于 macOS 的 .pkg)使安装过程极其高效且易于理解。下载并运行该文件,然后让安装向导完成剩下的工作。每次下载更新后,新版本的 Node 和 npm 将取代旧版本。
或者,macOS 用户可以使用上面的 npm 和 n 指令。
在更新
node_modules
依赖项文件夹时,我建议跳过所有可能让您头疼的事情,只需转到您的特定项目并再次重新运行npm install
即可。在任何人执行此操作之前,我建议首先检查您的
package.json
文件中的以下内容:作为 NodeJS 包的用户,您可以在 package.json 文件中指定您的应用程序可以接受哪些类型的更新。例如,如果您从软件包版本 1.0.4 开始,则可以通过三种基本方式指定允许的更新版本范围:
允许补丁版本:1.0 或 1.0.x 或 ~ 1.0.4
允许次要版本:1或1.x或^1.0.4
允许主要版本:* 或 x
说明:
MAJOR 版本(当存在不兼容的 API 更改时)。 -->
~
MINOR 版本,用于以向后兼容的方式添加功能时。 -->
^
PATCH 版本,用于完成向后兼容的错误修复。 --> <代码>*
When it comes to
Linux
I suggest an Update Node Using a Package Manager:Node comes with npm pre-installed, but the manager is updated more frequently than Node. Run npm -v to see which version you have, then
npm install npm@latest -g
to install the newest npm update. Runnpm -v
again if you want to make sure npm updated correctly.To update
NodeJS
, you’ll need npm’s handy n module. Run this code to clear npm’s cache, install n, and install the latest stable version ofNode
:To install the latest release, use
n latest
. Alternatively, you can run n #.#.# to get a specificNode
version.When it comes to
Windows/ macOS
I suggest using Installers on Nodejs.orgThe Node.js downloads page includes binary packages for Windows and macOS — but why make your life more difficult? The pre-made installers — .msi for Windows and .pkg for macOS — make the installation process unbelievably efficient and understandable. Download and run the file, and let the installation wizard take care of the rest. With each downloaded update, the newer versions of Node and npm will replace the older version.
Alternatively, macOS users can use the npm and n instructions above.
When it comes to updating your
node_modules
dependencies folder, I suggest skipping all the things that could cause you a headache and just go to your specific project and re-runnpm install
again.Before anyone does that, I suggest first checking your
package.json
file for the following:As a user of NodeJS packages, you can specify which kinds of updates your app can accept in the package.json file. For example, if you were starting with a package version 1.0.4, this is how you could specify the allowed update version ranges in three basic ways:
To Allow Patch Releases: 1.0 or 1.0.x or ~1.0.4
To Allow Minor Releases: 1 or 1.x or ^1.0.4
To Allow Major Releases: * or x
Explanation:
MAJOR version for when there are incompatible API changes. -->
~
MINOR version for when functionality is added in a backwards compatible manner. -->
^
PATCH version for when backward compatible bug fixes are done. -->
*
对于nodejs应该卸载它并从nodejs.org下载您最喜欢的版本
对于 npm 在 cmd 中运行以下行:
for nodejs should uninstall it and download your favorite version from nodejs.org
for npm run below line in cmd:
警告:如果您需要从旧版本更新 Node(在我的例子中
v4.6.0
),最好从头开始重新安装 Nodejs(下载链接:https://nodejs.org),否则 npm 也会将自身更新到与新 Node 不兼容的版本(请参阅 此讨论)。这是我使用 npm 更新 Node(在 Windows 上)后收到的错误消息
新安装 npm 再次工作后:
Warning: if you need update Node from an old version (in my case
v4.6.0
) it is better to re-install nodejs from scratch (download link: https://nodejs.org) otherwise npm will also update itself to a version that's not compatible with the new Node (see this discussion).This is the error message that I got after updating Node (on Windows) with npm
After new installation npm works again:
另外,如果您想更新到特定版本,请按照以下步骤操作:
Also if you want to update to a particular version, follow this:
只需在控制台上运行以下脚本:
这仅适用于 Linux 和 MAC
Just run the below scripts on console:
This will work for Linux and MAC only
如何更新
npm
使用以下命令:
请参阅<代码>更新命令:
此外,请参阅有关 Node.js 和 npm 安装 和升级 npm。
以下原始答案来自不再存在的旧常见问题解答,但它应该适用于 Linux 和 Mac:
如何更新 Node.js
要更新 Node.js 本身,我建议您使用
nvm
(节点版本管理器)。以下是官方npm
的引用 文档:以下是使用
nvm
工具执行此操作的方法:要安装 Node 的最新版本,请执行以下操作:
(注意:这里
node
是最新版本的别名)安装节点的特定版本:
更多详细信息可以在官方
nvm
GitHub 页面的 "用法" 部分。How to update
npm
Use the following command:
See the documentation for the
update
command:Additionally, see the documentation on Node.js and npm installation and Upgrading npm.
The following original answer is from the old FAQ that no longer exists, but it should work for Linux and Mac:
How to update Node.js
To update Node.js itself, I recommend you use
nvm
(Node Version Manager). Here is the quote from the officialnpm
documentation:Here is how you can do it using
nvm
tool:To install the latest release of Node, do this:
(NOTE: here
node
is an alias for the latest version)To install a specific version of node:
More details can be found on the official
nvm
GitHub page in the "Usage" section.