vue2项目使用node-sass时,npm install报错

6 阅读1分钟

node-sass使用npm镜像安装时,网络不同会导致安装失败,然后走本地编译(需依赖Python环境),导致报错

gyp verb could not find "python". checking python launcher
gyp verb could not find "python". guessing location 
gyp verb ensuring that file exists: C:\Python27\python.exe
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (D:\project\my-app\node_modules\node-gyp\lib\configure.js:484:19)
gyp ERR! stack     at PythonFinder.<anonymous> (D:\project\my-app\node_modules\node-gyp\lib\configure.js:509:16)
gyp ERR! stack     at callback (D:\project\my-app\node_modules\graceful-fs\polyfills.js:306:20)
gyp ERR! stack     at FSReqCallback.oncomplete (fs.js:192:21)
gyp ERR! System Windows_NT 10.0.22621
gyp ERR! command "C:\\Users\\admin\\AppData\\Local\\Volta\\tools\\image\\node\\14.21.3\\node.exe" "D:\\project\\my-app\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd D:\project\my-app\node_modules\node-sass
gyp ERR! node -v v14.21.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Build failed with error code: 1
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-sass@4.14.1 postinstall: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-sass@4.14.1 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

解决方案

设置node-sass安装使用淘宝镜像,然后重新install即可

npm set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass

血泪教训:不建议升级为sass(dart-sass)

1.语法兼容问题:升级后/deep/语法会报错,导致项目运行内存溢出,npm run dev启动不起来; 2.node-sass和sass-loader以及node版本兼容问题,需要一一对应,否则项目也会启动失败报错。