简体   繁体   English

在 Mac OS 上升级 Node.js 到最新版本

[英]Upgrade Node.js to the latest version on Mac OS

Currently I am using Node.js v0.6.16 on Mac OS X 10.7.4.目前我在 Mac OS X 10.7.4 上使用 Node.js v0.6.16。 Now I want to upgrade it to the latest Node.js v0.8.1.现在我想将它升级到最新的 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.但是从 nodejs.org 下载并安装最新的 package 文件后,当我在终端中键入“node -v”时,我发现系统仍在使用 v0.6.16 而不是 v0.8.1。 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顺便说一句,我知道 nvm 可以帮助管理 nodejs package

https://github.com/creationix/nvm/ https://github.com/creationix/nvm/

Is there any way to upgrade the Node.js without using it? Node.js有什么办法不用它就可以升级吗?

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.我已经用谷歌搜索了这个问题,但在我看来,对于最新的 Node.js,这个问题没有非常明确的答案。

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 the terminal):以下是我在没有任何其他要求(如 brew 等)的情况下成功从v0.8.18升级到v0.10.20的方法(在终端中键入这些命令):

  1. sudo npm cache clean -f (force) clear you npm cache sudo npm cache clean -f (强制)清除你的 npm 缓存
  2. sudo npm install -gn install n (this might take a while) sudo npm install -gn install n (这可能需要一段时间)
  3. sudo n stable upgrade to the current stable version sudo n stable升级到当前稳定版本

Note that sudo might prompt your password.请注意, sudo可能会提示您输入密码。

Additional note regarding step 3: stable can be exchanged for latest , lts (long term support) or any specific version number such as 0.10.20 .关于第 3 步的附加说明: stable可以换成latestlts (长期支持)或任何特定版本号,例如0.10.20

If the version number doesn't show up when typing node -v , you might have to reboot.如果键入node -v时没有显示版本号,您可能需要重新启动。

These instructions are found here as well: davidwalsh.name/upgrade-nodejs这些说明也可以在这里找到: davidwalsh.name/upgrade-nodejs
More info about the n package found here: npmjs.com/package/n有关n包的更多信息,请参见: npmjs.com/package/n
More info about Node.js' release schedule: github.com/nodejs/Release有关 Node.js 发布时间表的更多信息: github.com/nodejs/Release

If you initially installed Node.js with Homebrew , run:如果您最初使用Homebrew安装了Node.js ,请运行:

brew update
brew upgrade node
npm install -g npm

Or as a one-liner:或作为单行:

brew update && brew upgrade node && npm install -g npm

A convenient way to change versions is to use nvm :更改版本的一种便捷方法是使用nvm

brew install nvm

To install the latest version of Node.js with nvm:使用 nvm 安装最新版本的 Node.js:

nvm install node

If you installed via a package, then download the latest version from nodejs.org .如果您通过软件包安装,请从nodejs.org下载最新版本。 See Installing Node.js and updating npm .请参阅安装 Node.js 和更新 npm

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.因为在搜索如何在 mac 上升级 nodejs 时,这似乎是谷歌的首要任务,我将为将来出现的任何人提供我的提示,尽管它已经很老了。

Upgrading via NPM通过 NPM 升级
You can use the method described by @Mathias above or choose the following simpler method via the terminal.您可以使用上面@Mathias 描述的方法,也可以通过终端选择以下更简单的方法。

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.您的 nodejs 应该已经升级到最新版本。 If you wish to upgrade to a specific one say v0.8.19 then instead of如果您想升级到特定版本,请说 v0.8.19,而不是

sudo n stable

use利用

sudo n 0.8.19

EDIT Avoid using sudo unless you need to.编辑除非需要,否则避免使用 sudo。 Refer to comment by Steve in the comments请参阅评论中史蒂夫的评论

Go to http://nodejs.org and download and run the installer.转到http://nodejs.org并下载并运行安装程序。 It works now - for me at least.它现在有效 - 至少对我来说。

You could install nvm and have multiple versions of Node.js installed.您可以安装nvm并安装多个版本的 Node.js。

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

and then run:然后运行:

nvm install 0.8.22  #(or whatever version of Node.js you want)

you can see what versions you have installed with :你可以看到你安装了哪些版本:

nvm list

and you can change between versions with:您可以在版本之间进行更改:

nvm use 0.8.22

The great thing about using NVM is that you can test different versions alongside one another.使用 NVM 的好处在于您可以同时测试不同的版本。 If different apps require different versions of Node.js, you can run them both.如果不同的应用程序需要不同版本的 Node.js,您可以同时运行它们。

