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

npm安装失败问题:找不到Python2、node-sass与node-gyp报错

Elixir项目assets目录npm安装报错解决指南

问题现象

启动新Elixir项目时,在assets文件夹执行npm install反复失败:先是提示找不到Python2,随后触发node-sass或node-gyp相关编译错误,核心错误日志如下:

npm error     variables, includes, depth, check, True)
npm error   File "/Users/username/Desktop/ProjectFolder/elixir-projects/project_name/assets/node_modules/node-gyp/gyp/pylib/gyp/input.py", line 399, in LoadTargetBuildFile
npm error     includes, True, check)
npm error   File "/Users/username/Desktop/ProjectFolder/elixir-projects/project_name/assets/node_modules/node-gyp/gyp/pylib/gyp/input.py", line 271, in LoadOneBuildFile
npm error     aux_data, includes, check)
npm error   File "/Users/username/Desktop/ProjectFolder/elixir-projects/project_name/assets/node_modules/node-gyp/gyp/pylib/gyp/input.py", line 308, in LoadBuildFileIncludesIntoDict
npm error     LoadOneBuildFile(include, data, aux_data, None, False, check),
npm error   File "/Users/username/Desktop/ProjectFolder/elixir-projects/project_name/assets/node_modules/node-gyp/gyp/pylib/gyp/input.py", line 251, in LoadOneBuildFile
npm error     None)
npm error   File "/Users/username/.node-gyp/18.18.2/include/node/common.gypi", line 1
npm error     e_data_file_flag%': 1
npm error                         ^
npm error SyntaxError: EOL while scanning string literal
npm error gyp ERR! configure error 
npm error gyp ERR! stack Error: `gyp` failed with exit code: 1
npm error gyp ERR! stack     at ChildProcess.onCpExit (/Users/username/Desktop/ProjectFolder/elixir-projects/project_name/assets/node_modules/node-gyp/lib/configure.js:345:16)
npm error gyp ERR! stack     at ChildProcess.emit (node:events:517:28)
npm error gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:292:12)
npm error gyp ERR! System Darwin 23.4.0
npm error gyp ERR! command "/usr/local/bin/node" "/Users/username/Desktop/ProjectFolder/elixir-projects/project_name/assets/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm error gyp ERR! cwd /Users/username/Desktop/ProjectFolder/elixir-projects/project_name/assets/node_modules/node-sass
npm error gyp ERR! node -v v18.18.2
npm error gyp ERR! node-gyp -v v3.8.0
npm error gyp ERR! not ok 
npm error Build failed with error code: 1
npm error A complete log of this run can be found in: /Users/username/.npm/_logs/2024-06-04T16_41_15_604Z-debug-0.log

解决方案

1. 解决Python版本依赖问题

旧版node-gyp依赖Python2,可通过以下方式指定版本:

  • 若已安装Python2,临时设置环境变量:
    export PYTHON=/usr/bin/python2
    
  • 或用pyenv管理版本,先安装Python2.7,再在项目目录执行:
    pyenv local 2.7.18
    

2. 修复node-gyp缓存文件错误

错误日志显示common.gypi文件存在语法错误,删除对应版本的node-gyp缓存后重新安装:

rm -rf ~/.node-gyp/18.18.2
npm install

3. 替换node-sass为dart-sass(推荐长期方案)

node-sass已停止维护,兼容性差,建议替换为dart-sass:

  1. 卸载node-sass:
    npm uninstall node-sass
    
  2. 安装dart-sass:
    npm install sass --save-dev
    
  3. 检查项目配置文件(如webpack.config.js),将所有node-sass相关引用替换为sass

内容的提问来源于stack exchange,提问作者Max Lyman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 15:13:13