一生之敌 node-gyp
一般来说, 有些项目使用了
node-sass
, 当我们在安装node-sass
的时候,需要配置一下node-gyp
以确保node-sass
正常编译
正常来说, 此处需要安装 python 和 vsbuild 两个一般来说, 网上搜到的教程都是需要安装window-build-tools, 但是由于未知原因把, 安装的时候总是会卡住, 所以这里记录一种其他的方式绕过这个问题
- 在
cmd
中执行yarn global add windows-build-tools
, 等待卡住, 同时观察%userprofile%/.windows-build-tools
一般情况下都是python 安装成功 但是 vs_buildtools 安装失败;
-
观察到
%userprofile%/.windows-build-tools
出现python27
出现时, 可以认为 python 环境安装成功, 此时新打开一个cmd
输入py -V
, 确认 python 是否安装成功(出现python版本号即为安装成功); -
手动安装vs_build, 双击
vs_BuildTools.exe
, 安装Visual Studio 15 生成工具 2017
-
至此, 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"