1. 为何要Node版本管理
为何要进行node版本管理,更多是因为旧项目的各个依赖之前互相牵扯,所依赖的版本特性也不一致,而解决这些问题往往需要进行node降级处理。
最近刚好调到新的项目组,需要维护一个老旧的商城项目,对应某些依赖要node12~14才能正常运行,而我常用16+,项目依赖的版本不一致,没办法只能降级...
2. nvm的问题
之前一直使用nvm管理node,在Windows系统还好,但最近都在使用mac,且折腾过shell,所以在切换node的时候就遇到了各种奇怪的问题,还要配置.nvmrc;然后又用了n,但n是不跨平台的,万一到时用回Windows,用习惯了又要换。
另一个问题是,如果你同时使用csh、zsh、bash等shell,适配$PATH会有多麻烦~
3. 为啥用fnm
fnm的特点,Github上的介绍是使用rust编写,具备使用简单、跨平台、速度快等优势,完美适配个人需求,所以果断切换到fnm。
4. fnm的安装使用
接下来理所当然开始按照说明安装。
然后理所当然的过程依然有点坎坷...
0. 安装流程
首先是按着官网描述开始安装,因为是mac,所以
使用curl
curl -fsSL https://fnm.vercel.app/install | bash
用curl安装不知道为啥一直报connnect error等错误,于是改用brew
用mac不是很熟练,一直不是很清楚这两个的区别...
使用brew
brew install fnm
然后就是遇到的问题,下面举其中两个问题及解决方案:
1. Mac brew安装报错“no bottle available!"
需要按顺序执行以下命令
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
brew update
接下来才能正常使用brew安装。
2. Mac brew安装报错"fatal: not in a git directory"
错误信息大概如下
fatal: not in a git directory
Error: Command failed with exit 128: git
解决方法就是首先使用brew -v查看,可以发现有两个fatal(致命错误)提示:
fatal:xxxxxxxxxx
TO add xxxx
fatal:xxxxxxxxxx
TO add xxxx
而解决方法就在提示中,根据提示,分别执行如下两个命令:
git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-core
git config --global --add safe.directory /opt/homebrew/Library/Taps/homebrew/homebrew-cask
完成之后,再次执行brew -v查看,fatal提示已经没有了。
接下来再次执行brew install fnm安装。
安装成功后,shell验证
% fnm -h
fnm 1.33.1
A fast and simple Node.js manager
USAGE:
fnm [OPTIONS] <SUBCOMMAND>
OPTIONS:
--arch <ARCH>
Override the architecture of the installed Node binary. Defaults to arch of fnm binary
[env: FNM_ARCH]
--fnm-dir <BASE_DIR>
The root directory of fnm installations [env: FNM_DIR]
-h, --help
Print help information
--log-level <LOG_LEVEL>
The log level of fnm commands [env: FNM_LOGLEVEL] [default: info] [possible values:
quiet, info, all, error]
--node-dist-mirror <NODE_DIST_MIRROR>
https://nodejs.org/dist/ mirror [env: FNM_NODE_DIST_MIRROR] [default:
https://nodejs.org/dist]
-V, --version
Print version information
--version-file-strategy <VERSION_FILE_STRATEGY>
A strategy for how to resolve the Node version. Used whenever `fnm use` or `fnm install`
is called without a version, or when `--use-on-cd` is configured on evaluation [env:
FNM_VERSION_FILE_STRATEGY] [default: local] [possible values: local, recursive]
SUBCOMMANDS:
alias Alias a version to a common name
completions Print shell completions to stdout
current Print the current Node.js version
default Set a version as the default version
env Print and set up required environment variables for fnm
exec Run a command within fnm context
help Print this message or the help of the given subcommand(s)
install Install a new Node.js version
list List all locally installed Node.js versions [aliases: ls]
list-remote List all remote Node.js versions [aliases: ls-remote]
unalias Remove an alias definition
uninstall Uninstall a Node.js version
use Change Node.js version
出现以上信息表明已经成功安装。
5. shell配置
如果不配置shell,使用fnm命令会出现如下的问题
error: `fnm env` was not applied in this context.
Can't find fnm's environment variables
所以需要配置shell,官方也有相关说明
举个配置bash的例子
-
打开
.bashrccode ~/.bashrc -
底部添加
eval "$(fnm env --use-on-cd)" -
执行source命令
source ~/.bashrc -
查看验证
ZSH
CSH
JS调试终端
查看版本
总结
本文记录了node版本管理工具从nvm到n再到fnm的使用过程,中间也花费了一定时间折腾,但工具始终只是工具,在仅仅是使用的前提下,不应该花费太多时间精力在此,够用足以。