如何解决 npm ERR!安装webpack时的代码1

时间:2021-05-28 13:48:52

标签: npm webpack node-gyp native-module

当我想安装 webpack 时,我有很多错误。 我的节点版本是:v14.17.0 npm 版本:7.13.0 我在 Windows 10 上

以下是错误:

npm ERR! code 1
npm ERR! path D:\LocalSites\fundraise-better\app\public\wp-content\themes\fundraisebetter\node_modules\fibers
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node build.js || nodejs build.js
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@3.8.0
npm ERR! gyp info using node@14.17.0 | win32 | ia32
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 (child_process.js:319:12)
npm ERR! gyp ERR! stack     at ChildProcess.emit (events.js:376:20)
npm ERR! gyp ERR! stack     at maybeClose (internal/child_process.js:1055:16)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
npm ERR! gyp ERR! System Windows_NT 10.0.19041
npm ERR! gyp ERR! command "C:\\Program Files (x86)\\nodejs\\node.exe" "D:\\LocalSites\\fundraise-better\\app\\public\\wp-content\\themes\\fundraisebetter\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--release"
npm ERR! gyp ERR! cwd D:\LocalSites\fundraise-better\app\public\wp-content\themes\fundraisebetter\node_modules\fibers
npm ERR! gyp ERR! node -v v14.17.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok
npm ERR! node-gyp exited with code: 1
npm ERR! Please make sure you are using a supported platform and node version. If you
npm ERR! would like to compile fibers on this machine please make sure you have setup your
npm ERR! build environment--
npm ERR! Windows + OS X instructions here: https://github.com/nodejs/node-gyp
npm ERR! Ubuntu users please run: `sudo apt-get install g++ build-essential`
npm ERR! RHEL users please run: `yum install gcc-c++` and `yum groupinstall 'Development Tools'`
npm ERR! Alpine users please run: `sudo apk add python make g++`
npm ERR! 'nodejs' is not recognized as an internal or external command,
npm ERR! operable program or batch file.
npm timing npm Completed in 240044ms

3 个答案:

答案 0 :(得分:3)

问题是原生模块问题!这是一个常见的问题!

原生模块绑定原生程序和二进制文件模块!例如一个c++项目!因此需要项目构建和编译!为此,大多数软件包使用 node-gyp

问题很简单!您遇到的问题是您的环境缺少必要的构建和编译工具

如果你检查错误信息很好!都说了!

<块引用>

npm 错误!请确保您使用的是支持的平台和节点版本。如果你
npm 错误!想要在这台机器上编译纤维,请确保您已经设置了您的构建环境--
npm 错误! Windows + OS X 说明https://github.com/nodejs/node-gyp
npm 错误! Ubuntu 用户请运行:sudo apt-get install g++ build-essential
npm 错误! RHEL 用户请运行yum install gcc-c++yum groupinstall 'Development Tools'
npm 错误! Alpine 用户请运行sudo apk add python make g++

所以取决于您是什么平台Linux、Macos 或 Windows!按照上面的说明操作!

您可以简单地查看https://github.com/nodejs/node-gyp以查看不同的说明!

Python 在所有平台上都是必需的! (Node-gyp 大量使用 python 这就是为什么)

enter image description here

在linux中使用gcc! (检查上面的消息错误!获取说明)

"
基于 debian:sudo apt-get install g++ build-essential,
Redhat 和 Fedora:yum install gcc-c++yum groupinstall 'Development Tools'
任何其他分布搜索等价物!我从来没有在 linux 中遇到过这样的问题!大多数发行版都有!
"

在windows中需要visual studio构建工具

https://github.com/nodejs/node-gyp#on-windows

在 macos 中使用了 clang!而xcode需要有clang!

https://github.com/nodejs/node-gyp#on-macos

查看 node-gyp 文档!

文档中的步骤

在 macOS 上

注意:如果您的 Mac 已升级到 macOS Catalina (10.15),请阅读 macOS_Catalina.md。

  • Python v3.6、v3.7、v3.8 或 v3.9
  • Xcode
    您还需要通过运行 xcode-select --install 来安装 XCode 命令行工具。或者,如果您已经安装了完整的 Xcode,您可以在菜单 Xcode -> Open Developer Tool -> More Developer Tools...下找到它们。这一步将安装 clang、clang++ 和 make。

在 Windows 上

