简体   繁体   English

NPM 使用 node-gyp 安装失败

[英]NPM install fails with node-gyp

We are having problems running "npm install" on our project.我们在项目上运行“npm install”时遇到问题。 A certain file cannot be found:找不到某个文件:

fatal error C1083: Cannot open include file: 'windows.h'

It appears to be coming from the node-gyp module:它似乎来自node-gyp模块:

c:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\src\win_delay_lo ad_hook.c(13): c:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\src\win_delay_lo ad_hook.c(13):

fatal error C1083: Cannot open include file: 'windows.h': No suc h file or directory [D:\ngs-frontend-next\node_modules\browser-sync\node_module s\socket.io\node_modules\engine.io\node_modules\ws\node_modules\bufferutil\buil d\binding.sln]致命错误 C1083:无法打开包含文件:'windows.h':没有这样的文件或目录 [D:\ngs-frontend-next\node_modules\browser-sync\node_module s\socket.io\node_modules\engine.io\ node_modules\ws\node_modules\bufferutil\build d\binding.sln]

This node-gyp seems to be a never ending source of pain for us.这个 node-gyp 似乎是我们永无止境的痛苦之源。 At first it complained that it needed python, so we installed that.起初它抱怨它需要 python,所以我们安装了它。 Then it complained that it needed VCBuild, which we installed (using .NET 2.0 SDK), now we have this error.然后它抱怨它需要我们安装的 VCBuild(使用 .NET 2.0 SDK),现在我们有这个错误。 It's almost as if the errors are getting more and more obscure, and it feels like we are going down some wrong path.就好像错误越来越模糊,感觉我们走错了路。

The strange thing is, that other people in our team have zero problems running the npm-install.奇怪的是,我们团队中的其他人在运行 npm-install 时遇到了零问题。

The full error looks like this:完整的错误如下所示:

c:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\src\win_delay_lo ad_hook.c(13): c:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\src\win_delay_lo ad_hook.c(13):

fatal error C1083: Cannot open include file: 'windows.h': No suc h file or directory [D:\ngs-frontend-next\node_modules\browser-sync\node_module s\socket.io\node_modules\engine.io\node_modules\ws\node_modules\bufferutil\buil d\binding.sln]致命错误 C1083:无法打开包含文件:'windows.h':没有这样的文件或目录 [D:\ngs-frontend-next\node_modules\browser-sync\node_module s\socket.io\node_modules\engine.io\ node_modules\ws\node_modules\bufferutil\build d\binding.sln]

gyp ERR: build error gyp ERR! gyp ERR:构建错误 gyp ERR! stack Error: C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe failed with exit code: 1 stack at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node _modules\node-gyp\lib\build.js:270:23) gyp ERR.堆栈错误: C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe失败,退出代码:ChildProcess.onExit 处有 1 个堆栈(C:\Program Files\nodejs\node_modules\npm\node _modules\node- gyp\lib\build.js:270:23) gyp 错误。 stack at emitTwo (events:js:87.13) gyp ERR.堆栈在 emitTwo (events:js:87.13) gyp ERR。 stack at ChildProcess:emit (events:js.172.7) gyp ERR.堆栈在 ChildProcess:emit (events:js.172.7) gyp ERR。 stack at Process.ChildProcess:_handle:onexit (internal/child_proces s.js.200:12) gyp ERR.堆栈在 Process.ChildProcess:_handle:onexit (internal/child_proces s.js.200:12) gyp ERR。 System Windows_NT 6:1.7601 gyp ERR: command "C.\Program Files\nodejs\node.exe" "C.\Program Files\nodej s\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" "rebuild" gyp ERR.系统 Windows_NT 6:1.7601 gyp ERR: 命令 "C.\Program Files\nodejs\node.exe" "C.\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" “重建”gyp ERR。 cwd D.\ngs-frontend-next\node_modules\browser-sync\node_modules\socket, io\node_modules\engine.io\node_modules\ws\node_modules\bufferutil gyp ERR. cwd D.\ngs-frontend-next\node_modules\browser-sync\node_modules\socket, io\node_modules\engine.io\node_modules\ws\node_modules\bufferutil gyp ERR。 node -v v4.2.2 gyp ERR!节点 -v v4.2.2 gyp 错误! node-gyp -v v3.0.3 gyp ERR! node-gyp -v v3.0.3 gyp 错误! not ok npm WARN optional dep failed, continuing utf-8-validate@1.2.1不行 npm WARN optional dep failed, continuing utf-8-validate@1.2.1

