npm 安装时不同的节点版本冲突

different node version conflicts while npm install

每次我想做的时候都会遇到这个问题npx create-react-app my_app

yarn add v1.15.2
[1/4]   Resolving packages...
[2/4]   Fetching packages...
error eslint@6.2.1: The engine "node" is incompatible with this module. 
Expected version "^8.10.0 || ^10.13.0 || >=11.10.1". Got "8.3.0"

但是当我使用 nvm 和 bash 检查我的版本时,这就是我得到的

$ node -v
v8.16.1
$ npm -v
6.4.1
$ nvm ls
nvm ls
->      v8.16.1
        v9.10.1
        v12.0.0
         system
default -> node (-> v12.0.0)
node -> stable (-> v12.0.0) (default)
stable -> 12.0 (-> v12.0.0) (default)
iojs -> N/A (default)
lts/* -> lts/dubnium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.1
lts/dubnium -> v10.16.3 (-> N/A)

我真的不明白为什么它在执行 npx 命令时说我得到了节点 8.3.0 而那个版本无处出现

可能是因为我前段时间用brew安装过node,可能和nvm设置冲突?


当我分别发布 npm install -g create-react-appcreate-react-app my-app 时它有效,因为这是 facebook 推荐的旧节点版本

$ npm install -g create-react-app
+ create-react-app@3.1.1
added 91 packages from 45 contributors in 4.491s

$ create-react-app trainin
yarn add v1.15.2
[1/4]   Resolving packages...
[2/4]   Fetching packages...
[3/4]   Linking dependencies...
warning "react-scripts > @typescript-eslint/eslint-plugin@1.13.0" has incorrect peer dependency "eslint@^5.0.0".
warning "react-scripts > @typescript-eslint/parser@1.13.0" has incorrect peer dependency "eslint@^5.0.0".
warning "react-scripts > @typescript-eslint/eslint-plugin > tsutils@3.17.1" has unmet peer dependency "typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta".
[4/4]   Building fresh packages...
success Saved lockfile.
success Saved 7 new dependencies.
info Direct dependencies
├─ react-dom@16.9.0
├─ react-scripts@3.1.1
└─ react@16.9.0
info All dependencies
├─ eslint-config-react-app@5.0.1
├─ react-dev-utils@9.0.3
├─ react-dom@16.9.0
├─ react-error-overlay@6.0.1
├─ react-scripts@3.1.1
├─ react@16.9.0
└─ scheduler@0.15.0
✨  Done in 15.45s.

最好卸载brew安装的版本,并设置默认兼容npx的版本

您可以使用 n 包,它允许您在多个节点版本之间切换