概要
NestJS でプロジェクト作成した時にエラーが発生したので、その時の解消手順の備忘録。
事象
NestJS のプロジェクト作成をする。
# プロジェクト作成
$ nest new demo-nestjs
▹▹▹▸▹ Installation in progress... ☕
# 途中で失敗
Failed to execute command: npm install --silent
✖ Installation in progress... ☕
🙀 Packages installation failed!
In case you don't see any errors above, consider manually running the failed command npm install to see more details on why it errored out.
Thanks for installing Nest 🙏
Please consider donating to our open collective
to help us maintain this package.
🍷 Donate: https://opencollective.com/nest
エラーメッセージにこう書いてある。
In case you don't see any errors above, consider manually running the failed command npm install to see more details on why it errored out.
👉 訳:『失敗したコマンド npm install を手動で実行して、エラー理由の詳細を直接確認してね』
プロジェクト作成した時に、内部で npm install --silent
してくれてるようなので、じゃあ、プロジェクトの中で自分で npm install
してみる。
# npm インストール
$ npm i
npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /Users/ユーザ名/.npm/_cacache/index-v5/83/20/xxxxx
npm ERR! errno EACCES
npm ERR!
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR!
npm ERR! To permanently fix this problem, please run:
npm ERR! sudo chown -R 501:20 "/Users/ユーザ名/.npm"
npm ERR! A complete log of this run can be found in: /Users/ユーザ名/.npm/_logs/2023-04-11T14_51_42_160Z-debug-0.log
👉 ラストのログの中身でエラー理由が書いてました。
原因
どうやら管理者権限が無いからみたい。
EACCES: permission denied, open '/Users/ユーザ名/.npm/_cacache/index-v5/83/20/xxxxx'
2707 error code EACCES
2708 error syscall open
2709 error path /Users/tommy/.npm/_cacache/index-v5/83/20/xxxxx error errno EACCES
2711 verbose FetchError: Invalid response body while trying to fetch https://registry.npmjs.org/path-scurry: EACCES: permission denied, open '/Users/ユーザ名/.npm/_cacache/index-v5/83/20/xxxxx'
at /Users/ユーザ
解決策・対策
方法 ❶ そもそも自分に root 権限付ける
方法 ❷ 途中まで作成済みのプロジェクトの中で sudo つけて npm インストールする
$ cd demo-nestjs
$ sudo npm i
👉 ③ よりこっちの方が良い
方法 ❸ sudo つけてプロジェクト作成する
$ sudo nest new demo-nestjs
👉 こっちはオススメしない。生成されたソースがすべて管理者権限になって通常コマンドでは編集できなくなるから
方法 ❹ 奇策。一旦 GitHub にソースだけあげて、もっかいローカルにクローンする。
👉 ローカルの環境設定次第なので人による。ただ、実際、私はこれでやった。
上記でうまくいった。
確かに、そもそも Nest CLI を入れる時点で、npm
コマンドに sudo
付けないと入らなかったから怪しいとは思ってた。(npm に管理者権限が無いのは、nodebrew で入れた Node.js だからなのかは不明)