npm install 报错 “npm ERR! code 1” 解决

郎家岭伯爵 2022年02月11日 977次浏览

问题

安装Node.js,执行npm install是报错npm ERR! code 1

报错详情:

npm ERR! code 1
npm ERR! path D:\Java\博客评论系统\halo-comment-joe2.0-qiu-master\node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node scripts/build.js
npm ERR! Building: D:\SoftWare\node\node.exe D:\Java\博客评论系统\halo-comment-joe2.0-qiu-master\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp verb cli [
npm ERR! gyp verb cli   'D:\\SoftWare\\node\\node.exe',
npm ERR! gyp verb cli   'D:\\Java\\博客评论系统\\halo-comment-joe2.0-qiu-master\\node_modules\\node-gyp\\bin\\node-gyp.js',
npm ERR! gyp verb cli   'rebuild',
npm ERR! gyp verb cli   '--verbose',
npm ERR! gyp verb cli   '--libsass_ext=',
npm ERR! gyp verb cli   '--libsass_cflags=',
npm ERR! gyp verb cli   '--libsass_ldflags=',
npm ERR! gyp verb cli   '--libsass_library='
npm ERR! gyp verb cli ]
npm ERR! gyp info using node-gyp@3.8.0
npm ERR! gyp info using node@16.14.0 | win32 | ia32
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 (D:\Java\博客评论系统\halo-comment-joe2.0-qiu-master\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\Java\博客评论系统\halo-comment-joe2.0-qiu-master\node_modules\which\which.js:68:19)

...

报错截图

解决

降低Node.js的版本,最好使用14.x版本。此方案参考自Stack Overflow

以往Node版本下载

注:

  • npm install命令下载的文件数量超过三万,因此如遇下载出错时请删除node_modules文件夹,然后多尝试几次命令。

npm install加载成功

Node.js版本

总结

  • Node.js版本过新会造成意料之外的错误,建议使用稳定版安装包。
  • 通常由于npm install命令需下载超过三万个文件,因此需多尝试几次下载。