npm i初始化项目node-sass报错 check python checking for Python executable “python2“ in the PATH

文章目录

  • 背景
  • 原因
  • 安装python2
  • 啪的一下,就好了
  • 总结

背景

在本地跑一个前端项目,初始化失败,报错如下:
npm ERR! path D:\work\test\project-v2\node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\windows\system32\cmd.exe /d /s /c C:\Users\shanlt1\AppData\Local\Temp\postinstall-cd1b353a.cmd
npm ERR! Building: D:\tools\nodejs\node.exe D:\work\test\project-v2\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library=
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp verb cli [
npm ERR! gyp verb cli   'D:\\tools\\nodejs\\node.exe',
npm ERR! gyp verb cli   'D:\\work\\test\\project-v2\\node_modules\\node-gyp\\bin\\node-gyp.js',
npm ERR! gyp verb cli   'rebuild',
npm ERR! gyp verb cli   '--verbose',
npm ERR! gyp verb cli   '--libsass_ext=',
npm ERR! gyp verb cli   '--libsass_cflags=',
npm ERR! gyp verb cli   '--libsass_ldflags=',
npm ERR! gyp verb cli   '--libsass_library='
npm ERR! gyp verb cli ]
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | win32 | x64
npm ERR! gyp verb command rebuild []
npm ERR! gyp verb command clean []
npm ERR! gyp verb clean removing "build" directory
npm ERR! gyp verb command configure []
npm ERR! gyp verb check python checking for Python executable "python2" in the PATH
npm ERR! gyp verb `which` failed Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\work\test\project-v2\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\work\test\project-v2\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\work\test\project-v2\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\work\test\project-v2\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\work\test\project-v2\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\work\test\project-v2\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:206:21)
npm ERR! gyp verb `which` failed  python2 Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\work\test\project-v2\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\work\test\project-v2\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\work\test\project-v2\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\work\test\project-v2\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\work\test\project-v2\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\work\test\project-v2\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:206:21) {
npm ERR! gyp verb `which` failed   code: 'ENOENT'
npm ERR! gyp ERR! stack Error: Command failed: D:\tools\python\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack   File "", line 1
npm ERR! gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
npm ERR! gyp ERR! stack SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
npm ERR! gyp ERR! stack
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:400:12)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1093:16)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5)
npm ERR! gyp ERR! System Windows_NT 10.0.19045
npm ERR! gyp ERR! command "D:\\tools\\nodejs\\node.exe" "D:\\work\\test\\project-v2\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd D:\work\test\project-v2\node_modules\node-sass
npm ERR! gyp ERR! node -v v16.17.1
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1

原因

主要原因如下,sass编译需要用到python2,本电脑未安装找不到python2,所以报错,接下来我们安装一下。
npm i初始化项目node-sass报错 check python checking for Python executable “python2“ in the PATH_第1张图片

安装python2

  1. 下载地址 https://www.python.org/download/releases/2.7/ , 下载自己电脑适配的版本,本人电脑64位。
    npm i初始化项目node-sass报错 check python checking for Python executable “python2“ in the PATH_第2张图片
  2. 安装到指定目录
    npm i初始化项目node-sass报错 check python checking for Python executable “python2“ in the PATH_第3张图片
  3. 若是电脑没有python3,配置一下环境变量即可。
    npm i初始化项目node-sass报错 check python checking for Python executable “python2“ in the PATH_第4张图片
  4. 若是安装了python3,python2指令在cmd无效,这时候我们配一下npm指定python的目录,我的安装目录为D盘。
npm config set python D:\tools\python2.7

啪的一下,就好了

npm i 
npm run serve

npm i初始化项目node-sass报错 check python checking for Python executable “python2“ in the PATH_第5张图片

总结

碰到跑新的项目先看看node版本对不对,建议安装一下nvm做node版本控制,安装方法点这里:nvm安装,然后再看报错log,一步一步解决,新项目跑不动基本就是node版本不对,node-sass安装失败,python2没有导致编译失败等环境问题。

你可能感兴趣的:(前端,npm,前端,node.js)