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?

npmのアップデートをしたら、他プロジェクトで動かなくなったので解決した

Posted at

npmのバージョンを最新にしたところ、いくつかの古いプロジェクトでnpmコマンドを打つと以下のエラーがでてしまい一切動かなくなりました。

internal/modules/cjs/loader.js:796
    throw err;
    ^

古いプロジェクトでは、nodeのバージョン10~12系を使っていました。

そもそもサポート外なので使わない方が良いですが、なんとか解決できたので記録を残します。

PC環境について

チップ:Apple M2 Pro
OS: MacOS Sonoma バージョン14.6
nodeのバージョン管理: nodebrew

結果やったこと

エラーの原因

ごうやらnpmが壊れていることが原因っぽい感じだったので、対象のnodeのバージョンをアンインストールして、再度インストールすることに。
しかし、nodeの古いバージョンをインストールできないエラーが出ました。

Python2系をインストールする

M1以降のMacにはPython3系が入っており、nodeの古いバージョンをインストールするにはPython2系が必要みたいです。
なので、Python2系をインストールします。

Pyenvを使うと、Pythonのバージョン管理ができるみたいなので便利です。

Pyenv をインストール: 参照した記事 https://dev.classmethod.jp/articles/pyenv-command-not-found/

$ brew install pyenv

.bash_profile に環境設定を追加

$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

変更を反映

$ source .bash_profile

Pyenvで色々なバージョンをインストールできます。
参照記事 : https://qiita.com/twipg/items/75fc9428e4c33ed429c0
今回は2系が必要なので、Python 2.7.18をインストールします。

$ pyenv install 2.7.18

インストール後、グローバルのバージョンを変更

$ pyenv global 2.7.18

バージョンを確認し、切り替わってたらOK

$ python --version

インストールできない場合は、シンボリックリンクの関連付けが必要みたいです。
参照した記事 https://dev.classmethod.jp/articles/pyenv-command-not-found/#toc-2

シンボリックリンクとは
参照記事:https://s-port.shinwart.com/tech-column/kawatama03/

最後にnodeの古いバージョンをインストールする。

古いバージョンのインストールに、install-binaryとcompileを書く方法がでてきましたが、私はcompileを使うとエラーでインストールできなかったため、install-binaryを使うことで解決できました、

$ nodebrew install-binary v14.20.0
$ nodebrew compile v10.16.3

参考記事

https://dev.classmethod.jp/articles/pyenv-command-not-found/
https://blog.zzzmisa.com/m1-mac-with-node10/
https://envader.plus/course/8/scenario/1076
https://qiita.com/twipg/items/75fc9428e4c33ed429c0
https://qiita.com/nekyo/items/19be05578638fe3e4aec
https://s-port.shinwart.com/tech-column/kawatama03/

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?