背景

本地一个前端项目初始化失败报错如下
npm ERR! path D:worktestproject-v2node_modulesnode-sass
npm ERR! command failed
npm ERR! command C:windowssystem32cmd.exe /d /s /c C:Usersshanlt1AppDataLocalTemppostinstall-cd1b353a.cmd
npm ERR! Building: D:toolsnodejsnode.exe D:worktestproject-v2node_modulesnode-gypbinnode-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 node-gyp@3.8.0
npm ERR! gyp info using node@16.17.1 | 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:worktestproject-v2node_moduleswhichwhich.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:worktestproject-v2node_moduleswhichwhich.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:worktestproject-v2node_moduleswhichwhich.js:80:29)
npm ERR! gyp verb `which` failed     at D:worktestproject-v2node_moduleswhichwhich.js:89:16
npm ERR! gyp verb `which` failed     at D:worktestproject-v2node_modulesisexeindex.js:42:5
npm ERR! gyp verb `which` failed     at D:worktestproject-v2node_modulesisexewindows.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:worktestproject-v2node_moduleswhichwhich.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:worktestproject-v2node_moduleswhichwhich.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:worktestproject-v2node_moduleswhichwhich.js:80:29)
npm ERR! gyp verb `which` failed     at D:worktestproject-v2node_moduleswhichwhich.js:89:16
npm ERR! gyp verb `which` failed     at D:worktestproject-v2node_modulesisexeindex.js:42:5
npm ERR! gyp verb `which` failed     at D:worktestproject-v2node_modulesisexewindows.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:toolspythonpython.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack   File "<string>", 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:worktestproject-v2node_modulesnode-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,所以报错,接下来我们安装一下。
在这里插入图片描述

安装python2

  1. 下载地址 https://www.python.org/download/releases/2.7/ , 下载自己电脑适配版本,本人电脑64位。
    在这里插入图片描述
  2. 安装指定目录
    在这里插入图片描述
  3. 若是电脑没有python3,配置一下环境变量即可
    在这里插入图片描述
  4. 若是安装python3,python2指令cmd无效,这时候我们配一下npm指定python的目录,我的安装目录为D盘。
npm config set python D:toolspython2.7

啪的一下,就好了

npm i 
npm run serve

在这里插入图片描述

总结

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

原文地址:https://blog.csdn.net/littleWhiteSan/article/details/130881408

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_29774.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注