如何将 Node.js 和 npm 更新到最新版本?

发布于 2024-11-13 04:46:59 字数 358 浏览 1 评论 0原文

我刚刚安装了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

贵在坚持 2024-11-20 04:47:00

我在 David Walsh 的博客上发现了这种更新 Node.js 的非常巧妙的方法。您可以通过安装 n 来完成此操作:

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

它将安装当前稳定版本的 Node.js。


更新

但请不要再使用 n 。我建议使用 nvm。您可以按照以下命令简单地安装稳定版:

nvm ls-remote
nvm install <version>
nvm use <version>

I found this really neat way of updating Node.js 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.js.


UPDATE

But please don't use n anymore. I recommend using nvm. You can simply install stable by following the commands below:

nvm ls-remote
nvm install <version>
nvm use <version>
绝不放开 2024-11-20 04:47:00

更新 npm 很简单:

npm install npm@latest -g

Updating npm is easy:

npm install npm@latest -g
千と千尋 2024-11-20 04:47:00

我知道这个问题是针对 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.

怀中猫帐中妖 2024-11-20 04:47:00

您可能已经知道,npm 目前与 Node.js 捆绑在一起。这意味着如果您安装了 Node.js,那么您也已经安装了 npm。

另外,请注意 Node.js 和 npm 发行版本表,它向我们展示了大致的版本兼容性。有时,版本差异可能会导致不兼容错误。

因此,如果您是一名开发人员,那么使用 Node.js 版本管理器之一来管理您的开发环境是一种“最佳实践”。

以下是一些最流行的列表和使用说明:

Homebrew (macOS)

如果您使用的是 ma​​cOS,则可以使用 自制

实际上,它不仅仅是一个 Node.js 版本管理器。

要将 Homebrew 安装到您的 Mac:

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

要使用 Homebrew 安装 Node.js 和 npm,请运行:

brew install node

稍后,您将能够使用以下命令更新它们:

brew update && brew upgrade node

另外,您也可以在 Node.js 版本之间切换

brew switch node 0.10.26

npm 将自动升级/降级

n (macOS、Linux)

n 最有可能rvm< /em> (Ruby Version Manager),用于同时管理 Node.js 和 npm 版本。它是在纯 Linux shell 上编写的,并作为 npm 模块 提供。因此,如果您已经安装了任何 Node.js 版本,则可以通过 npm 安装/更新 n 软件包:

npm install -g n

下载、安装并切换到 Node.js 和 npm 版本非常简单:

n 0.10.26
n 0.8.17
n 0.9.6

要下载、安装并切换到最新官方版本,请使用:

n latest

要下载、安装并切换到最新稳定版

n stable

要切换到以前的活动版本(又名 $ cd -),使用:

n prev

如果您想查看已安装的 Node.js 版本列表,只需运行 n 从你的命令行。输出将类似于以下内容:

n

输出:

  0.10.26
• 0.8.17
  0.9.6

其中点 (•) 表示它是当前活动版本。要从列表中选择另一个 Node.js 版本,请使用 向上/向下 箭头键并使用 Enter 键激活。

要列出可安装的版本:

n lsr

nvm (macOS、Linux)

nvm is也和rvm一样,连命令名称和用法都非常相似。

要安装 nvm,您可以使用安装脚本(需要 Git) cURL

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

Wget

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

下载并安装特定的 Node.js和 npm 版本,使用:

nvm install 0.10

然后,您可以切换到已安装的版本,使用:

nvm use 0.10

另外,您可以创建包含版本号的 .nvmrc 文件,然后使用以下命令切换到指定版本:

nvm use

要查看已安装的 Node.js 版本列表,请使用:

nvm ls

要列出可安装的版本:

nvm ls-remote

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:

nvm on

要下载并安装特定的 Node.js 和 npm 版本,请使用:

nvm install 0.12

然后,您可以切换到已安装的版本,使用:

nvm use 0.12

