【Windows】【Nodejs】npm installできない??
React で node-sass を使おうと思い、npm install node-sass したらエラーが出ました
どうやら、Windows で Node 環境使用している際によくあるエラーらしい...
とはいえ、インストールできないと積むので、解決していきます
エラー内容
まずは、エラーの内容を確認してみます
npm ERR! gyp verb find Python Python is not set from command line or npm configuration
npm ERR! gyp verb find Python Python is not set from environment variable PYTHON
npm ERR! gyp verb find Python checking if "python3" can be used
npm ERR! gyp verb find Python - executing "python3" to get executable path
npm ERR! gyp verb find Python - "python3" is not in PATH or produced an error
npm ERR! gyp verb find Python checking if "python" can be used
npm ERR! gyp verb find Python - executing "python" to get executable path
npm ERR! gyp verb find Python - executable path is "C:\Users\****\anaconda3\python.exe"
npm ERR! gyp verb find Python - executing "C:\Users\****\anaconda3\python.exe" to get version
npm ERR! gyp verb find Python - version is "3.9.12"
npm ERR! gyp info find Python using Python version 3.9.12 found at "C:\Users\****\anaconda3\python.exe"
(略)
npm ERR! gyp verb find VS msvs_version not set from command line or npm config
npm ERR! gyp verb find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp verb find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
npm ERR! gyp verb find VS looking for Visual Studio 2015
npm ERR! gyp verb find VS - not found
npm ERR! gyp verb find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack at VisualStudioFinder.fail (C:\Users\****\Desktop\my-app-react\node_modules\node-gyp\lib\find-visualstudio.js:122:47)
npm ERR! gyp ERR! stack at C:\Users\****\Desktop\my-app-react\node_modules\node-gyp\lib\find-visualstudio.js:75:16
npm ERR! gyp ERR! stack at VisualStudioFinder.findVisualStudio2013 (C:\Users\****\Desktop\my-app-react\node_modules\node-gyp\lib\find-visualstudio.js:363:14)
npm ERR! gyp ERR! stack at C:\Users\****\Desktop\my-app-react\node_modules\node-gyp\lib\find-visualstudio.js:71:14
npm ERR! gyp ERR! stack at C:\Users\****\Desktop\my-app-react\node_modules\node-gyp\lib\find-visualstudio.js:384:16
npm ERR! gyp ERR! stack at C:\Users\****\Desktop\my-app-react\node_modules\node-gyp\lib\util.js:54:7
npm ERR! gyp ERR! stack at C:\Users\****\Desktop\my-app-react\node_modules\node-gyp\lib\util.js:33:16
npm ERR! gyp ERR! stack at ChildProcess.exithandler (node:child_process:420:5)
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" "C:\\Users\\****\\my-app-react\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd C:\Users\****\Desktop\my-app-react\node_modules\node-sass
npm ERR! gyp ERR! node -v v18.12.1
npm ERR! gyp ERR! node-gyp -v v8.4.1
npm ERR! gyp ERR! not ok
npm ERR! Build failed with error code: 1
Visual Studio が見つからないことが原因みたいです
NodeJs は一部 C ++を使用しているらしく、そのためのコンパイラが必要になるよう。
また、色々調べるとPython3 系もダメみたいで、2 系をインストールする必要があるとのこと。
応急処置をしてみる
PowerShell を管理者権限で実行し、下記のコマンドを実行すると解決する場合が多いらしい
npm install --global --production windows-build-tools
Python や C++のインストールやパスを通すことを勝手にしてくれる→ エラーに出ていた足りないものを補ってくれるとのこと
ひとまず、やってみた
PS C:\Users\*****\Desktop\my-app-react> npm install --global --production windows-build-tools
npm WARN config production Use `--omit=dev` instead.
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated windows-build-tools@5.2.2: Node.js now includes build tools for Windows. You probably no longer need this tool. See https://github.com/felixrieseberg/windows-build-tools for details.
npm ERR! code 1
npm ERR! path C:\Users\*****\AppData\Roaming\npm\node_modules\windows-build-tools
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node ./dist/index.js
Downloading python-2.7.15.amd64.msi
npm ERR! [============================================>] 100.0% of 20.2 MB (13.5 MB/s)
npm ERR! Downloaded python-2.7.15.amd64.msi. Saved to C:\Users\*****\.windows-build-tools\python-2.7.15.amd64.msi.
Downloading vs_BuildTools.exe
npm ERR! [============================================>] 100.0% of 1.12 MB (1.12 MB/s)
npm ERR! Downloaded vs_BuildTools.exe. Saved to C:\Users\*****\.windows-build-tools\vs_BuildTools.exe.
npm ERR!
npm ERR! Starting installation...
npm ERR! Downloading installers failed. Error: TypeError: 'process.env' only accepts a configurable, writable, and enumerable data descriptor
npm ERR! at Function.defineProperty (<anonymous>)
npm ERR! at Object.removePath (C:\Users\*****\AppData\Roaming\npm\node_modules\windows-build-tools\dist\utils\remove-path.js:11:12)
npm ERR! at Object.install (C:\Users\*****\AppData\Roaming\npm\node_modules\windows-build-tools\dist\install\index.js:29:19)
npm ERR! at C:\Users\*****\AppData\Roaming\npm\node_modules\windows-build-tools\dist\start.js:17:19
npm ERR! at Object.download (C:\Users\*****\AppData\Roaming\npm\node_modules\windows-build-tools\dist\download.js:35:5)
npm ERR! at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
npm ERR! at async Object.aquireInstallers (C:\Users\*****\AppData\Roaming\npm\node_modules\windows-build-tools\dist\aquire-installers.js:32:13) {
npm ERR! code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY'
npm ERR! }
npm ERR! windows-build-tools will now exit.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\*****\AppData\Local\npm-cache\_logs\2022-12-18T09_57_44_392Z-debug-0.log
別のエラーが出ました。。。
沼りそうなので、Python と Visual Studio をインストールして、パスを通すことにします
インストールやら、パスを通すやら
Visual Studio Build Tools のインストール
下記 URL からダウンロードしたものを実行(ダブルクリック)します
少し待つとインストーラが起動するので、C ++のビルドツールをインストールします
インストール済みになれば OK! ※今回は「Visual Studio Build Tools 2022」を入れました
Python2.7 をインストール
下記の URL から、「Windows x86-64 MSI installer」をおとします
おとしたファイルを実行します
どんどん「Next」おして、インストールしていきます
npm にパスを通す
インストールが終わったので、それらのパスを npm に通します
// python2.7のパスを通す
PS C:\Users\****> npm config set python python2.7
PS C:\Users\****> npm config get python python2.7
python=python2.7
python2.7=undefined
// Visual Studio Build Tools のパスを通す
// インストールしたバージョンを指定(今回は2022)
PS C:\Users\****> npm config set msvs_version 2022
PS C:\Users\****> npm config get msvs_version
2022
再実行してみる
これで準備が整ったので、もう一回 node-sass をインストールしてみます
C:\Users\***> npm install node-sass
npm WARN deprecated @npmcli/move-file@1.1.2: This functionality has been moved to @npmcli/fs
npm WARN deprecated @npmcli/move-file@2.0.1: This functionality has been moved to @npmcli/fs
added 210 packages, and audited 211 packages in 2s
20 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
いけました!
参考文献
Discussion