gifsicle@3.0.3 postinstall D:\ngs-frontend-next\node_modules\gulp-imagemin\nod e_modules\imagemin\node_modules\imagemin-gifsicle\node_modules\gifsicle node lib/install.js gifsicle@3.0.3 安装后 D:\ngs-frontend-next\node_modules\gulp-imagemin\node_modules\imagemin\node_modules\imagemin-gifsicle\node_modules\gifsicle node lib/install.js

Out package.json looks like this: package.json看起来像这样:

{
  "name": "Fast-nunjucks",
  "version": "0.0.1",
  "description": "A simple boilerplate using nunjucks as a template engine",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/willianjusten/Fast-nunjucks.git"
  },
  "keywords": [
    "nunjucks",
    "node",
    "gulp",
    "stylus"
  ],
  "author": "Willian Justen de Vasconcellos",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/willianjusten/Fast-nunjucks/issues"
  },
  "homepage": "https://github.com/willianjusten/Fast-nunjucks",
  "devDependencies": {
    "autoprefixer-stylus": "^0.7.1",
    "browser-sync": "^2.8.2",
    "gulp": "^3.9.0",
    "gulp-cache": "^0.3.0",
    "gulp-concat": "^2.6.0",
    "gulp-if": "^1.2.5",
    "gulp-imagemin": "^2.3.0",
    "gulp-minify-html": "^1.0.4",
    "gulp-nunjucks-html": "^1.2.2",
    "gulp-order": "^1.1.1",
    "gulp-plumber": "^1.0.1",
    "gulp-stylus": "^2.0.6",
    "gulp-uglify": "^1.2.0",
    "gulp-util": "^3.0.6",
    "jeet": "^6.1.2",
    "kouto-swiss": "^0.11.13",
    "minimist": "^1.1.3",
    "rupture": "^0.6.1"
  },
  "dependencies": {
    "gulp-install": "^0.6.0"
  }
}

The error messages have caused confusion for me and have not helped me in resolving the errors completely.错误消息给我造成了困惑,并没有帮助我完全解决错误。

The README.md for node-gyp project does a better job of listing down the installation instructions for Unix, Max OS X and Windows . node- gyp项目的 README.md 更好地列出了 Unix、Max OS X 和Windows的安装说明。

In Windows systems you can either go with Option 1 or Option 2 but the main thing is that you need to install the Visual C++ Build Tools .Windows系统中,您可以使用选项 1 或选项 2,但主要是您需要安装Visual C++ Build Tools

The following quote is from the Windows installation section:以下引用来自Windows安装部分:

  • On Windows:在 Windows 上:
    • Visual C++ Build Environment: Visual C++ 构建环境:
      • Option 1: Install Visual C++ Build Tools using the Default Install option.选项 1:使用默认安装选项安装 Visual C++ 构建工具。
      • Option 2: Install Visual Studio 2015 (or modify an existing installation) and select Common Tools for Visual C++ during setup.选项 2:安装 Visual Studio 2015(或修改现有安装)并在安装过程中选择用于 Visual C++ 的通用工具 This also works with the free Community and Express for Desktop editions.这也适用于免费的 Community 和 Express for Desktop 版本。

      [Windows Vista / 7 only] requires .NET Framework 4.5.1 [仅限 Windows Vista / 7] 需要 .NET Framework 4.5.1
    • Install Python 2.7 ( v3.xx is not supported), and run安装 Python 2.7(不支持v3.xx ),然后运行
      npm config set python python2.7
      (or see below for further instructions on specifying the proper Python version and path.) (或参见下文以获取有关指定正确 Python 版本和路径的进一步说明。)
    • Launch cmd,启动cmd,
       npm config set msvs_version 2015

If the above steps didn't work for you, please visit Microsoft's Node.js Guidelines for Windows for additional tips.如果上述步骤对您不起作用,请访问 Microsoft 的 Windows Node.js 指南以获取更多提示。

Common instructions for Python configuration: Python配置的常用说明:

If you have multiple Python versions installed, you can identify which Python version node-gyp uses by setting the '--python' variable:如果您安装了多个 Python 版本,您可以通过设置“--python”变量来识别 node-gyp 使用的 Python 版本:

$ node-gyp --python /path/to/python2.7

If node-gyp is called by way of npm and you have multiple versions of Python installed, then you can set npm's 'python' config key to the appropriate value:如果通过 npm 调用 node-gyp 并且您安装了多个版本的 Python,那么您可以将 npm 的“python”配置键设置为适当的值:

$ npm config set python /path/to/executable/python2.7

Successfully configured my system following the above instructions.按照上述说明成功配置了我的系统。

System Info系统信息

λ ver

Microsoft Windows [Version 6.1.7601]

λ node -v
v6.2.0
λ npm -v
3.9.2

Links to relevant tools / articles:相关工具/文章的链接:

Visual C++ Build Tools Visual C++ 构建工具

Visual Studio 2015 视觉工作室 2015

.NET Framework 4.5.1 .NET 框架 4.5.1

Python 2.7蟒蛇 2.7

Microsoft's Node.js Guidelines for Windows Microsoft 针对 Windows 的 Node.js 指南

Try to install again with --force option :尝试使用--force option再次安装:

npm install --force

If this doesn't work try to update npm globally:如果这不起作用,请尝试全局更新 npm:

npm update -g npm 

and try again with the --force option.并使用--force选项重试。

I tried all the solutions above but none of them worked.我尝试了上述所有解决方案,但都没有奏效。 My problem was with some files in the root directory that I had to delete:我的问题是我必须删除根目录中的一些文件:

package-lock.json and yarn.lock and yarn-error.log package-lock.jsonyarn.lockyarn-error.log

after removing these files, I ran:删除这些文件后,我运行:

npm cache clean --force

then I ran the command:然后我运行命令:

npm install

and the error was gone错误消失了

I had nearly the same problem on Mac.我在 Mac 上遇到了几乎相同的问题。 I got lot's of spam error messages, but finally I recognized the issue, it was a version missmatch...我收到了很多垃圾邮件错误消息,但最后我意识到了这个问题,它是版本不匹配...

Description:描述:

npm install failed on installing gyp (node-gyp): npm install 安装 gyp (node-gyp) 失败:

npm ERR! gyp ERR! node -v v16.2.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

Solution:解决方案:

downgrade npm / node to stable version!将 npm / node 降级到稳定版本!

Try:尝试:

Install 'n': npm i -gn安装“n”: npm i -gn

Downgrade npm: sudo n stable降级 npm: sudo n stable

Rerun npm install and this works!重新运行npm install就可以了!

The last time I saw a similar error it was because I was using the wrong version of npm and/or node for one of my dependencies.上次我看到类似的错误是因为我为我的一个依赖项使用了错误版本的npm和/或node Try upgrading these and try again.尝试升级这些并重试。

Before trying again remove your node_modules directory.在再次尝试删除您的node_modules目录之前。

You may need to investigate what versions of npm and node your dependencies need.您可能需要调查您的依赖项需要哪些版本的npmnode You could try the latest versions of all your dependencies, node and npm.您可以尝试所有依赖项、node 和 npm 的最新版本。