如果您想查看已安装 Node.js 版本的列表,请使用:

nvm list

要列出可安装的版本:

nvm list available

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:

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

To install Node.js and npm using Homebrew, run:

brew install node

Later, you will be able to update them using:

brew update && brew upgrade node

Also, you can switch between Node.js versions as well:

brew switch node 0.10.26

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:

npm install -g n

Downloading, installing and switching to Node.js and npm versions is as easy as:

n 0.10.26
n 0.8.17
n 0.9.6

To download, install, and switch to the latest official release, use:

n latest

To download, install, and switch to the latest stable official release, use:

n stable

To switch to the previously active version (aka $ cd -), use:

n prev

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:

n

Output:

  0.10.26
• 0.8.17
  0.9.6

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:

n lsr

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:

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

To download and install a specific Node.js and npm version, use:

nvm install 0.10

Then, you can switch to the installed version, using:

nvm use 0.10

Also, you can create the .nvmrc file containing the version number, then switch to the specified version using the following command:

nvm use

To see the list of installed Node.js versions, use:

nvm ls

To list the versions available to install:

nvm ls-remote

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:

nvm on

To download and install a specific Node.js and npm version, use:

nvm install 0.12

Then, you can switch to the installed version, using:

nvm use 0.12

If you want to see the list of installed Node.js versions, use:

nvm list

To list the versions available to install:

nvm list available
携余温的黄昏 2024-11-20 04:47:00

首先检查您的 NPM 版本

npm -v

1) 将 NPM 更新到当前版本:

查看当前 NPM 版本:

npm view npm version

将 npm 更新到当前版本:

npm i -g npm

2) 列出所有可用的 NPM 版本并进行自定义安装/更新/回滚

查看所有版本,包括“ alpha”、“beta”和“rc”(候选版本)

npm view npm versions --json

将 NPM 重新安装到从版本列表中选择的特定版本 - 例如 5.0.3

npm i -g [email protected]
  • 安装一个版本将自动删除当前安装的版本。

  • 对于 Linux 和 iOS,使用 sudo 前置命令

First check your NPM version

npm -v

1) Update NPM to current version:

View curent NPM version:

npm view npm version

Update npm to current version:

npm i -g npm

2) List all available NPM versions and make a custom install/update/roll-back

View all versions including "alpha", "beta" and "rc" (release candidate)

npm view npm versions --json

Reinstall NPM to a specific version chosen from the versions list - for example to 5.0.3

npm i -g [email protected]
  • Installing one version will automatically remove the one currently installed.

  • For Linux and iOS prepend commands with sudo

若沐 2024-11-20 04:47:00

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

首先,确保您可以通过运行在系统上执行脚本
来自提升的 PowerShell 的以下命令。运行 PowerShell
以管理员身份,单击开始,搜索 PowerShell,右键单击
PowerShell 并选择以管理员身份运行。

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force    

然后,要安装并使用此升级程序工具,请运行(也可以从
提升的 PowerShell 或 cmd.exe):

npm install --global --production npm-windows-upgrade
npm-windows-upgrade

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
貪欢 2024-11-20 04:47:00

首先更新npm

npm install -g npm@next

然后更新node到下一个版本,

npm install -g node@nextnpm install -gn@next
或者,对于最新版本,

npm install -g node@latestnpm 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 or npm install -g n@next
or, to the latest,

npm install -g node@latest or npm install -g node

check after version installation,

node --versionor node -v

孤蝉 2024-11-20 04:47:00

只需使用这个

npm i -g npm

这是当新的更新/错误修复发布时我在控制台上从 npm 得到的提示:

< img src="https://i.sstatic.net/CdKR2.png" alt="在此处输入图像描述">

SIMPLY USE THIS

npm i -g npm

This is what i get promped on my console from npm when new update/bug-fix are released:

enter image description here

