0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel Breeze(Vue)導入時に Ubuntu(Sail)で npm install が権限エラー → Windowsでは成功した話と原因・対策まとめ【環境差トラブル備忘録】

Last updated at Posted at 2025-03-28

はじめに

Laravel Breeze で Vue を使った認証機能を導入しようとした際、Ubuntu(Laravel Sail)上で npm install を実行すると、謎の権限エラーが発生

ところが、同じコマンドを Windows 環境で実行すると何の問題もなく成功

この体験から、ホストOSとDockerコンテナ間のファイル・権限の違いについて学んだことが多かったので、備忘録としてまとめておきます。


実行したコマンド一覧

# Breezeパッケージのインストール
./vendor/bin/sail composer require laravel/breeze --dev

# Vue版の Breeze をインストール
./vendor/bin/sail artisan breeze:install vue

# フロントエンドの依存パッケージインストール
./vendor/bin/sail npm install

# 開発用ビルド
./vendor/bin/sail npm run dev

①Ubuntu(Sail)環境で発生したエラー

npm install 実行時に、以下のようなエラーが大量に表示されました。

npm ERR! code EPERM
npm ERR! syscall chmod
npm ERR! path /var/www/html/node_modules/◯◯
npm ERR! Error: EPERM: operation not permitted, chmod ...
npm WARN cleanup EACCES: permission denied, rmdir ...

②しかしWindows環境では成功

同じプロジェクトフォルダを Windows 側で開いて、VSCodeのターミナルで以下を実行:

npm install
npm run dev

→エラーなしで成功

③原因の考察:ホストOSとDockerの権限の違い

結論:

  • node_modules がホスト(Windows)と Docker(Ubuntu)間で共有され、権限エラーが発生していた

詳細:
IMG_7733.jpeg

対策1: dockerignore に node_modules を追加する(推奨)

# .dockerignore に追記
node_modules

これにより、ホストとコンテナの間でnode_modules が共有されなくなり、Docker内でのみ管理されるようになる。

# 再実行
rm -rf node_modules package-lock.json
./vendor/bin/sail npm install
./vendor/bin/sail npm run dev

対策2:ファイル所有者の修正(応急処置)

./vendor/bin/sail shell

# コンテナ内で実行
chown -R sail:sail /var/www/html

権限が root などになっていると npm install 時に書き込めないため、sailユーザーに所有権を戻す。

対策3:npm install をホスト側(Windows)で行う

npm install
npm run dev

Vue側のビルドは、Laravel本体とは別にホストOS上で完結させる方法もあり。

④教訓:Sail(Docker)+npm は環境差に要注意!

IMG_7734.jpeg

⑤まとめ

  • npm install は意外とOSとファイルシステムの影響を受けやすい
  • Laravel Breeze + Vue を Docker(Sail)で使うなら、権限・マウント・ignore設定をしっかり意識する
  • .dockerignoreに node_modules を入れるのは地味だけど超重要!
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?