如何解决npm安装错误“npm ERR!code 1”

71

我正在尝试安装Gulp.js,但当我输入npm install时,出现了以下问题:

npm ERR! code 1
npm ERR! path D:\www\wegrow\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: C:\Program Files (x86)\nodejs\node.exe D:\www\wegrow\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   'C:\\Program Files (x86)\\nodejs\\node.exe',
npm ERR! gyp verb cli   'D:\\www\\wegrow\\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.1.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:\www\wegrow\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\www\wegrow\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\www\wegrow\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:194:21)
npm ERR! gyp verb `which` failed  python2 Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\www\wegrow\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\www\wegrow\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\www\wegrow\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\www\wegrow\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:194: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:\Python39\python.EXE
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Command failed: C:\Python39\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: invalid syntax
npm ERR! gyp ERR! stack
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:326:12)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:365:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1067:16)
npm ERR! gyp ERR! stack     at Socket.<anonymous> (node:internal/child_process:453:11)
npm ERR! gyp ERR! stack     at Socket.emit (node:events:365:28)
npm ERR! gyp ERR! stack     at Pipe.<anonymous> (node:net:661:12)
npm ERR! gyp ERR! System Windows_NT 10.0.19042
npm ERR! gyp ERR! command "C:\\Program Files (x86)\\nodejs\\node.exe" "D:\\www\\wegrow\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd D:\www\wegrow\node_modules\node-sass
npm ERR! gyp ERR! node -v v16.1.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

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\wbalu\AppData\Local\npm-cache\_logs\2021-05-05T10_38_29_779Z-debug.log

我已经格式化了我的电脑。在这之前一切都正常。现在当我尝试安装它时,就会弹出这个窗口。我已经尝试重新安装Node.js,但没有帮助。我该怎么做?


4
你有一个用Python2编写的程序,现在想要在Python3中运行它,这两种语言虽然相关但是不同。这与node.js无关,也与Git无关。 - torek
1
我已经下载了Python27并将其添加到了PATH中,但它仍然不起作用 :/ 出现了相同的错误。我不知道我是否做错了什么。 - Wojciech Bałucki
20个回答

66

如果您的Node.js版本非常新,请尝试降级。稳定版14.16.1可行。


2
这对我来说绝对是个问题。我经历了一堆麻烦,降级 Node 是解决问题的方法。14.x 目前是大多数人推荐的版本。Yarn 强制升级到 16.x。在 Homebrew 中,你可以使用 brew install node@14 brew unlink node brew link node@14 来回到 14 或在它们之间切换。 - Jake
2
感谢@AlejandroS,这在我的苹果硅情况下起作用了,使用@Jake展示的brew,但是我没有使用unlink/link,而是简单地为npm、npx和node中的每一个创建别名alias npm14='/opt/homebrew/Cellar/node@14/*/bin/npm',并且使用这些别名可以正常工作,我相信我还没有解决全局node_modules路径的问题。 - jimmont
下载链接:https://nodejs.org/dist/v14.16.1/ - TECNO
3
这对我也解决了问题。顺便说一下,我强烈推荐使用Node版本管理器轻松安装/切换节点版本。这是一个简单明了的设置指南:https://www.freecodecamp.org/news/node-version-manager-nvm-install-guide/。 - D Story
哇塞,谢谢你,我搜索了2个小时才找到这个答案。我之前使用的是Node 20.4,降级到18.16后终于成功了。 - starryn1ght
显示剩余2条评论

42

降级Node.js是一个选项,但我不喜欢只因为一个包抛出错误就降级Node.js的想法。在我看来,更好的方法是找出导致错误的包,然后进行修复。

快速解决方案

npm ERR! code 1后面的行中,您应该能够看到npm ERR! path...。在我的情况下,路径表明node-sass npm包存在问题:

npm ERR! code 1
npm ERR! path /home/gignu/GitHub/Movie-Subtitles/node_modules/node-sass
npm ERR! command failed

现在,从package.json中删除这个npm包,然后看看安装是否成功。如果成功了,你就找到了罪魁祸首!在我的情况下,我只需要升级node-sass到最新版本即可解决问题。

// package.json with the outdated dependency

"node-sass": "^4.14.1",
// package.json with the updated dependency

"node-sass": "^7.0.1",

