Node版本不兼容致NPM无法安装依赖,NVM切换后问题未解决求助
问题描述
我有一个React项目,安装任何包时都会出现Node.js版本不兼容的警告,执行命令sudo npm i --save @fortawesome/fontawesome-svg-core时输出如下错误:
sudo npm i --save @fortawesome/fontawesome-svg-core npm WARN cli npm v10.2.5 does not support Node.js v19.3.0. This version of npm supports the following node versions: `^18.17.0 || >=20.5.0`. You can find the latest version at https://nodejs.org/. npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@typescript-eslint/eslint-plugin@7.10.0', npm WARN EBADENGINE required: { node: '^18.18.0 || >=20.0.0' }, npm WARN EBADENGINE current: { node: 'v19.3.0', npm: '10.2.5' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@typescript-eslint/parser@7.10.0', npm WARN EBADENGINE required: { node: '^18.18.0 || >=20.0.0' }, npm WARN EBADENGINE current: { node: 'v19.3.0', npm: '10.2.5' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@typescript-eslint/scope-manager@7.10.0', npm WARN EBADENGINE required: { node: '^18.18.0 || >=20.0.0' }, npm WARN EBADENGINE current: { node: 'v19.3.0', npm: '10.2.5' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@typescript-eslint/type-utils@7.10.0', npm WARN EBADENGINE required: { node: '^18.18.0 || >=20.0.0' }, npm WARN EBADENGINE current: { node: 'v19.3.0', npm: '10.2.5' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@typescript-eslint/types@7.10.0', npm WARN EBADENGINE required: { node: '^18.18.0 || >=20.0.0' }, npm WARN EBADENGINE current: { node: 'v19.3.0', npm: '10.2.5' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@typescript-eslint/typescript-estree@7.10.0', npm WARN EBADENGINE required: { node: '^18.18.0 || >=20.0.0' }, npm WARN EBADENGINE current: { node: 'v19.3.0', npm: '10.2.5' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@typescript-eslint/utils@7.10.0', npm WARN EBADENGINE required: { node: '^18.18.0 || >=20.0.0' }, npm WARN EBADENGINE current: { node: 'v19.3.0', npm: '10.2.5' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '@typescript-eslint/visitor-keys@7.10.0', npm WARN EBADENGINE required: { node: '^18.18.0 || >=20.0.0' }, npm WARN EBADENGINE current: { node: 'v19.3.0', npm: '10.2.5' } npm WARN EBADENGINE } npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: 'vite@5.2.11', npm WARN EBADENGINE required: { node: '^18.0.0 || >=20.0.0' }, npm WARN EBADENGINE current: { node: 'v19.3.0', npm: '10.2.5' } npm WARN EBADENGINE } up to date, audited 296 packages in 3s 66 packages are looking for funding run `npm fund` for details found 0 vulnerabilities
尝试用nvm切换版本,执行nvm ls输出如下:
v18.17.0 -> v18.18.0 system default -> lts/* (-> N/A) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v18.18.0) (default) stable -> 18.18 (-> v18.18.0) (default) lts/* -> lts/iron (-> N/A) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.12 (-> N/A) lts/fermium -> v14.21.3 (-> N/A) lts/gallium -> v16.20.2 (-> N/A) lts/hydrogen -> v18.20.3 (-> N/A) lts/iron -> v20.14.0 (-> N/A)
已经移除了之前安装的v21.4.0和v20.10.0版本,但npm还是提示版本不兼容。项目的package.json内容如下:
{ "name": "controls", "private": true, "version": "0.0.0", "type": "module", "scripts": { "start": "tsc && vite", "dev": "vite", "build": "tsc && vite build", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview" }, "dependencies": { "@fortawesome/fontawesome-svg-core": "^6.5.2", "@vitejs/plugin-react": "^4.3.0", "react": "^18.2.0", "react-dom": "^18.2.0" }, "devDependencies": { "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", "@typescript-eslint/eslint-plugin": "^7.2.0", "@typescript-eslint/parser": "^7.2.0", "@vitejs/plugin-react-swc": "^3.5.0", "autoprefixer": "^10.4.19", "eslint": "^8.57.0", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-refresh": "^0.4.6", "postcss": "^8.4.38", "tailwindcss": "^3.4.3", "typescript": "^5.2.2", "vite": "^5.2.0" } }
解决方法
1. 不要用sudo执行npm命令
你用sudo执行npm时,会跳过nvm的环境隔离,直接调用系统级的Node版本(v19.3.0)。直接执行以下命令即可:
npm i --save @fortawesome/fontawesome-svg-core
2. 确认nvm切换的版本在当前终端生效
先检查当前终端的Node版本:
node -v
如果输出不是v18.18.0,重新执行切换命令并验证:
nvm use 18.18.0 node -v
3. 设置默认Node版本
避免每次打开终端都手动切换,设置v18.18.0为默认版本:
nvm alias default 18.18.0
重启终端后,执行node -v确认版本是否正确。
4. 清理缓存并重新安装依赖
如果版本切换正确但仍有警告,清理缓存并重装依赖:
npm cache clean --force rm -rf node_modules package-lock.json npm install
5. 升级到更稳定的LTS版本
从nvm输出看,v18的最新LTS是v18.20.3,v20的是v20.14.0,安装并切换到这些版本:
# 安装并使用v18最新LTS nvm install lts/hydrogen nvm use lts/hydrogen # 或者安装并使用v20最新LTS nvm install lts/iron nvm use lts/iron
6. 临时忽略引擎检查(不推荐)
如果上述方法无效,可临时跳过引擎检查,但可能引发兼容性问题:
npm i --save @fortawesome/fontawesome-svg-core --force
内容的提问来源于stack exchange,提问作者Simple Fellow
相关产品推荐
相关产品推荐

