【问题标题】:How to solve npm install error “npm ERR! code 1”如何解决 npm 安装错误“npm ERR!代码 1”
【发布时间】:2021-07-27 16:28:31
【问题描述】:

我正在尝试安装 gulp,当我写 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,但没有帮助。我不知道该怎么办。

【问题讨论】:

  • 你有一个用 Python2 语言编写的程序,你正试图用 Python3 运行它:一种相关但不同的语言。这不是node.js的问题,和Git无关。
  • 我已经下载了 python27 并将其添加到 PATH 中,但它仍然无法正常工作:/ 同样的错误。我不知道我是不是做错了什么。

标签: npm gulp npm-install


【解决方案1】:

试试 npm install --python=python2.7。 它应该工作。 编辑:或者,--python=path to your python executable

【讨论】:

  • npm config set python 和你的 python 可执行文件?然后npm install?
【解决方案2】:

如果您的 Node 版本非常新,请尝试降级。稳定版 14.16.1 工作正常。

【讨论】:

  • 这对我来说绝对是个问题。我经历了很多麻烦,降级节点是有效的。 14.x 是目前推荐给大多数人的。纱线迫使我达到 16.x。在自制软件中,您可以brew install node@14 brew unlink node brew link node@14 回到 14 或在它们之间切换。
  • 感谢@AlejandroS 这在我的apple-silicon 场景中工作,使用brew 作为@Jake 显示,但是我只是简单地为npm、npx、节点alias npm14='/opt/homebrew/Cellar/node@14/*/bin/npm' 中的每一个取别名并使用这些作品,我确定全局 node_modules 路径会有问题,我还没有解决。
  • 下载链接:nodejs.org/dist/v14.16.1
【解决方案3】:

试试

npm cache clean --force

删除 node_modules 和 package-lock.json

npm i

【讨论】:

  • 这对我有用,对我来说似乎是比降级任何东西更好的解决方案
【解决方案4】:

我也遇到了这个问题。查看我的设置后,我没有发现任何问题,并删除了缓存并删除了包锁,但仍然没有成功。

这是一个较旧的网站,我尝试使用 update/etc 无济于事。

所以我取出了 package.json 文件中的整个依赖项部分,然后开始一次安装一个。

有效!

显然,几乎所有版本都是一个重大更新,其背后的更新太多了。

【讨论】:

    【解决方案5】:

    降级 Node 是一种选择,但我不喜欢仅仅因为一个包引发错误而降级 Node 的想法。在我看来,找出导致错误的包然后修复它会是一种更好的方法。

    快速解决方案

    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 升级到最新版本,然后运行npm i node-sass --force

    // 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 文件中取出了整个依赖项部分,并开始按照自己的方式一次安装一个。

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

    如果这不起作用,请尝试将坏的 npm 包替换为另一个包,或者查看他们的 GitHub 存储库以获取解决方案。

    【讨论】:

      【解决方案6】:

      升级 Angular 后我遇到了同样的问题,所以我尝试了多个步骤来解决它。下面提到了一些步骤。

      1. 尝试删除 package-lock.json 文件和 node_module 文件夹,然后运行
      npm cache clean --force 
      

      如果仍然无法正常工作,请再次尝试 npm i 安装包检查步骤 2

      1. 检查 nodeJs 版本和 npm 版本是否兼容。就我而言,我使用的是 node v14.15.0 和 npm v8.1.6,所以我使用以下命令卸载了 npm 并再次安装了 node js 以使 node 和 npm 版本同步。
      npm uninstall npm -g
      

      这对我有用。

      【讨论】:

        猜你喜欢
        • 2021-01-18
        • 1970-01-01
        • 2021-12-19
        • 2020-12-27
        • 1970-01-01
        • 2021-11-06
        • 2022-12-14
        相关资源
        最近更新 更多