I use Node version manager (called n) for it.我为此使用 Node 版本管理器(称为 n)。

npm install -g n

then然后

n latest

OR或者

n stable

Simply go to node JS Website and install the latest version.只需转到 node JS 网站并安装最新版本。

Do install latest version instead of the recommended stable version.请安装最新版本而不是推荐的稳定版本。 It will give you freedom to use latest ES6 Features on node.它将让您自由地在节点上使用最新的 ES6 功能。

Can be Found here Node JS .可以在这里找到Node JS

also to update npm , you will have to use this command.还要更新npm ,您必须使用此命令。

sudo npm i -g npm@latest

All your projects will work fine.您的所有项目都会正常工作。

Update: 2020 another good option is to use nvm for node which can then support multiple versions.更新:2020 年另一个不错的选择是将nvm用于节点,然后可以支持多个版本。 use nvm install --lts to always be able to update to latest node version use nvm ls-remote command to to check new versions of node.使用nvm install --lts始终能够更新到最新的节点版本 使用nvm ls-remote命令检查新版本的节点。


Other option for mac :: brew update && brew install node && npm -g npm mac 的其他选项 :: brew update && brew install node && npm -g npm

Upgrade the version of node without installing any package , not even nvm itself:不安装任何包的情况下升级 node 的版本,甚至不安装 nvm 本身:

sudo npx n stable

Explanations :说明
This approach is similar to Johan Dettmar 's answer.这种方法类似于Johan Dettmar的回答。 The only difference is here the package n is not installed glabally in the local machine.唯一的区别是这里的包n没有安装在本地机器上。

On macOS the homebrew recommended way is to run在 macOS 上,自制软件推荐的方法是运行

brew install node
npm install -g npm@latest

终端命令截图

我可以使用以下命令升级节点

nvm install node --reinstall-packages-from=node

sadly, n doesn't worked for me.可悲的是, n对我不起作用。 I use node version manager or nvm and it works like a charm.我使用node version manager or nvm ,它就像一个魅力。 heres the link on how to install nvm : https://github.com/creationix/nvm#installation这是有关如何安装nvm的链接: https ://github.com/creationix/nvm#installation

  • nvm i 8.11.2 upgrade to latest LTS nvm i 8.11.2升级到最新的 LTS
  • nvm use 8.11.2 use it nvm use 8.11.2使用它
  • node -v check your latest version node -v检查你的最新版本

Go to the website nodejs.org and download the latest pkg then install.转到网站nodejs.org并下载最新的 pkg 然后安装。 it works for me这个对我有用

I used brew to upgrade my node.我使用 brew 来升级我的节点。 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.它已经安装,但它位于/usr/local/Cellar/node/5.5.0并且/usr/local/bin/node中有一个默认节点,这让我很困扰。 I don't want to make soft link because I don't really know how brew is organized.我不想做软链接,因为我真的不知道 brew 是如何组织的。 So I download the pkg file, installed and I got this info:所以我下载了pkg文件,安装了,我得到了这个信息:

Node.js was installed at Node.js安装在

/usr/local/bin/node

npm was installed at npm安装在

/usr/local/bin/npm

Make sure that /usr/local/bin is in your $PATH.确保/usr/local/bin在您的 $PATH 中。

Now the upgrade is completed现在升级完成

There are five different ways (and counting?) to update Node.js on Mac:在 Mac 上更新 Node.js 有五种不同的方法(并且还在计数?):

  1. Install the newer binary by downloading from nodejs.org通过从 nodejs.org 下载安装较新的二进制文件

  2. Update Node.js through Homebrew通过 Homebrew 更新 Node.js

    Command: brew update && brew upgrade node命令: brew update && brew upgrade node

  3. Update Node.js using Node Version Manager (NVM)使用节点版本管理器 (NVM) 更新 Node.js

    Command: nvm install {version} && nvm use {version}命令: nvm install {version} && nvm use {version}

  4. Update Node.js using n package manager使用 n 包管理器更新 Node.js

    Command: sudo n latest命令: sudo n latest

  5. Update Node.js through MacPorts通过 MacPorts 更新 Node.js

    Command (same version update): sudo port selfupdate && sudo port upgrade {version}命令(同版本更新): sudo port selfupdate && sudo port upgrade {version}

