开发者问题收集

NPM 安装因 node-gyp 而失败

2015-11-24
398720

我们在项目上运行 “npm install” 时遇到问题。无法找到某个文件:

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

它似乎来自 node-gyp 模块:

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]

这个 node-gyp 似乎给我们带来了无穷无尽的痛苦。起初它抱怨说需要 python,所以我们安装了它。然后它抱怨说需要 VCBuild,我们安装了它(使用 .NET 2.0 SDK),现在我们遇到了这个错误。好像错误越来越模糊,感觉我们走错了路。

奇怪的是,我们团队中的其他人在运行 npm-install 时没有遇到任何问题。

完整的错误如下所示:

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]

gyp ERR! build error 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! stack at emitTwo (events.js:87:13) gyp ERR! stack at ChildProcess.emit (events.js:172:7) gyp ERR! stack at 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! 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! node-gyp -v v3.0.3 gyp ERR! not ok npm WARN optional dep failed, continuing [email protected]

[email protected] postinstall D:\ngs-frontend-next\node_modules\gulp-imagemin\nod e_modules\imagemin\node_modules\imagemin-gifsicle\node_modules\gifsicle node lib/install.js

Out 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"
  }
}
3个回答

我尝试了上述所有解决方案,但都不起作用。我的问题在于根目录中的一些文件,我必须删除它们:

package-lock.jsonyarn.lock 以及 yarn-error.log

删除这些文件后,我运行:

npm cache clean --force

然后我运行命令:

npm install

错误消失了

Mahdieh Shavandi
2021-03-16

这些错误消息让我感到困惑,并没有帮助我完全解决错误。

README.md 适用于 node-gyp 项目的 README.md 更好地列出了 Unix、Max OS X 和 Windows 的安装说明。

Windows 系统中,您可以选择选项 1 或选项 2,但最重要的是您需要安装 Visual C++ Build工具

以下引述来自 Windows 安装部分:

  • On Windows:
    • Visual C++ Build Environment:
      • Option 1: Install Visual C++ Build Tools using the Default Install option.
      • Option 2: Install Visual Studio 2015 (or modify an existing installation) and select Common Tools for Visual C++ during setup. This also works with the free Community and Express for Desktop editions.

      [Windows Vista / 7 only] requires .NET Framework 4.5.1
    • Install Python 2.7 ( v3.x.x is not supported), and run
      npm config set python python2.7
      (or see below for further instructions on specifying the proper Python version and path.)
    • Launch 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.

Python 配置的常用说明:

If you have multiple Python versions installed, you can identify which Python version node-gyp uses by setting the '--python' variable:

$ 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 config set python /path/to/executable/python2.7

按照上述说明成功配置了我的系统。

系统信息

λ ver

Microsoft Windows [Version 6.1.7601]

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

相关工具/文章的链接:

Visual C++ Build Tools

Visual Studio 2015

.NET Framework 4.5.1

Python 2.7

Microsoft 的适用于 Windows 的 Node.js 指南

Sourav Paul
2016-05-25

尝试使用 --force 选项 再次安装:

npm install --force

如果此方法不起作用,请尝试全局更新 npm:

npm update -g npm 

然后使用 --force 选项重试。

David Vega
2015-11-24