背景

在本地跑一个前端项目,初始化失败,报错如下:
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 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:\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 "<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:\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,所以报错,接下来我们安装一下。
在这里插入图片描述

安装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:\tools\python2.7

啪的一下,就好了

npm i 
npm run serve

在这里插入图片描述

总结

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

GitHub 加速计划 / no / node-sass
8.5 K
1.33 K
下载
:rainbow: Node.js bindings to libsass
最近提交(Master分支:23 天前 )
6081731a Bumps [actions/setup-node](https://github.com/actions/setup-node) from 3 to 4. - [Release notes](https://github.com/actions/setup-node/releases) - [Commits](https://github.com/actions/setup-node/compare/v3...v4) --- updated-dependencies: - dependency-name: actions/setup-node dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> 7 个月前
62c0f46c Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v3...v4) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