LoginSignup
5
4

More than 3 years have passed since last update.

WSL2 + Ubuntu 20.04 で npm実行時「bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory」と出た際の対応

Last updated at Posted at 2021-03-09

初投稿になります、よろしくお願いします。

環境

wsl2
Ubuntu20.04

状況

npm install

を実行後
bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory
のエラーに遭遇

原因

windowsのPATH(/mnt/~)がubuntuに取り込まれ、元々windowsにインストールされたnpmが実行されようとしたことが原因

対処法

https://hackmd.io/@badging/wsl2#Troubleshooting-PATH より引用

~/.bashrc に以下を追加

.bashrc
PATH=$(echo "$PATH" | sed -e 's/:\/mnt.*//g') # PATH変数からwindows側のPATHを取り除く

追加出来たら再度ログインし、windowsからインポートされたPATHが無いことを確認。

echo $PATH

追記

/etc/wsl.conf 内にappendWindowsPath=falseを記述することでPATH環境変数を引き継がないように出来ることを知りました。

/etc/wsl.conf
[interop]
appendWindowsPath=true

後談

対処したのちqiitaに投稿しようと検証で一度.bashrcを編集前に戻したが、症状が出ず…。
色々試していた中でbashがキャッシュを保持してることを知り削除したことが再現できなかった理由かもしれません。
そもそも.bashrcに書かなくてもキャッシュを削除してれば解決していた可能性も有り。

PATHのキャッシュ削除
hash -r

参考

https://qiita.com/antimalware_service_executable/items/89a620b3e6728ddc48a4
https://stupiddog.jp/note/archives/951
https://github.com/microsoft/WSL/issues/1890

5
4
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
5
4