白色秋天 2024-11-20 04:47:00
  • 要更新 Node.js,请使用 nvm (或 nvmw(适用于 Windows)。

  • 更新 npmnpm 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

甜警司 2024-11-20 04:47:00

我认为管理node.js的最佳方法是使用NVM
NVM 代表 Node Version Manager。

它是 Node.js 开发者必备的工具!

您可以使用以下命令安装 NVM,打开终端并运行以下任意一个以下:-

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

wget -qO- https://raw.githubusercontent.com/nvm-sh//nvm/v0.39.0/install.sh | bash

安装后建议关闭当前终端并打开一个新终端,因为 NVM 将添加一些环境变量,因此需要重新启动终端。

我将列出一些使用 NVM 的基本命令。

  • 这将从互联网获取所有节点版本。将显示从开始至今的所有节点版本,它还将同时提及 LTS 版本。
nvm ls-remote 
  • 这将向您显示应该使用的 npm 和 node 的推荐版本。 (仅当您已经安装了 npm 版本时才有效,如果不使用上述命令)
npm doctor
  • 这将安装您想要的节点版本(使用上述命令获取版本列表)
nvm install <version recommended by 'npm doctor' or 'nvm ls-remote'>

例如 nvm install v14.18.1

  • 此命令将为我们提供本地安装的节点版本列表
nvm ls
  • 此命令用于从计算机中删除您想要的节点版本
nvm uninstall v10.15.1
  • 以下命令将帮助您升级到最新的工作 npm 在当前节点版本
nvm install-latest-npm
  • NVM上即可用于同时管理多个节点版本
  • 它还可以帮助您将所有全局 npm 包从一个版本安装到另一个版本,而不是手动安装每个版本!
  • nvm 还有许多其他用途,详细信息和命令可以在这里找到 Node Version Manager

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:-

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/nvm-sh//nvm/v0.39.0/install.sh | bash

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.

  • This will fetch all the node versions from the internet. All node versions from beginning till date will be shown, It will also mention LTS versions alongside.
nvm ls-remote 
  • This will show you the recommended versions of npm and node you should use. (This works only if you already have a version of npm installed, if not use the above command)
npm doctor
  • This will install the node version which you want (version list is obtained using the above command)
nvm install <version recommended by 'npm doctor' or 'nvm ls-remote'>

for example nvm install v14.18.1

  • This command will give us the list of node versions that are installed locally
nvm ls
  • This command is used to remove the node version that you want from your computer
nvm uninstall v10.15.1
  • The following command will help you upgrade to the latest working npm on the current node version
nvm install-latest-npm
  • NVM can be used to manage multiple node versions simultaneously
  • It can also help you install all the global npm packages from one version to another instead of manually installing each one of them!
  • There are many other uses of nvm the details of which and the commands can be found here Node Version Manager
少女的英雄梦 2024-11-20 04:47:00

尝试最新稳定版本的 npm

查看您正在运行的 npm 版本:

npm -v

在 *nix(OSX、Linux 等)上升级

(您可能需要在这些命令前加上 sudo 前缀,特别是在 Linux 或 OS X 上,如果您使用默认安装程序安装了 Node。)

您可以使用以下方式升级到最新版本的 npm:

npm install -g npm@latest

或者升级到最新版本:

npm install -g npm@next

在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

    • 将 npmrc 文件复制回 %ProgramFiles%\nodejs\node_modules\npm

关于内置 Windows 配置的简要说明

Node 安装程序将 Windows 的一个特殊部分直接安装到 npm 文件夹中- 特定配置,告诉 npm 在哪里安装全局包。当 npm 用于安装自身时,它应该将这个特殊的内置配置复制到新安装中。 npm 的某些版本中存在一个错误,导致此功能无法正常工作,因此您可能需要手动修复该问题。运行以下命令查看 npm 将在何处安装全局包以验证其是否正确。

npm config get prefix -g

如果未设置为 :\Users\\AppData\Roaming\npm,您可以运行以下命令来更正它:

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

顺便说一句,如果您愿意的话软件包不会安装到您的漫游配置文件中(因为您的共享网络有配额,或者它会使域登录或注销变得缓慢),您可以将其放入本地应用程序数据中:

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

...以及复制%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:

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

命比纸薄 2024-11-20 04:47:00
$ npm install -g npm stable

帮我从 1.4.28 更新到 2.1.5

$ npm install -g npm stable

Worked for me to update from 1.4.28 to 2.1.5

心头的小情儿 2024-11-20 04:47:00

安装 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

白龙吟 2024-11-20 04:47:00

使用 npm 安装最新版本的 npm:

sudo npm install npm@latest

我在 Linux 上运行它,所以我不确定其他操作系统。

在 Linux 上,您还可以运行:

sudo apt-get update
sudo apt-get upgrade

这将告诉 apt-get 包管理器更新和升级所有包。

To install the latest version of npm using npm:

sudo npm install npm@latest

I run this on Linux so I am not sure about other operating systems.

On Linux you can also run:

sudo apt-get update
sudo apt-get upgrade

This will tell the apt-get package manager to update and upgrade all packages.

2024-11-20 04:47:00

我刚刚在一台新的 Windows 7 机器上安装了 Node.js,结果如下:

> node -v
v0.12.0
> npm -v
2.5.1

然后我执行了上述过程:

> npm install -g npm

并且它升级到了 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\

> npm -v
2.7.3

享受吧。 :-)