从 Microsoft Store 软件包安装当前版本的 Python。

手动安装工具和配置:

安装 Visual C++ 构建环境:Visual Studio Build 工具(使用“Visual C++ 构建工具”工作负载)或 Visual Studio Community(使用“使用 C++ 进行桌面开发”工作负载) 启动cmd,

npm config set msvs_version 2017

很重要!

如果上述步骤对您不起作用,请访问 Microsoft's Node.js Guidelines for Windows 以获取其他提示。

要在 ARM 上的 Windows 10 上以原生 ARM64 Node.js 为目标,请添加组件“Visual C++ compilers and libraries for ARM64”和“Visual C++ ATL for ARM64”。

从上面的链接下载构建工具安装程序后!或者来自download page here

enter image description here

工具是这样的

enter image description here

您也可以查看 c++ 的 vscode 文档:https://code.visualstudio.com/docs/cpp/config-msvc

只需要安装 C++ 构建工具!以及正确的元素!

您也可以选中 Installing MS C++ 14.0 for python without Visual Studio 以获得额外的意义!

和简单的工具

在windows上这篇文章都给出了很好的解释和简单的设置方法

https://spin.atomicobject.com/2018/06/18/windows-node-js/

<块引用>

编译扩展 我们没有任何自己的扩展需要在我们的项目中构建,但是我们的一些依赖项(即 node-sass) 可以。

像这样的扩展使用 node-gyp 构建,node-gyp 需要两件东西:Python (2…wince) 和 C 编译器两者都不是Windows 系统上的标准设备如果您没有它们并且需要它们来构建扩展,您将看到一长串 gyp ERR!安装依赖项时的消息。

值得庆幸的是,有一种合理的安装它们的简单方法 已经为 node-gyp 配置windows-build-tools

<块引用>

安装上面的 Scoop nodejs 包后,假设您安装了 Sudo,您现在可以运行:

<块引用>

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

(如果您还没有使用 scoop 包命令!那么只需删除 sudo 并在具有管理员权限的控制台上运行它

<块引用>

请注意,我们观察到这些安装程序至少重新启动系统一次,这实际上中止了该过程。在这种情况下,我们通过像这样重新运行安装程序来解决此问题:

<块引用>

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

来自 windows-build-toolsdoc

<块引用>

在 Windows 上?想要编译原生 Node 模块?使用这一单行安装构建工具。以管理员身份启动 PowerShell 并运行:

npm install --global windows-build-tools

enter image description here

<块引用>

安装后,npm 会自动执行这个模块,该模块会下载并安装 Visual C++ Build Tools,由微软免费提供给大多数用户(作为 Visual Studio 社区的一部分,请查阅许可证以确定您是否”重新合格)。编译流行的本机模块需要这些工具。如果尚未安装,它还将安装 Python 3.8,相应地配置您的机器和 npm。

<块引用>

||| [仅适用于 Windows Vista / 7] 需要 .NET Framework 4.5.1(此软件包当前未自动安装)

两种安装都是无冲突的,这意味着它们不会与 Visual Studio、C++ 构建工具或 Python 的现有安装混淆。如果您看到任何其他内容,请提交错误。

VS 2017 或 2015

<块引用>

此模块能够安装来自 Visual Studio 2017 或 Visual Studio 2015 的构建工具。

<块引用>

默认情况下,此工具将安装 2017 构建工具。要更改它,请使用 --vs2015 参数运行此脚本。

VS 2019

正如 node-gyp 文档!并指导!他们建议 vs 2017

<块引用>

启动 cmd,npm 配置集 msvs_version 2017

大多数软件包使用 vs 2015 或 2017! 2017年应该做的好!如果您因某种原因遇到任何问题!安装它们!

此外,如果在任何时间和任何原因需要 VS 2019!可以采用上述手动方式!

并使用 npm config set msvs_version 2019

人们总是可以使用它进行版本更改!说到 npm!

嗯,这是你可能永远不需要关心的事情!但以防万一!

带有可能有并发症的本机模块的包!通常他们会说明如何使用构建工具!如果有的话!

答案 1 :(得分:0)

试试管理员模式

npm install --global windows-build-tools

答案 2 :(得分:0)

我终于解决了这个问题。 node-gyp 已安装在我的机器上,但仍然无法正常工作。最后运行这个命令解决了这个问题:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser