node-sass踩坑

前言:

今天一公司某位同学突然需要启动一个很久以前的项目(其实也没多久,只是好久没有这个项目的新需求,所以就尘封了,项目还在运行的)

启动报错了~~

Node Sass could not find a binding for your current environment: Windows 32-bit with Node.js 10.x

这行报错,据我目测这位同学是升级了node到10.x版本然后node-sass不支持。
情况的确是这个情况,之前是使用node8.11.3前一段时间换成了10.15.3
重新安装一下依赖吧

然后我惊呆了

error D:\work\m.dongdao\node_modules\node-sass: Command failed.
Exit code: 1
Command: node scripts/build.js
Arguments:
Directory: D:\work\m.dongdao\node_modules\node-sass
Output:
Building: C:\Program Files (x86)\nodejs\node.exe D:\work\m.dongdao\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 (x86)\\nodejs\\node.exe',
gyp verb cli   'D:\\work\\m.dongdao\\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.15.3 | win32 | ia32
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.7" in the PATH
gyp verb `which` failed Error: not found: python2.7
gyp verb `which` failed     at getNotFoundError (D:\work\m.dongdao\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (D:\work\m.dongdao\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (D:\work\m.dongdao\node_modules\which\which.js:80:29)
gyp verb `which` failed     at D:\work\m.dongdao\node_modules\which\which.js:89:16
gyp verb `which` failed     at D:\work\m.dongdao\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at D:\work\m.dongdao\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:153:21)
gyp verb `which` failed  python2.7 { Error: not found: python2.7
gyp verb `which` failed     at getNotFoundError (D:\work\m.dongdao\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (D:\work\m.dongdao\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (D:\work\m.dongdao\node_modules\which\which.js:80:29)
gyp verb `which` failed     at D:\work\m.dongdao\node_modules\which\which.js:89:16
gyp verb `which` failed     at D:\work\m.dongdao\node_modules\isexe\index.js:42:5
gyp verb `which` failed     at D:\work\m.dongdao\node_modules\isexe\windows.js:36:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:153:21)
gyp verb `which` failed   stack:
gyp verb `which` failed    'Error: not found: python2.7\n    at getNotFoundError (D:\\work\\m.dongdao\\node_modules\\which\\which.js:13:12)\n    at F (D:\\work\\m.dongdao\\node_modules\\which\\which.js:68:19)\n    at E (D:\\work\\m.dongdao\\node_modules\\which\\which.js:80:29)\n    at D:\\work\\m.dongdao\\node_modules\\which\\which.js:89:16\n    at D:\\work\\m.dongdao\\node_modules\\isexe\\index.js:42:5\n    at D:\\work\\m.dongdao\\node_modules\\isexe\\windows.js:36:5\n    at FSReqWrap.oncomplete (fs.js:153:21)',
gyp verb `which` failed   code: 'ENOENT' }
gyp verb could not find "python2.7". checking python launcher
gyp verb could not find "python2.7". 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 "python2.7", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (D:\work\m.dongdao\node_modules\node-gyp\lib\configure.js:484:19)
gyp ERR! stack     at PythonFinder.<anonymous> (D:\work\m.dongdao\node_modules\node-gyp\lib\configure.js:509:16)
gyp ERR! stack     at callback (D:\work\m.dongdao\node_modules\graceful-fs\polyfills.js:295:20)
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:153:21)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "C:\\Program Files (x86)\\nodejs\\node.exe" "D:\\work\\m.dongdao\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd D:\work\m.dongdao\node_modules\node-sass
gyp ERR! node -v v10.15.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
我觉得我需要认真对待了
从报错信息上看
(1)这次安装过程中python2.7没找到
(2)错误都是gyp开头,node-gyp是跨平台调用的node插件,比如可以调用python,c++等
(3)这两个错误导致node-sass安装失败
看起来只要把python2.7安装就可以了

npm install --global --production windows-build-tools

这个包会在电脑上安装三个语言支持
1、python(v2.7 ,3.x不支持);
2、visual C++ Build Tools,或者 (vs2015以上(包含15))
3、.net framework 4.5.1

静静等待····················
静静等待····················
静静等待····················

我环境变量都改了你还找不到,我觉得我可能错怪你了,难道找不到python只是表象?
我决定看一看node-gyp 嗯,这玩意需要运行在node32位的版本上
我的node也不是32位啊,难道我要换一个32位的node?
就在我等待下载node32位安装包的过程中,我觉得这个问题不会只有我遇到过,百度一下,还真不少人遇到!
但每个人使用的node-sass的版本是不一样的,我决定升级一下node-sass试试,现在项目的版本是4.9.0是支持node10.x的,那就直接升级成node-sass@4.13.1这是目前最新的稳定版本了。
嗯~ 干的漂亮,还是安装不上
(事态逐渐失控,我安慰前端同学,别慌)
这个时候做个试验吧,新建一个文件夹

npm init && npm install node-sass@4.13.1 --save-dev

还是不行,这就不是项目里面各个包依赖关系出问题了,而是在node10.15.3下yarn npm 安装node-sass都安装失败
不科学~~~
本着死马当活马医的态度,我又试了一下

cnpm install node-sass@4.13.1 --save-dev

emmmmmmmmmmmmm

cnpm 和 npm 有什么区别

是nodejs 的包管理器,用于node插件管理 ( 包括安装,卸载 ,管理 依赖等 )