I just installed Node.js on a new Windows 7 machine, with the following results:

> node -v
v0.12.0
> npm -v
2.5.1

I then did the above described procedure:

> npm install -g npm

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\

> npm -v
2.7.3

Enjoy. :-)

远昼 2024-11-20 04:47:00

适用于 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

爱冒险 2024-11-20 04:47:00

只需以 root/管理员身份在终端中运行以下命令:

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

它在 Linux 上对我有用

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

青巷忧颜 2024-11-20 04:47:00

你可以尝试这个

npm install -g npm@latest

You could try this

npm install -g npm@latest

埋情葬爱 2024-11-20 04:47:00

我最近偶然发现了这篇文章: 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.

五里雾 2024-11-20 04:47:00

您可以使用以下命令升级到 npm 的最新版本:

npm install -g npm@latest

You can upgrade to the latest version of npm using:

npm install -g npm@latest
淡笑忘祈一世凡恋 2024-11-20 04:47:00

更新 npm :

npm install npm@{version} -g

将 npm 更新到最新版本:

npm install npm@latest -g

并检查版本:

npm -v

更新 Node js :

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

检查:

node -v

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
鱼忆七猫命九 2024-11-20 04:47:00

有时,从 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!

妥活 2024-11-20 04:47:00

刚刚听了 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 from 1.x to 2.x. Instead, use:
npm install npm -g

帥小哥 2024-11-20 04:47:00

只需使用这段代码

npm install update

Just with this code

npm install update
远昼 2024-11-20 04:47:00

当谈到 Linux 时,我建议使用包管理器更新节点:

Node 预装了 npm ,但是 manager 的更新比 Node 更频繁。运行 npm -v 查看您拥有的版本,然后 npm install npm@latest -g 安装最新的 npm 更新。如果您想确保 npm 正确更新,请再次运行 npm -v

要更新 NodeJS,您需要 npm 方便的 n 模块。运行此代码以清除 npm 的缓存,安装 n,并安装 Node 的最新稳定版本:

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

要安装最新版本,请使用 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. Run npm -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 of Node:

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

To install the latest release, use n latest. Alternatively, you can run n #.#.# to get a specific Node version.


When it comes to Windows/ macOS I suggest using Installers on Nodejs.org

The 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-run npm 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. --> *

剪不断理还乱 2024-11-20 04:47:00

对于nodejs应该卸载它并从nodejs.org下载您最喜欢的版本
对于 npm 在 cmd 中运行以下行:

npm i npm

