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路徑↓↓↓
// 刪除node_modules建議用命令行,更快!!
rmdir /s node_modules
// set SASS_BINARY_PATH=node-sass所在的路徑
set SASS_BINARY_PATH=D:/nodesass/win32-x64-64_binding.node
設置好node-sass路徑好正常安裝依賴即可
npm i 或 yarn
如果安裝依賴成功,但運行時出了node-sass的錯,則直接設置node-sass路徑然后運行↓↓↓
// set SASS_BINARY_PATH=node-sass所在的路徑
set SASS_BINARY_PATH=D:/nodesass/win32-x64-64_binding.node
npm run dev 或 yarn dev
注意:如果你通過以上方法成功安裝了依賴並成功運行了,但是,你在同時維護幾個不同node版本的項目,需要切換不同版本的node-sass和node,則建議切換項目后再設置一下node-sass路徑。
【補充】mac M1 pro node-sass 安裝失敗解決方法
說明:經過嘗試,發現 node-sass 好像不兼容 m1 ,所以建議將 node-sass 替換為 sass
- 刪除package.json 中的 node-sass 和 sass-loader
- 刪除 yarn.log !!!!!! (特別重要)
- 刪除 node_modules,終端指令 rm -rf node_modules
- node 版本切換為 14+
- 安裝 sass, yarn add -D sass 或 npm install --save-dev sass
- 安裝依賴 yarn
end~