If you are still using older MacOS version, it may be better to use lightweight, special-purpose package manager like n or nvm .如果您仍在使用旧的 MacOS 版本,最好使用轻量级的专用包管理器,例如nnvm You can refer to this sample use case for updating Node.js on old Mac (High Sierra).您可以参考此示例用例在旧 Mac (High Sierra) 上更新 Node.js。

你可以运行,但你不能隐藏......最后你还是会使用NVM

You can just go to nodejs.org and download the newest package.你可以去 nodejs.org 下载最新的包。 It will update appropriately for you.它将为您适当更新。 NPM will be updated as well. NPM 也将更新。

我认为使用最新版本的 Node.js 最简单的方法是在网站https://nodejs.org/en/download/current/中获取最新的 Node.js pkg 文件,如果您想使用不同版本的 Node .js 你可以使用 nvm 或 n 来管理它。

These 2 methods I tried are not working:我试过的这两种方法都不起作用:

  1. Use npm使用 npm

sudo npm cache clean -f

sudo npm install -gn

sudo n stable

  1. Manual install node from official website ( https://nodejs.org/en/ )从官方网站手动安装节点( https://nodejs.org/en/

After trying, node -v still shows the old version of node.尝试后, node -v仍然显示旧版本的node。


Below method works for me:以下方法对我有用:

Step 1: Install nvm (for more details: https://github.com/creationix/nvm#installation )第 1 步:安装 nvm(更多详情: https ://github.com/creationix/nvm#installation)

Open terminal and type this command:打开终端并输入以下命令:

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

Close terminal and reopen it.关闭终端并重新打开它。

Type this command to check if nvm is installed:键入此命令以检查是否安装了 nvm:

command -v nvm

在此处输入图像描述

Step 2: To download, compile, and install the latest release of node, type this:第 2 步:要下载、编译和安装最新版本的节点,请输入:

nvm install node ("node" is an alias for the latest version) nvm install node (“node”是最新版本的别名)

To check if node gets the latest version (v10.11.0).检查节点是否获取最新版本(v10.11.0)。

在此处输入图像描述

Installing the latest node also installs the latest npm.安装最新的节点也会安装最新的 npm。

Check if npm gets the latest version (6.4.1).检查 npm 是否获取最新版本(6.4.1)。

在此处输入图像描述

sudo npm install -g n

and then接着

sudo n latest for linux/mac users

For Windows please reinstall node.对于 Windows,请重新安装节点。

Nvm Nvm is a script-based node version manager. Nvm Nvm 是一个基于脚本的节点版本管理器。 You can install it easily with a curl and bash one-liner as described in the documentation.如文档中所述,您可以使用 curl 和 bash 单线轻松安装它。 It's also available on Homebrew.它也可以在 Homebrew 上找到。

Assuming you have successfully installed nvm.假设您已成功安装 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.最后一个选项将所有全局 npm 包安装到您的新版本。 This way packages like mocha and node-inspector keep working.这样像 mocha 和 node-inspector 这样的包可以继续工作。

NN is an npm-based node version manager. NN 是一个基于 npm 的节点版本管理器。 You can install it by installing first some version of node and then running npm install -gn .您可以通过首先安装某个版本的节点然后运行npm install -gn来安装它。

Assuming you have successfully installed n.假设您已成功安装 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. Homebrew Homebrew 是两个流行的 Mac 包管理器之一。 Assuming you have previously installed node with brew install node.假设您之前使用 brew install node 安装了节点。 You can get up-to-date with formulae and upgrade to the latest Node.js version with the following.您可以使用以下公式获取最新的公式并升级到最新的 Node.js 版本。

1 brew update
2 brew upgrade node

MacPorts MacPorts is the another package manager for Mac. MacPorts MacPorts 是 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.然后它将安装最新版本的 Node.js。 This works even if you have previous version of the package installed.即使您安装了以前版本的软件包,这也有效。

1 sudo port selfupdate
2 sudo port install nodejs-devel

for latest release: nvm install node对于最新版本: nvm install node

specific version: nvm install 6.14.4具体版本: nvm install 6.14.4

https://github.com/creationix/nvm https://github.com/creationix/nvm

Easy nad Safe Steps简单和安全的步骤

Step 1: Install NVM第 1 步:安装 NVM

brew install nvm

Step 2: Create a directory for NVM第 2 步:为 NVM 创建目录

mkdir ~/.nvm/

Step 3: Configure your environmental variables第 3 步:配置环境变量

nano ~/.bash_profile

PASTE BELOW CODE粘贴以下代码

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

source ~/.bash_profile

Step 4: Double check your work第 4 步:仔细检查您的工作

nvm ls

Step 5: Install Node第 5 步:安装节点

nvm install 9.x.x

Step6: Upgrade第六步:升级

nvm ls-remote

   v10.16.2   (LTS: Dubnium)
   v10.16.3   (Latest LTS: Dubnium) ..........

nvm install v10.16.3 nvm 安装 v10.16.3

Troubleshooting故障排除

Error Example #1
rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh

https://www.chrisjmendez.com/2018/02/07/install/ https://www.chrisjmendez.com/2018/02/07/install/

Now this works for me:现在这对我有用:

  1. sudo npm install -gn须藤 npm 安装 -gn
  2. sudo n latest sudo n 最新

Happy code!快乐的代码!

Use nvm to upgrade node as per the project requirement..根据项目要求使用 nvm 升级节点..

install nvm through homebrew.. brew update brew install nvm mkdir ~/.nvm nano ~/.bash_profile通过 homebrew 安装 nvm.. 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 :在您的 .bash_profile 文件中(根据您的 shell,您可能正在使用其他文件),添加以下内容:

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

source ~/.bash_profile echo $NVM_DIR源 ~/.bash_profile echo $NVM_DIR

You can directly use curl to upgrade node to the latest version.您可以直接使用 curl 将 node 升级到最新版本。 Run the following command:运行以下命令:

curl "https://nodejs.org/dist/latest/node-${VERSION:-$(wget -qO- https://nodejs.org/dist/latest/ | sed -nE 's|.*>node-(.*)\.pkg</a>.*|\1|p')}.pkg" > "$HOME/Downloads/node-latest.pkg" && sudo installer -store -pkg "$HOME/Downloads/node-latest.pkg" -target "/"

Reference: https://nodejs.org/en/download/package-manager/#macos参考: https ://nodejs.org/en/download/package-manager/#macos

Pretty Simple.很简单。

  sudo npm i -g n

Then you can specify the version you want.然后你可以指定你想要的版本。

sudo n 12.8.0 

Cheers!!干杯!!

I had the same problem.我有同样的问题。 This is what worked for me because I downloaded and installed node.js globally from the node.js website.这对我有用,因为我从 node.js 网站全局下载并安装了 node.js。

What I did was Give NVM (Node Version Manager) a try.我所做的是尝试 NVM(节点版本管理器)。 Please do the commands in the following order in your terminal请在终端中按以下顺序执行命令

  1. curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash重击

  2. command -v nvm命令 -v nvm

  3. nvm install node nvm 安装节点

  4. node -v (to confirm the update) node -v(确认更新)

This is just to add some info for people who didn't have Node installed with Homebrew but getting that very error when trying to install packages with npm on Mac OS X .这只是为那些没有使用Homebrew安装 Node 但在Mac OS X上尝试使用npm安装软件包时遇到错误的人添加一些信息。

I found this good article explaining how to completely remove Node whichever the way you originally installed it.我发现这篇 很好的文章解释了如何以最初安装的方式完全删除Node

After node , npm and n were completely removed from my machine, I just reinstalled Node.js using the official .pckg installer from Node website and everything just went back to normal.nodenpmn从我的机器上完全删除之后,我刚刚使用Node 网站上的官方.pckg 安装程序重新安装了Node.js ,一切都恢复了正常。

Hope this helps out someone.希望这可以帮助某人。

First install nvm with this command:首先使用以下命令安装nvm

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

credirts to @Saviah Kao归功于@Saviah Kao

Then install node:然后安装节点:

nvm install node --reinstall-packages-from=node

credits to @Elad归功于@Elad

An easy way is go straight to the website https://nodejs.org/en/download/ Click download the latest version and install pkg file.一个简单的方法是直接访问网站https://nodejs.org/en/download/点击下载最新版本并安装 pkg 文件。 After that you will get the latest node and npm version之后,您将获得最新的节点和 npm 版本

I'm using a Mac Mini M1.我正在使用 Mac Mini M1。 I just removed my very old packages of node.js and npm and decided to install them using homebrew.我刚刚删除了我非常旧的 node.js 和 npm 软件包,并决定使用自制软件安装它们。 I found out that now brew install node also installs npm.我发现现在brew install node也安装了 npm。 FYI供参考

I was able to update in ~20 seconds with just one line of code只需一行代码,我就能在大约 20 秒内完成更新

sudo n latest

Other commands weren't working for me, but this one worked.其他命令对我不起作用,但这个有效。 Hope it helps somebody.希望它可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM