[Node.js]解決npm install 時出現 gyp verb which failed Error: not found: python2 錯誤 === ###### tags: `Node.js` `前端` `nvm` `npm` 今天有新人來詢問為什麼公司Angular教育訓練課程裡的範例程式,照著教學做卻出現ERR。 看了一下原因,範例專案是多年前建立的,而新人電腦內的Node.js版本很新,和範例專案內的node-sass不匹配造成。 錯誤訊息如下 --- ```gherkin= Fnpm ERR! code 1 npm ERR! path D:\Study-Folder\angular-zero\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:\Study-Folder\angular-zero\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:\\Study-Folder\\angular-zero\\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.6.2 npm ERR! gyp info using node@16.13.2 | 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:\Study-Folder\angular-zero\node_modules\which\which.js:13:12) npm ERR! gyp verb `which` failed at F (D:\Study-Folder\angular-zero\node_modules\which\which.js:68:19) npm ERR! gyp verb `which` failed at E (D:\Study-Folder\angular-zero\node_modules\which\which.js:80:29) npm ERR! gyp verb `which` failed at D:\Study-Folder\angular-zero\node_modules\which\which.js:89:16 npm ERR! gyp verb `which` failed at D:\Study-Folder\angular-zero\node_modules\isexe\index.js:42:5 npm ERR! gyp verb `which` failed at D:\Study-Folder\angular-zero\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:\Study-Folder\angular-zero\node_modules\which\which.js:13:12) npm ERR! gyp verb `which` failed at F (D:\Study-Folder\angular-zero\node_modules\which\which.js:68:19) npm ERR! gyp verb `which` failed at E (D:\Study-Folder\angular-zero\node_modules\which\which.js:80:29) npm ERR! gyp verb `which` failed at D:\Study-Folder\angular-zero\node_modules\which\which.js:89:16 npm ERR! gyp verb `which` failed at D:\Study-Folder\angular-zero\node_modules\isexe\index.js:42:5 npm ERR! gyp verb `which` failed at D:\Study-Folder\angular-zero\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 C:\Users\2205001\AppData\Local\Programs\Python\Python39\python.EXE npm ERR! gyp verb could not find "C:\Users\2205011\AppData\Local\Programs\Python\Python39\python.EXE". checking python launcher npm ERR! gyp verb could not find "C:\Users\2205011\AppData\Local\Programs\Python\Python39\python.EXE". guessing location npm ERR! gyp verb ensuring that file exists: C:\Python27\python.exe npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Can't find Python executable "C:\Users\2205011\AppData\Local\Programs\Python\Python39\python.EXE", you can set the PYTHON env variable. npm ERR! gyp ERR! stack at PythonFinder.<anonymous> (D:\Study-Folder\angular-zero\node_modules\node-gyp\lib\configure.js:508:16) npm ERR! gyp ERR! stack at D:\Study-Folder\angular-zero\node_modules\graceful-fs\polyfills.js:284:29 npm ERR! gyp ERR! stack at FSReqCallback.oncomplete (node:fs:198:21) npm ERR! gyp ERR! System Windows_NT 10.0.19044 sass_library=" npm ERR! gyp ERR! cwd D:\Study-Folder\angular-zero\node_modules\node-sass npm ERR! gyp ERR! node -v v16.13.2 npm ERR! gyp ERR! node-gyp -v v3.6.2 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\2205011\AppData\Local\npm-cache\_logs\2022-06-21T02_01_44_054Z-debug.log ``` * 新人電腦安裝的Node.js版本是`v16.13.2` * 範例專案`package-lock.json` 內 node-sass 版本為 `4.7.2` 由 https://www.npmjs.com/package/node-sass 可以查到匹配的版本,再不改動範例專案的情況下,就要將Node.js降到`v8`版本  但是因為同仁專案需求要使用`v16.13.2`,**不能直接降板RRR** ➡️ 那就要使用nvm來管理Node.js的版本!! ## 安裝 NVM 環境 :::info 簡介: NVM 為 Node Version Manager 的縮寫,顧名思義就是「Node 版本管理控制」。 【常用的功能包含】: 1. 切換 Node.js 的版本 1. 使用特定 Node.js 作為預設啟用環境 1. 安裝不同的套件至特定的版本上 ::: 官方gitHub https://github.com/nvm-sh/nvm Windosw 安裝懶人包:https://github.com/coreybutler/nvm-windows/releases 下載`nvm-setup.exe`去安裝即可  然後在 ==命列提示字元(以系統管理員身分執行)== 打上`nvm`指令,有看到以下畫面就是成功安裝了  :::danger 注意:一定要以系統管理員身分執行,沒有最高權限,或是用 VS code 內整合的 PowerShell 就會出現下面錯誤 :::  🔥 常用的 NVM 指令 * nvm install {{版本}}:安裝特定版本的 Node.js * nvm uninstall {{版本}} :刪除指定版本Node.js * nvm ls:列出本定端所安裝的 Node.js 環境 * nvm use {{版本}}:當前命令列套用特定版本的 Node.js 1. 這邊直接於CDM輸入 `nvm install 8.11.3`,下載完畢後輸入 `nvm ls`可以看到目前本機內安裝的Node.js版本有`16.13.2`(使用中)和`8.11.3` 1. CDM輸入 `nvm use 8.11.3` 選擇使用v8.11.3的版本  最後再回到範例專案的資料夾,輸入指令`npm install`就安裝成功,可以再用`npm start`啟動範例專案囉~~~
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.