场景: 使用express-generator生成了项目框架,css引擎使用的sass(会用到node-sass)
执行npm i时报错, 错误信息中关键词:node-sass、node-gyp、python2
npm ERR! gyp info using node-gyp@3.8.0
npm ERR! gyp info using node@18.15.0 | win32 | x64
npm ERR! gyp verb command rebuild []
npm ERR! gyp verb command clean []
npm ERR! gyp verb clean removing "build" directory
npm ERR! gyp verb command configure []
npm ERR! gyp verb check python checking for Python executable "python2" in the PATH
npm ERR! gyp verb `which` failed Error: not found: python2
npm ERR! gyp verb `which` failed at getNotFoundError (C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed at F (C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed at E (C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed at C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed at C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed at C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed at FSReqCallback.oncomplete (node:fs:208:21)
npm ERR! gyp verb `which` failed python2 Error: not found: python2
npm ERR! gyp verb `which` failed at getNotFoundError (C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed at F (C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed at E (C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed at C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed at C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed at C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed at FSReqCallback.oncomplete (node:fs:208:21) {
npm ERR! gyp verb `which` failed code: 'ENOENT'
npm ERR! gyp verb `which` failed }
npm ERR! gyp verb check python checking for Python executable "python" in the PATH
npm ERR! gyp verb `which` succeeded python C:\Program Files\python\python.EXE
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Command failed: C:\Program Files\python\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack File "<string>", line 1
npm ERR! gyp ERR! stack import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
npm ERR! gyp ERR! stack SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
npm ERR! gyp ERR! stack
npm ERR! gyp ERR! stack at ChildProcess.exithandler (node:child_process:419:12)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! stack at maybeClose (node:internal/child_process:1091:16)
npm ERR! gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:302:5)
npm ERR! gyp ERR! System Windows_NT 10.0.22621
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\11529\\CodeRepos\\express-locallibrary-tutorial\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd C:\Users\11529\CodeRepos\express-locallibrary-tutorial\node_modules\node-sass
npm ERR! gyp ERR! node -v v18.15.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1
问题原因:
node版本和 node-sass版本不匹配 导致报错
GitHub(GitHub - sass/node-sass: Node.js bindings to libsass)上复制粘贴的版本对应关系:
Below is a quick guide for minimum and maximum supported versions of node-sass:
| NodeJS | Supported node-sass version | Node Module |
|---|---|---|
| Node 19 | 8.0+ | 111 |
| Node 18 | 8.0+ | 108 |
| Node 17 | 7.0+, <8.0 | 102 |
| Node 16 | 6.0+ | 93 |
| Node 15 | 5.0+, <7.0 | 88 |
| Node 14 | 4.14+ | 83 |
| Node 13 | 4.13+, <5.0 | 79 |
| Node 12 | 4.12+, <8.0 | 72 |
| Node 11 | 4.10+, <5.0 | 67 |
| Node 10 | 4.9+, <6.0 | 64 |
| Node 8 | 4.5.3+, <5.0 | 57 |
| Node <8 | <5.0 | <57 |
在上面case中,express-generator生成的项目package.json中使用了"node-sass-middleware": "0.11.0"
查看node-sass-middleware的package.json发现依赖的node-sass版本为^4.3.0也就是会安装4.x的最新版本,
从对照表可以看到node-sass 4.x依赖node版本为14,
而从上面错误信息可以看到,环境是node 18.15.0
解决方案: 切换到node 14
在我的case中,使用的是nvm来管理node版本,所以依次执行:
nvm install 14
nvm use 14
nvm i
安装成功~ 问题解决🙌🤞
ps:
看错误log被误导以为是python2或者node-gyp导致的问题,
所以还去切换了python的版本,去查看了node-gyp的依赖,
改了一圈环境发现啥用没有,
最后还用最开始的环境,只切换一下node版本问题就解决了