You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建React应用时遇npm ERR! code 1 node-sass构建失败求助

创建React应用时node-sass安装失败问题

错误信息

npm ERR! code 1
npm ERR! path D:\Programs\btech\virtual experence\backpack\my-app\node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node scripts/build.js
npm ERR! Building: C:\Program Files\nodejs\node.exe D:\Programs\btech\virtual experence\backpack\my-app\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp verb cli [
npm ERR! gyp verb cli 'C:\Program Files\nodejs\node.exe',
npm ERR! gyp verb cli 'D:\Programs\btech\virtual experence\backpack\my-app\node_modules\node-gyp\bin\node-gyp.js',
npm ERR! gyp verb cli 'rebuild',
npm ERR! gyp verb cli '--verbose',
npm ERR! gyp verb cli '--libsass_ext=',
npm ERR! gyp verb cli '--libsass_cflags=',
npm ERR! gyp verb cli '--libsass_ldflags=',
npm ERR! gyp verb cli '--libsass_library='
npm ERR! gyp verb cli ]
npm ERR! gyp info using node-gyp@8.4.1
npm ERR! gyp info using node@18.10.0 | win32 | x64
npm ERR! gyp verb command rebuild []
npm ERR! gyp verb command clean []
npm ERR! gyp verb clean removing "build" directory
npm ERR! gyp verb command configure []
npm ERR! gyp verb find Python Python is not set from command line or npm configuration
npm ERR! gyp verb find Python Python is not set from environment variable PYTHON
npm ERR! gyp verb find Python checking if "python3" can be used
npm ERR! gyp verb find Python - executing "python3" to get executable path
npm ERR! gyp verb find Python - "python3" is not in PATH or produced an error
npm ERR! gyp verb find Python checking if "python" can be used
npm ERR! gyp verb find Python - executing "python" to get executable path
npm ERR! gyp verb find Python - executable path is

环境信息

  • Node版本: v18.10.0
  • npm版本: 9.2.0
解决方案

核心问题是node-gyp找不到Python环境,node-sass依赖node-gyp完成编译,以下是两种解决思路:

方案一:替换node-sass为dart-sass(推荐)

node-sass已停止维护,dart-sass是官方推荐替代方案,无需编译,兼容性更强:

  1. 卸载现有node-sass:
npm uninstall node-sass
  1. 安装dart-sass:
npm install sass
  1. 重启项目验证运行状态。

方案二:配置Python环境给node-gyp

若需保留node-sass,需让node-gyp识别到Python:

  1. 安装Python 2.7或3.7-3.10版本(Node 18推荐3.7+),安装时务必勾选Add Python to PATH选项。
  2. 打开cmd验证Python是否在PATH中:输入python --version或python3 --version,确认能正常输出版本号。
  3. 配置npm指向Python路径:
npm config set python "C:\你的Python安装路径\python.exe"
  1. 清理依赖后重新安装:
rm -rf node_modules package-lock.json
npm install

内容的提问来源于stack exchange,提问作者Kiran Jadhav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 16:32:23