node-sass模块在安装过程中调用python出现语法错误, 怎么解决?

2023-10-07 14:06:47 浏览数 (3087)

微信截图_20231007140617

node-sass模块在安装过程中调用python出现语法错误:

npm ERR! code 1
npm ERR! path D:\app\xxd-en\node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node scripts/build.js
npm ERR! Building: C:\Program Files\nodejs\node.exe D:\app\xxd-en\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   'C:\\Program Files\\nodejs\\node.exe',
npm ERR! gyp verb cli   'D:\\app\\xxd-en\\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@18.15.0 | 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:\app\xxd-en\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\app\xxd-en\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\app\xxd-en\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\app\xxd-en\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\app\xxd-en\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\app\xxd-en\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:208:21)
npm ERR! gyp verb `which` failed  python2 Error: not found: python2
npm ERR! gyp verb `which` failed     at getNotFoundError (D:\app\xxd-en\node_modules\which\which.js:13:12)
npm ERR! gyp verb `which` failed     at F (D:\app\xxd-en\node_modules\which\which.js:68:19)
npm ERR! gyp verb `which` failed     at E (D:\app\xxd-en\node_modules\which\which.js:80:29)
npm ERR! gyp verb `which` failed     at D:\app\xxd-en\node_modules\which\which.js:89:16
npm ERR! gyp verb `which` failed     at D:\app\xxd-en\node_modules\isexe\index.js:42:5
npm ERR! gyp verb `which` failed     at D:\app\xxd-en\node_modules\isexe\windows.js:36:5
npm ERR! gyp verb `which` failed     at FSReqCallback.oncomplete (node:fs:208:21) {
npm ERR! gyp verb `which` failed   code: 'ENOENT'
npm ERR! gyp verb `which` failed }
npm ERR! gyp verb check python checking for Python executable "python" in the PATH
npm ERR! gyp verb `which` succeeded python C:\Users\42538\anaconda3\python.EXE
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Command failed: C:\Users\42538\anaconda3\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: invalid syntax
npm ERR! gyp ERR! stack 
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:419:12)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1091:16)
npm ERR! gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:302:5)
npm ERR! gyp ERR! System Windows_NT 10.0.19045
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\app\\xxd-en\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd D:\app\xxd-en\node_modules\node-sass
npm ERR! gyp ERR! node -v v18.15.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1


可以尝试以下几种方法:

1. 升级node-sass到最新版本,新版本移除了对python 2的依赖。

npm uninstall node-sass
npm install node-sass

2. 如果必须使用旧版本node-sass,可以单独安装python 2,并确保将其路径添加到PATH环境变量中。

3. 在安装node-sass时指定python 2的路径:

npm install node-sass --python=python2

4. 使用node-gyp自定义编译参数,跳过执行python脚本:

npm install --node-gyp-norequire

总体来说,建议尽可能升级到最新版本的node-sass,这样可以避免与python 2的兼容问题。

如果必须使用旧版本,可以尝试指定python 2的路径或者自定义编译参数。