1. 程式人生 > 其它 >npm install時出現gyp verb `which` failed Error: not found: python2相關錯誤

npm install時出現gyp verb `which` failed Error: not found: python2相關錯誤

從網上下載一個專案,npm install時出現如下錯誤:

npm ERR! code 1
npm ERR! path D:\2022_2_11_clear\sadjkl\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: D:\Program Files\nodejs\node.exe D:\2022_2_11_clear\sadjkl\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:\\Program Files\\nodejs\\node.exe', npm ERR! gyp verb cli 'D:\\2022_2_11_clear\\sadjkl\\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.13.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:\2022_2_11_clear\sadjkl\node_modules\which\which.js:13:12) npm ERR! gyp verb `which` failed at F (D:\2022_2_11_clear\sadjkl\node_modules\which\which.js:68:19) npm ERR! gyp verb `which` failed at E (D:\2022_2_11_clear\sadjkl\node_modules\which\which.js:80:29) npm ERR! gyp verb `which` failed at D:\2022_2_11_clear\sadjkl\node_modules\which\which.js:89:16 npm ERR! gyp verb `which` failed at D:\2022_2_11_clear\sadjkl\node_modules\isexe\index.js:42:5 npm ERR! gyp verb `which` failed at D:\2022_2_11_clear\sadjkl\node_modules\isexe\windows.js:36:5 npm ERR! gyp verb `which` failed at FSReqCallback.oncomplete (node:fs:198:21) npm ERR! gyp verb `which` failed python2 Error: not found: python2 npm ERR! gyp verb `which` failed at getNotFoundError (D:\2022_2_11_clear\sadjkl\node_modules\which\which.js:13:12) npm ERR! gyp verb `which` failed at F (D:\2022_2_11_clear\sadjkl\node_modules\which\which.js:68:19) npm ERR! gyp verb `which` failed at E (D:\2022_2_11_clear\sadjkl\node_modules\which\which.js:80:29) npm ERR! gyp verb `which` failed at D:\2022_2_11_clear\sadjkl\node_modules\which\which.js:89:16 npm ERR! gyp verb `which` failed at D:\2022_2_11_clear\sadjkl\node_modules\isexe\index.js:42:5 npm ERR! gyp verb `which` failed at D:\2022_2_11_clear\sadjkl\node_modules\isexe\windows.js:36:5 npm ERR! gyp verb `which` failed at FSReqCallback.oncomplete (node:fs:198: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 D:\anaconda3\python.EXE npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Command failed: D:\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:397:12) npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:390:28) npm ERR! gyp ERR! stack at maybeClose (node:internal/child_process:1064:16) npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) npm ERR! gyp ERR! System Windows_NT 10.0.14393 npm ERR! gyp ERR! command "D:\\Program Files\\nodejs\\node.exe" "D:\\2022_2_11_clear\\sadjkl\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" npm ERR! gyp ERR! cwd D:\2022_2_11_clear\sadjkl\node_modules\node-sass npm ERR! gyp ERR! node -v v16.13.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 npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\mdzz\AppData\Local\npm-cache\_logs\2022-03-01T13_51_54_809Z-debug.log
文字版錯誤日誌

經過查詢資料,推測是NodeJS和node-sass版本不匹配的問題:

| NodeJS  | Supported node-sass version | Node Module |
|---------|-----------------------------|-------------|
| Node 16 | 6.0+                        | 93          |
| Node 15 | 5.0+                        | 88          |
| Node 14 | 4.14+                       | 83          |
| Node 13 | 4.13+, <5.0                 | 79          |
| Node 12 | 4.12+                       | 72          |
| Node 11 | 4.10+, <5.0                 | 67          |
| Node 10 | 4.9+, <6.0                  | 64          |
| Node 8  | 4.5.3+, <5.0                | 57          |
| Node <8 | <5.0                        | <57         |

執行node --version指令,本機node版本為v16.13.0

檢視專案的package.json,devDependencies中node-sass版本是4.12.0

根據上面的表格,確實存在不匹配。

嘗試辦法①——修改node-sass的版本

根據上表,

npm i node-sass@6.0.1

執行該指令之後,devDependencies中的node-sass版本也被修改為相應版本,繼續執行:

npm install

這次沒有再報錯,繼續執行:

npm run serve

結果報錯:SyntaxError:Error:NodeSassversion6.0.1isincompatiblewith^4.0.0.

詳情如下:

D:\2022_2_11_clear\sadjkl>npm run serve

> unclear@0.1.0 serve
> vue-cli-service serve

 INFO  Starting development server...
98% after emitting CopyPlugin

Failed to compile with 1 error                                                       下午10:56:39
 error  in ./src/App.vue?vue&type=style&index=0&lang=scss&

Syntax Error: Error: Node Sass version 6.0.1 is incompatible with ^4.0.0.


 @ ./node_modules/vue-style-loader??ref--9-oneOf-1-0!./node_modules/css-loader/dist/cjs.js??ref--9-oneOf-1-1!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/src??ref--9-oneOf-1-2!./node_modules/sass-loader/dist/cjs.js??ref--9-oneOf-1-3!./node_modules/cache-loader/dist/cjs.js??ref--1-0!./node_modules/vue-loader/lib??vue-loader-options!./src/App.vue?vue&type=style&index=0&lang=scss& 4:14-416 15:3-20:5 16:22-424
 @ ./src/App.vue?vue&type=style&index=0&lang=scss&
 @ ./src/App.vue
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://172.27.2.205:8080&sockPath=/sockjs-node (webpack)/hot/dev-server.js ./src/main.js

辦法②——npm install --global windows-build-tools

但是根據stackoverflow網友描述:

npm install --global windows-build-tools installs Python 2.7, and installs it globally

也就是該辦法本質上仍然是通過配置python2環境解決問題。執行指令後會全域性安裝python2.7,對於這種全域性安裝沒有詳細描述,這可能會產生一些後果,影響我本來搭建好的python3環境。因此考慮採用其它辦法配置python2環境。

嘗試③——用conda配置python2環境

本地原始環境狀況:已安裝anaconda,預設python版本為3.9(詳見win10按預設步驟安裝Anaconda後各指令狀況&Anaconda配置環境變數

如何用conda配置python2環境:https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html#managing-environments

執行:

conda create --name py2env python=2.7

報錯:

Collecting package metadata (current_repodata.json): failed

CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/main/win-64/current_repodata.json>
Elapsed: -

An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.

If your current network has https://www.anaconda.com blocked, please file
a support request with your network engineering team.

'https://repo.anaconda.com/pkgs/main/win-64'

切換手機熱點後有改善,但還是不行:

於是只能找映象了,先用了清華映象,卡死在開始,後面採用下面這個映象(截至2022-03-02證明有效),才成功。

採用檔案配置映象的辦法,編輯使用者資料夾下的.condarc檔案,把下面的東西替換進去,儲存:

channels:
  - defaults
show_channel_urls: true
default_channels:
  - http://mirrors.aliyun.com/anaconda/pkgs/main
  - http://mirrors.aliyun.com/anaconda/pkgs/r
  - http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
  conda-forge: http://mirrors.aliyun.com/anaconda/cloud
  msys2: http://mirrors.aliyun.com/anaconda/cloud
  bioconda: http://mirrors.aliyun.com/anaconda/cloud
  menpo: http://mirrors.aliyun.com/anaconda/cloud
  pytorch: http://mirrors.aliyun.com/anaconda/cloud
  simpleitk: http://mirrors.aliyun.com/anaconda/cloud

執行:

conda clean -i

重新執行最開始的“建立環境”指令。

測試環境:

C:\Users\mdzz>conda activate py2env

(py2env) C:\Users\mdzz>python --version
Python 2.7.18 :: Anaconda, Inc.

(py2env) C:\Users\mdzz>where python
D:\anaconda3\envs\py2env\python.exe
D:\anaconda3\python.exe

(py2env) C:\Users\mdzz>pip --version
pip 19.3.1 from D:\anaconda3\envs\py2env\lib\site-packages\pip (python 2.7)

(py2env) C:\Users\mdzz>pip list
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Package      Version
------------ -------------------
certifi      2020.6.20
pip          19.3.1
setuptools   44.0.0.post20200106
wheel        0.37.1
wincertstore 0.2

(py2env) C:\Users\mdzz>conda deactivate

x