缓慢的解决方案

如果之前的解决方案对你没有用,你可以尝试 @Andrew Fair 的解决方案:

我从 package.json 文件中删除了整个依赖部分,然后一次只安装一个依赖项逐个解决问题。

为此,只需将整个dependencies 部分和 devDependencies 部分从 package.json 中复制并粘贴到文本编辑器中进行备份。然后删除所有依赖项,逐个添加回一个依赖项并运行 npm i 检查是否会出现错误。如果你有许多依赖项,也可以每次添加回几个依赖项。找到导致错误的依赖项后,尝试升级该依赖项到较新版本,然后运行npm i --force

如果这样还是不行,尝试使用另一个 npm 包替换有问题的包,或者在其 GitHub 存储库上寻找解决方案。


这个解决方案对我有效,但只适用于node-sass 8.0.0(使用node v18.16和python 3)。 - Cerveser

11

尝试

npm cache clean --force

删除文件夹 node_modules 和文件 package-lock.json

npm i

1
这对我很有效,似乎比任何降级的解决方案都要好。 - johnnydoe82
2
StackOverflow作弊方法:搜索与npm相关的随机问题,然后建议此内容。 - Kerem atam

5

当我尝试在旧项目中运行npm install命令时,我遇到了相同的问题。通过按照以下步骤将package.json中的依赖项更新到最新版本来解决了这个问题:

  1. 删除目录下的 node_modules 文件夹和文件 package-lock.json

  2. 全局安装npm-check-updates以将所有软件包更新到新的主要版本

    运行 npm install -g npm-check-updates

  3. 运行 ncu -u 以将 package.json 中的依赖项更新为最新版本

    注意:您可以使用 npm update ,但是npm update不会更新到最新版本

  4. 运行 npm install


这破坏了一切 :D - Squibly

4

我也遇到了这个问题。检查了我的设置后,发现没有问题,并删除了缓存和package-lock文件,但仍然无法成功。

这是一个较旧的网站,我尝试使用更新等方法都不起作用。

所以我在我的package.json文件中去掉了整个依赖部分,并逐个安装开始工作。

它成功了!

显然,几乎所有版本都比最新版落后很多,这太难更新了。


3

在升级了Angular之后,我遇到了相同的问题。因此,我尝试了多个步骤来解决它。以下是其中一些步骤。

  1. try with deleting package-lock.json file and node_module folder then run

    npm cache clean --force
    

    And try npm i again to install packages. If it is still not working, check step 2.

  2. Check the Node.js version and npm version to see if both versions are compatible. In my case, I was using Node.js v14.15.0 and npm v8.1.6, so I uninstalled the npm with the below command and installed Node.js again to make the Node.js and npm versions in sync.

    npm uninstall npm -g
    

这对我来说是有效的。


经过很多努力,这个解决方案对我起作用了!npm和nodejs的版本不匹配,上述解决方案解决了这个问题。 - undefined

2

也许尝试使用 yarn install。对我来说有效。


1
  1. 如果存在,删除 package-lock.json 和 node_modules
  2. 运行 npm cache clean --force 命令
  3. 执行 npm install 或 npm install --legacy-peer-deps 命令

1

答案可以在这里找到。它是相同的问题: https://dev59.com/U8Pra4cB1Zd3GeqPuP9N#74197896

随着大多数操作系统删除python2,这对大多数项目来说是一个常见问题。在升级到当前的Linux/Debian Stable后,它肯定影响了我最近的4/5个项目,因为它删除了python2。

解决方案是让系统开始使用python3,为此您需要...

解决方案/已解决:

node-sass实际上有问题。直到版本6.0.0,才需要Python2。通常,您会升级到最新的7.0.3,但是node-sass目前已过时,因此我们将切换到sass项目。

yarn remove node-sass
yarn add sass

我在这里提供了更多关于原因的细节,或者如果你使用“render()和renderSync()函数”,可以考虑使用dart-sass。

Solved-ERR!gyp-info-using-node-gyp-3.8.0-gyp-verb-which-failed-Error-not-found-python2

影响范围:

  • Node.js/React网络应用
  • Elixir/Phoenix资产
  • Android应用
  • Vue.js项目

0
npm remove node-sass
npm add sass

对我来说,这个工作很顺利。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接