Check what versions your colleagues are using.检查您的同事使用的版本。

What OS are you using?您使用的是什么操作系统? That can have an impact as version of CLANG maybe different.这可能会产生影响,因为 CLANG 的版本可能不同。

for this solved it ( OS X ):为此解决了它(OS X):

rm -rf  ~/.node_gyp and
sudo npm install -g node-gyp@3.4.0
cd /usr/local/lib
sudo ln -s ../../lib/libSystem.B.dylib libgcc_s.10.5.dylib 
brew install gcc
npm install

如果您使用的是“node-sass”,请将其升级到版本 5.0.0,确保您已安装构建工具 2017 和 Python 3,并在 npm install 之前发出这些命令

npm config set msvs_version 2017

npm config set python npm config set python C:\Users\username\AppData\Local\Programs\Python\Python39\python.exe

尝试删除当前 node_modules 文件夹并以普通用户、非管理员或 root 用户身份运行命令

  1. Install VC++ Build Tools Technical Preview [Windows 7 only] requires .NET Framework 4.5.1安装 VC++ Build Tools Technical Preview [仅限 Windows 7] 需要 .NET Framework 4.5.1

  2. Install Python 2.7, and add it to your PATH, npm config set python python2.7安装 Python 2.7,并将其添加到您的 PATH,npm config set python python2.7

  3. Launch cmd, npm config set msvs_version 2015 --global (this is instead of npm install [package name] --msvs_version=2015 every time.)启动 cmd, npm config set msvs_version 2015 --global (这是每次都代替 npm install [package name] --msvs_version=2015。)

4.SO MUCH npm install :tada: 4.这么多 npm 安装 :tada:

Post my solution here in case anyone like me using Windows 10 still has the error after installing many Microsoft associated build tools.在此处发布我的解决方案,以防像我这样使用Windows 10的人在安装许多 Microsoft 相关构建工具后仍然遇到错误。

All you need are:您只需要:

Windows 10 64 bit视窗 10 64 位

python 2.7.x蟒蛇2.7.x

Visual Studio 2013 视觉工作室 2013

I've tried Visual Studio 2015 before but it didn't work at all.我之前尝试过 Visual Studio 2015,但它根本不起作用。


  1. First of all, I uninstall all the Microsoft associated build tools.首先,我卸载了所有与 Microsoft 相关的构建工具。 (If you didn't install anything to try to solve this problem before, skip this step.) (如果您之前没有安装任何东西来尝试解决此问题,请跳过此步骤。)

  2. Install Visual Studio 2013.安装 Visual Studio 2013。

  3. Config npm as @Sourav said:像@Sourav 所说的那样配置 npm:

Config python:配置蟒蛇:

 npm config set python /path/to/python2.7

Config msvs_version:配置 msvs_version:

 npm config set msvs_version 2013

Run a npm install or the npm command which occurs this error.运行出现此错误的npm install或 npm 命令。 It works for me!这个对我有用!

BTW, this solution is from here .顺便说一句,这个解决方案来自这里

Hey please try to install the windows build tools (global npm package)嘿,请尝试安装windows 构建工具(全局 npm 包)

This will fix your problem :-) You should be able to use your npm install command afterwards.这将解决您的问题 :-) 之后您应该可以使用您的 npm install 命令。

Based on the node-gyp README , update npm npm i npm -g , and will bring in a newer version of node-gyp.基于node-gyp README ,更新 npm npm i npm -g ,将引入更新版本的 node-gyp。

Hint: if you are on windows perform npm install --global --production windows-build-tools from an elevated PowerShell or CMD.exe (run as Administrator).提示:如果您在Windows上,请从提升的 PowerShell 或 CMD.exe(以管理员身份运行)执行npm install --global --production windows-build-tools

For Windows 8 or 10:对于 Windows 8 或 10:

Assuming node and npm installed already.假设节点和 npm 已经安装。

