error D:\xxx\xxx\node_modules\node-sass: Command failed.

导言:运行老项目的时候经常遇到node-sass安装不下来的问题,百度搜索更是千篇一律的镜像原因,当然镜像问题原因是更多人可能遇到的问题,比较少见的一种是运行老项目时,node版本比较低,导致node-sass安装不了

提示node-sass安装异常我遇到过两种情况,一种是安装依赖时需要node-sass,报错一般是↓↓↓

error D:\xxx\xxx\node_modules\node-sass: Command failed.
Exit code: 1
Command: node scripts/build.js
Arguments:
Directory: D:\xxx\xxx\node_modules\node-sass
Output:
Binary found at D:\software\nodesass\win32-x64-72_binding.node
Testing binary
Binary has a problem: Error: The module '\\?\D:\software\nodesass\win32-x64-72_binding.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 72. This version of Node.js requires
NODE_MODULE_VERSION 64. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:717:18)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at module.exports (D:\xxx\xxx\node_modules\node-sass\lib\binding.js:19:10)
    at Object.<anonymous> (D:\xxx\xxx\node_modules\node-sass\lib\index.js:14:35)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
Building the binary locally
Building: C:\Program Files\nodejs\node.exe D:\xxx\xxx\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
gyp info it worked if it ends with ok
gyp verb cli [ 'C:\\Program Files\\nodejs\\node.exe',
gyp verb cli   'D:\\xxx\\xxx\\node_modules\\node-gyp\\bin\\node-gyp.js',
gyp verb cli   'rebuild',
gyp verb cli   '--verbose',
gyp verb cli   '--libsass_ext=',
gyp verb cli   '--libsass_cflags=',
gyp verb cli   '--libsass_ldflags=',
gyp verb cli   '--libsass_library=' ]
gyp info using node-gyp@3.8.0
gyp info using node@10.14.1 | win32 | x64
gyp verb command rebuild []
gyp verb command clean []
gyp verb clean removing "build" directory
gyp verb command configure []
gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2
gyp verb `which` failed     at getNotFoundError (D:\xxx\xxx\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (D:\xxx\xxx\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (D:\xxx\xxx\node_modules\which\which.js:80:29)
gyp verb `which` failed     at D:\xxx\xxx\node_modules\which\which.js:89:16
gyp verb `which` failed     at D:\xxx\xxx\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at D:\xxx\xxx\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:154:21)
gyp verb `which` failed  python2 { Error: not found: python2
gyp verb `which` failed     at getNotFoundError (D:\xxx\xxx\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (D:\xxx\xxx\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (D:\xxx\xxx\node_modules\which\which.js:80:29)
gyp verb `which` failed     at D:\xxx\xxx\node_modules\which\which.js:89:16
gyp verb `which` failed     at D:\xxx\xxx\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at D:\xxx\xxx\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:154:21)
gyp verb `which` failed   stack:
gyp verb `which` failed    'Error: not found: python2\n    at getNotFoundError (D:\\xxx\\xxx\\node_modules\\which\\which.js:13:12)\n    at F (D:\\xxx\\xxx\\node_modules\\which\\which.js:68:19)\n    at E (D:\\xxx\\xxx\\node_modules\\which\\which.js:80:29)\n    at D:\\xxx\\xxx\\node_modules\\which\\which.js:89:16\n    at D:\\xxx\\xxx\\node_modules\\isexe\\index.js:42:5\n    at D:\\xxx\\xxx\\node_modules\\isexe\\windows.js:36:5\n    at FSReqWrap.oncomplete (fs.js:154:21)',
gyp verb `which` failed   code: 'ENOENT' }
gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` failed Error: not found: python
gyp verb `which` failed     at getNotFoundError (D:\xxx\xxx\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (D:\xxx\xxx\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (D:\xxx\xxx\node_modules\which\which.js:80:29)
gyp verb `which` failed     at D:\xxx\xxx\node_modules\which\which.js:89:16
gyp verb `which` failed     at D:\xxx\xxx\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at D:\xxx\xxx\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:154:21)
gyp verb `which` failed  python { Error: not found: python
gyp verb `which` failed     at getNotFoundError (D:\xxx\xxx\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (D:\xxx\xxx\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (D:\xxx\xxx\node_modules\which\which.js:80:29)
gyp verb `which` failed     at D:\xxx\xxx\node_modules\which\which.js:89:16
gyp verb `which` failed     at D:\xxx\xxx\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at D:\xxx\xxx\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:154:21)
gyp verb `which` failed   stack:
gyp verb `which` failed    'Error: not found: python\n    at getNotFoundError (D:\\xxx\\xxx\\node_modules\\which\\which.js:13:12)\n    at F (D:\\xxx\\xxx\\node_modules\\which\\which.js:68:19)\n    at E (D:\\xxx\\xxx\\node_modules\\which\\which.js:80:29)\n    at D:\\xxx\\xxx\\node_modules\\which\\which.js:89:16\n    at D:\\xxx\\xxx\\node_modules\\isexe\\index.js:42:5\n    at D:\\xxx\\xxx\\node_modules\\isexe\\windows.js:36:5\n    at FSReqWrap.oncomplete (fs.js:154:21)',
gyp verb `which` failed   code: 'ENOENT' }
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:\xxx\xxx\node_modules\node-gyp\lib\configure.js:484:19)
gyp ERR! stack     at PythonFinder.<anonymous> (D:\xxx\xxx\node_modules\node-gyp\lib\configure.js:509:16)
gyp ERR! stack     at D:\xxx\xxx\node_modules\graceful-fs\polyfills.js:284:29
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:154:21)
gyp ERR! System Windows_NT 10.0.19042
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\xxx\\xxx\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd D:\xxx\xxx\node_modules\node-sass
gyp ERR! node -v v10.14.1
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Build failed with error code: 1
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

一种是依赖安装成功,但跑项目时因为部分依赖依赖了node-sass导致运行不成功,异常一般是↓↓↓

internal/modules/cjs/loader.js:717
  return process.dlopen(module, path.toNamespacedPath(filename));
                 ^

Error: The module '\\?\D:\nodesass\win32-x64-72_binding.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 72. This version of Node.js requires
NODE_MODULE_VERSION 64. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:717:18)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at module.exports (D:\xxx\xxx\node_modules\node-sass\lib\binding.js:19:10)
    at Object.<anonymous> (D:\xxx\xxx\node_modules\node-sass\lib\index.js:14:35)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

出现这种情况一般都是node-sass版本和node版本不对应,拿以上错误举例

This version of Node.js requires NODE_MODULE_VERSION 64.

这句话的意思是:此版本的Node.js需要使用64版本的node-sass

可以去GitHub找到node-sass的对应版本:

https://github.com/sass/node-sass/releases

比如说我当前使用的node版本是10.14.1,则找到对应系统的对应node版本

下载对应系统对应node版本所需的node-sass

下载后放到本地磁盘,比如D盘


如果是安装依赖时出了错,则建议先把之前安装的node_modules删掉,然后设置node-sass路径↓↓↓

rmdir /s node_modules
set SASS_BINARY_PATH=D:\work\node-sass\win32-x64-93_binding.node

设置好node-sass路径好正常安装依赖即可

npm i 或 yarn

如果安装依赖成功,但运行时出了node-sass的错,则直接设置node-sass路径然后运行↓↓

npm run dev 或 yarn dev

注意:如果你通过以上方法成功安装了依赖并成功运行了,但是,你在同时维护几个不同node版本的项目,需要切换不同版本的node-sass和node,则建议切换项目后再设置一下node-sass路径。


【补充】mac M1 pro node-sass 安装失败解决方法
说明:经过尝试,发现 node-sass 好像不兼容 m1 ,所以建议将 node-sass 替换为 sass

  1. 删除package.json 中的 node-sass 和 sass-loader
  2. 删除 yarn.log !!!!!! (特别重要)
  3. 删除 node_modules,终端指令 rm -rf node_modules
  4. node 版本切换为 14+
  5. 安装 sass, yarn add -D sass 或 npm install --save-dev sass
  6. 安装依赖 yarn

end~

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