for nodejs should uninstall it and download your favorite version from nodejs.org
for npm run below line in cmd:

npm i npm
二智少女猫性小仙女 2024-11-20 04:47:00

警告:如果您需要从旧版本更新 Node(在我的例子中 v4.6.0),最好从头开始重新安装 Nodejs(下载链接:https://nodejs.org),否则 npm 也会将自身更新到与新 Node 不兼容的版本(请参阅 此讨论)。

这是我使用 npm 更新 Node(在 Windows 上)后收到的错误消息

$ npm install -g npm stable
[ . . .]
$ npm 
C:\Users\me\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js:85
      let notifier = require('update-notifier')({pkg})
      ^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supporte
d outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:974:3

新安装 npm 再次工作后:

$ npm -v
6.5.0
$ node -v
v10.15.0

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

$ npm install -g npm stable
[ . . .]
$ npm 
C:\Users\me\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js:85
      let notifier = require('update-notifier')({pkg})
      ^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supporte
d outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:974:3

After new installation npm works again:

$ npm -v
6.5.0
$ node -v
v10.15.0
听风念你 2024-11-20 04:47:00

另外,如果您想更新到特定版本,请按照以下步骤操作:

sudo npm cache clean -f
sudo npm install -g n
sudo n <specific version>

Also if you want to update to a particular version, follow this:

sudo npm cache clean -f
sudo npm install -g n
sudo n <specific version>
薄荷梦 2024-11-20 04:47:00

只需在控制台上运行以下脚本:

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

这仅适用于 Linux 和 MAC

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

微凉 2024-11-20 04:46:59

如何更新 npm

使用以下命令:

npm update -g npm

请参阅<代码>更新命令:

npm update [-g] [...]

此命令将更新所有列出的软件包到最新版本(由标签配置指定),尊重 semver。

此外,请参阅有关 Node.js 和 npm 安装升级 npm

以下原始答案来自不再存在的旧常见问题解答,但它应该适用于 Linux 和 Mac:

如何更新 npm?

npm install -g npm

请注意,此命令将删除您当前版本的 npm。如果在 Mac 上,请确保使用 sudo npm install -g npm

您还可以通过执行不带任何参数的 npm update 来更新所有过时的本地软件包,或通过执行 npm update -g 来更新全局软件包。

有时,npm 的版本会进步,导致当前版本无法与您已安装的版本正确安装。 (考虑一下,更新命令中是否存在错误。)在这些情况下,您可以执行以下操作:

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

如何更新 Node.js

要更新 Node.js 本身,我建议您使用 nvm节点版本管理器)。以下是官方 npm 的引用 文档

我们强烈建议使用 Node 版本管理器(例如 nvm)来安装 Node.js 和 npm我们不建议使用 Node 安装程序,因为 Node 安装过程将 npm 安装在具有本地权限的目录中,并且当您全局运行 npm 包时可能会导致权限错误。

以下是使用 nvm 工具执行此操作的方法:

  • 要安装 Node 的最新版本,请执行以下操作:

    nvm 安装节点
    

    注意:这里 node 是最新版本的别名

  • 安装节点的特定版本:

    nvm 安装 22.2.0
    

更多详细信息可以在官方 nvm GitHub 页面的 "用法" 部分。

How to update npm

Use the following command:

npm update -g npm

See the documentation for the update command:

npm update [-g] [<pkg>...]

This command will update all the packages listed to the latest version (specified by the tag config), respecting semver.

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 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

How to update Node.js

To update Node.js itself, I recommend you use nvm (Node Version Manager). Here is the quote from the official npm documentation:

We strongly recommend using a Node version manager like nvm to install Node.js and npm. We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally.

Here is how you can do it using nvm tool:

  • To install the latest release of Node, do this:

    nvm install node
    

    (NOTE: here node is an alias for the latest version)

  • To install a specific version of node:

    nvm install 22.2.0
    

More details can be found on the official nvm GitHub page in the "Usage" section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文