在不同时期项目, NPM 软件包对 Node 的版本依赖不同,比如老版本 Sass 就不兼容最新的 NodeJS,为了让我们的本地开发环境能够安装指定版本运行 NPM 软件,可以使用 NVM(Node Version Manager)管理和切换 Node 版本。
安装教程参考自 https://github.com/nvm-sh/nvm#installing-and-updating
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
# 退出 terminal 或者执行 source ~/.bashrc 加载安装后的 NVM 配置信息
# 安装指定的 NodeJS 版本: 14
nvm install 14
# 切换到 NodeJS 14
nvm use 14同时,在项目根目录的
package.json文件中,添加engines配置,指定 node 和 npm 的版本号。{
"engines": {
"node": "^14.19.0",
"npm": "^6.14.17"
}
}