My node and npm version (At the time of writing this answer) :我的节点和 npm 版本(在撰写此答案时):

node : 8.9.4节点:8.9.4

npm : 5.6.0 npm:5.6.0

  1. First install python 2.x首先安装python 2.x

  2. Make sure it is available on path确保它在路径上可用

  3. Install Visual Studio Installer安装Visual Studio 安装程序

  4. After installation, run it安装好后运行

  5. A new interface will open it where you can select various options.将打开一个新界面,您可以在其中选择各种选项。
  6. Now select Build Tools for Visual Studio 2017 from the available options and install it.现在从可用选项中选择Build Tools for Visual Studio 2017并安装它。 You don't need to install Visual Studio.您不需要安装 Visual Studio。

在此处输入图像描述 After the installation it should work perfectly.安装后它应该可以完美运行。

I was working on an older project and was encountering this error (or a very similar one with node-gyp) due to the package-lock being for an older version of node.我正在处理一个较旧的项目并遇到此错误(或与 node-gyp 非常相似的错误),因为 package-lock 是针对较旧版本的 node.js 的。 After downgrading to that version, I was able to run npm install without issue.降级到该版本后,我能够运行 npm install 没有问题。

I was running into similar issues on my project.我在我的项目中遇到了类似的问题。 In my case, deleting package-lock.json file and letting it recreate helped.就我而言,删除package-lock.json文件并让它重新创建会有所帮助。

编辑或删除您的 package-lock.json 并运行 npm install

I am using yarn so I just ran yarn global add node-gyp to globally install node-gyp without using npm and it worked.我正在使用纱线,所以我只是运行yarn global add node-gyp来全局安装node-gyp而不使用 npm 并且它工作。 I'm no more facing the error我不再面临错误

For those using npm can do it using npm install -g node-gyp .对于那些使用 npm 的人,可以使用npm install -g node-gyp来完成。

Make sure you know that running these commands will globally install node-gyp in your environment, probably outside the scope of your current app too.确保您知道运行这些命令将在您的环境中全局安装 node-gyp,也可能超出您当前应用程序的范围。 And in case you are running any other apps in the similar environment that are dependent on any particular version of node-gyp , you might face some troubles.如果您在类似环境中运行依赖于任何特定版本的node-gyp的任何其他应用程序,您可能会遇到一些麻烦。

I was stuck with the same error on my windows machine after a lot of troubleshooting with various stack answers I was not able to fix it, later did the following which made it work properly now.在对各种堆栈答案进行大量故障排除后,我在 Windows 机器上遇到了同样的错误,我无法修复它,后来做了以下操作,使它现在可以正常工作。

  1. npm cache clear --force npm 缓存清除 --force
  2. open explorer in address bar paste %appdata%, then delete the folders npm,npm-cache在地址栏中打开资源管理器粘贴 %appdata%,然后删除文件夹 npm,npm-cache
  3. uninstalled nodejs and reinstalled latest version.卸载nodejs并重新安装最新版本。
  4. then npm install worked fine for me然后 npm install 对我来说很好

删除package-lock.json然后重启你的机器(如果有更改生效)然后运行

npm install --legacy-peer-deps

this is what solved it for me这就是为我解决的问题

sudo npm install -g npm     
npm cache clean --force
npm install

Install python2 and try running npm install again.安装 python2 并尝试再次运行npm install This approach worked for me.这种方法对我有用。

The solution that worked for me is running对我有用的解决方案正在运行

npm install --global --production windows-build-tools

Though the script got stuck in the python installation part, the npm install works perfectly right after, plus it took a good while.虽然脚本卡在 python 安装部分,但 npm 安装之后就可以完美运行,而且花了很长时间。 I only knew it could work because i saw cpu usage of visual studio drop to 0 for a while.我只知道它可以工作,因为我看到 Visual Studio 的 cpu 使用率下降到 0 一段时间。

I just ran我刚跑

> yarn install

and it worked.它奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM