题意:
如何通过 Homebrew 安装特定版本的 Node?
问题背景:
For example I want to install either 7.9 or 7.10 but want to avoid Node 8 due to the webpack node-sass
breaking bug.
例如,我想安装 7.9 或 7.10 版本,但由于 webpack 的 node-sass 存在严重错误,我想避免安装 Node 8。
When I run brew search node
this is what I see:
当我运行 brew search node
时,我看到的是:
❯ brew search node leafnode llnode node ✔ node-build > [email protected] [email protected] node@4 node@6 nodebrew nodeenv nodenv caskroom/cask/node-profiler
If you meant "node" specifically: It was migrated from caskroom/cask to homebrew/core. You can access it again by running: brew tap homebrew/core
There is node which is checked (my current version is v7.4.0
then [email protected]
, [email protected]
, node@4
and node@6
?
显示的有 node(已选中,我当前的版本是 v7.4.0),然后是 [email protected]、[email protected]、node@4 和 node@6?
The reason I can't just fully upgrade to 8 is node-sass won't work in webpack.
我不能直接完全升级到 8 的原因是 node-sass 在 webpack 中将无法工作。
Just installed NVM and got this crazy error log:
刚安装了 NVM,却遇到了这个疯狂的错误日志:
=> nvm source string already in /Users/leongaban/.zshrc => Appending bash_completion source string to /Users/leongaban/.zshrc npm ERR! missing: is-path-cwd@^1.0.0, required by [email protected] npm ERR! missing: is-path-in-cwd@^1.0.0, required by [email protected] npm ERR! missing: p-map@^1.1.1, required by [email protected] npm ERR! missing: pify@^3.0.0, required by [email protected] npm ERR! missing: rimraf@^2.2.8, required by [email protected] npm ERR! missing: bluebird@^3.1.1, required by [email protected] npm ERR! missing: clone@^1.0.2, required by [email protected]
...
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:
If I'm reading this right, does this mean I can't use npm
to globally install packages anymore and have to use nvm
?
如果我理解得没错的话,这是不是说我现在不能再用 npm 全局安装包了,而必须使用 nvm
Update
I added the export lines to my .zshrc bash (I don't use bash_profile)
我将导出行添加到了我的 .zshrc
bash 文件中(我不使用 bash_profile
)
❯ nvm --version
0.33.2
问题解决:
There are two ways in the decision for using different version of Node. Second way is more convenient and practical by my opinion (also may be faster).
在决定使用不同版本的 Node 时有两种方法。在我看来,第二种方法更加方便实用(也可能更快)。
First way: 第一种方式
Install other Node version(for example 14) with using:
使用以下命令安装其他版本的 Node(例如 14):
brew install |
brew unlink |
brew link |
---|---|---|
brew install - github | brew unlink - github | brew link - github |
brew install node@14
brew unlink node
brew link node@14
node -v
PS You may use brew link
with flag --overwrite
, for example:
PS:你可以使用带 --overwrite
标志的 brew link
命令,例如:
brew link --overwrite node@14
PS2 Why unlink
and then link
again?
PS2:为什么要先执行 unlink
然后再执行 link
?
Documentation: 文档
Remove symlinks for formula from Homebrew's prefix. This can be useful for temporarily disabling a formula:
brew unlink formula && commands && brew link formula
In other words: 换句话说:
if you have both node and node@14 installed, where node is other version(..,15 or 16), so, for set active version 14:
如果你同时安装了 node 和 node@14(其中 node 代表其他版本,比如 15 或 16),那么,为了设置激活版本为 14:
you must unlink node |
and then link to new installed version 14 |
---|---|
brew unlink node |
brew link node@14 |
PS3
Brew typically supports only major versions of software packages, meaning it provides updates like node@18 rather than specific minor versions such as [email protected].
Homebrew 通常只支持软件包的主要版本,这意味着它提供的是像 node@18 这样的更新,而不是特定的次要版本,如 [email protected]。
This approach simplifies package management by focusing on core stable releases without maintaining every minor version.
这种方法通过专注于核心稳定版本而不维护每个次要版本来简化包管理。
For more precise version control, tools like nvm(Second way) are recommended, allowing to install specific minor or patch versions as needed.
对于更精确的版本控制,建议使用 nvm(第二种方法)等工具,允许根据需要安装特定的次要版本或补丁版本。
Second way: 第二种方式
Install Node Version Manager(nvm) and select Node version:
安装 Node 版本管理器(nvm)并选择 Node 版本:
brew install nvm
mkdir ~/.nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$(brew --prefix)/opt/nvm/nvm.sh" ] && . "$(brew --prefix)/opt/nvm/nvm.sh" # This loads nvm
[ -s "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" ] && . "$(brew --prefix)/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
nvm install 14
nvm use 14
nvm list