一生之敌 node-gyp

本文讲述了在配置node-sass时遇到的各种问题,如Python版本、VSBuild安装失败等,并提供了相应的解决方案,包括指定Python路径、设置msbuild_path以及处理openssl_fips相关错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一生之敌 node-gyp

一般来说, 有些项目使用了 node-sass, 当我们在安装node-sass的时候,需要配置一下node-gyp以确保node-sass正常编译
正常来说, 此处需要安装 python 和 vsbuild 两个

一般来说, 网上搜到的教程都是需要安装window-build-tools, 但是由于未知原因把, 安装的时候总是会卡住, 所以这里记录一种其他的方式绕过这个问题

  1. cmd中执行yarn global add windows-build-tools , 等待卡住, 同时观察 %userprofile%/.windows-build-tools
    在这里插入图片描述

一般情况下都是python 安装成功 但是 vs_buildtools 安装失败;

  1. 观察到%userprofile%/.windows-build-tools 出现 python27出现时, 可以认为 python 环境安装成功, 此时新打开一个 cmd 输入 py -V, 确认 python 是否安装成功(出现python版本号即为安装成功);

  2. 手动安装vs_build, 双击 vs_BuildTools.exe, 安装 Visual Studio 15 生成工具 2017
    在这里插入图片描述
    在这里插入图片描述

  3. 至此, node-gyp 环境配置完成, 回到工作目录, 删除 node-modules, 重新安装依赖;

报错1 python 问题

gyp ERR! find Python - executable path is “C:\Users\auto.windows-build-tools\python27\python.exe”
gyp ERR! find Python - version is “2.7.15”
gyp ERR! find Python - version is 2.7.15 - should be >=3.6.0

⭐解决方案: 指定python路径

# 执行 设置 python 路径(永久)
npm config set python "D:\Program Files\Python\Python311\python.exe"
yarn config set python "D:\Program Files\Python\Python311\python.exe"

# 切换 python 版本(永久)
npm config set python "D:\Program Files\Python\Python39\python.exe"
yarn config set python "D:\Program Files\Python\Python39\python.exe"

# 临时使用
npm install --python="D:\Program Files\Python\Python311\python.exe"

报错2 msbuild 问题

gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with ‘–loglevel silly’ for more details

⭐解决方案 :指定msvs_version和msbuild_path

npm config set -g msvs_version 2017
# 执行 设置 msbuild_path
npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe"
yarn config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe"

报错 3 msbuild_path is not a valid npm option

npm ERR! msbuild_path is not a valid npm option

原因是 npm 9.x 的版本已经不支持这个选项

可以用 npm --version 检查版本

⭐解决方案: 指定安装8.x版本

npm install -g npm@^8

报错 4 name ‘openssl_fips’ is not defined while evaluating condition ‘openssl_fips != “”’

name ‘openssl_fips’ is not defined while evaluating condition ‘openssl_fips != “”’ in binding.gyp while trying to load binding.gyp

# https://github.com/nodejs/node-gyp/issues/2673#issuecomment-1196931379
npm install --openssl_fips=''

# 我采用的解决方案
npm config set openssl_fips ""
yarn config set openssl_fips ""

报错5 ValueError: invalid mode: ‘rU’ while trying to load binding.gyp

https://github.com/nodejs/node-gyp/issues/2219#issuecomment-1356278079

原因 :

U 文件模式在 Python 3.11 中被删除,因为它没有任何效果,因此只能删除 U。

解决:

可以确认将我的 conda 环境中的 python 从 3.11.1 降级到 3.10.8 为我解决了这个问题。

npm config set python "D:\Program Files\Python\Python39\python.exe"
yarn config set python "D:\Program Files\Python\Python39\python.exe"
### Node-gyp 报错解决方案 Node-gyp 是用于构建原生插件的工具,在执行过程中可能会因为多种因素而发生错误。当 `node-gyp` 在运行时抛出异常,特别是针对文件 `node-gyp.js:81` 的位置出现问题时,通常可以从以下几个方面着手排查并解决问题。 #### Linux C++ 编译模块版本过低 如果是在Linux环境下操作,并且遇到了由 `node-gyp` 引发的编译错误,则可能是由于系统自带的 GCC 版本太旧所引起的[^1]。此时建议联系服务器管理员来更新系统的GCC至最新稳定版,这往往能有效解决大部分因编译器老旧而导致的问题。 #### NodeJSnode-gyp 不匹配 另一个常见的问题是使用的 NodeJS 版本可能与所需的 `node-gyp` 并不完全兼容。对于这种情况,可以通过升级整个 NodeJS 环境或是单独安装较新的 `node-gyp` 来尝试修复问题。具体做法如下: ```bash # 升级 NodeJS 到最新LTS版本 sudo npm cache clean -f sudo npm install -g n sudo n stable # 或者仅全局安装最新的 node-gyp npm install -g node-gyp@latest ``` #### Python 环境缺失或版本冲突 除了上述两个主要原因外,有时也会碰到缺少必要的Python解释器或者是已有的Python版本不适合当前项目的状况[^3]。为了确保一切顺利,应该确认本地已经正确配置了一个适当版本(通常是2.7.x系列)的Python路径给 `node-gyp` 使用: ```bash export PYTHON=/usr/bin/python2.7 ``` 通过以上措施,大多数情况下都可以成功绕过 `node-gyp` 所带来的麻烦。当然,具体情况还需要根据实际的日志输出来进行针对性分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值