【node】グローバルでインストールしたパッケージをnodeで使えるようにする【パスの通し方】
流れ
- npm グローバルインストール先を確認する
- nodeコマンド実行時に読み込むパスの一覧を確認する
- パスを通すための設定を行う
1. npm グローバルインストール先を確認する
$npm root -g
>
/Users/xxxxxxx/.npm-global/lib/node_modules
続いて、以下を入力。
2. nodeコマンド実行時に読み込むパスの一覧を確認する
$node
>
global.module.paths
<実行結果>
% node
Welcome to Node.js v18.16.0. Type ".help" for more information. global.module.paths [ '/Users/xxxxxxx/work/study/TIL/repl/node_modules', '/Users/xxxxxxx/work/study/TIL/node_modules', '/Users/xxxxxxx/work/study/node_modules', '/Users/xxxxxxx/work/node_modules', '/Users/xxxxxxx/node_modules', '/Users/node_modules', '/node_modules', '/Users/xxxxxxx/.node_modules', '/Users/xxxxxxx/.node_libraries', '/usr/local/lib/node' ]
先ほど、確認した
/Users/xxxxxxx/.npm-global/lib/node_modules
が上述の一覧にないことを確認する。
一覧になければパスが通っていないことになるため、次にパスを通すための設定を行っていく!
3. パスを通すための設定を行う
設定ファイルを編集してPATH/環境変数を加えればok.
3-1. zshのmacでPATHを通す
open ~/.zshrc
3-2. 通したいパスを設定する
→export PATH=通したいパス:$PATH
export NODE_PATH="/Users/kurino/.npm-global/lib/node_modules"
3-3. 変更したPATHを反映させる
source ~/.zshrc
以上です!
参考リンク グローバルインストールしたパッケージがnodeで使えない時の対処方法↓
グローバルインストールしたパッケージがnodeで使えない時の対処法 | tech-broccoli.life
パスの設定方法(mac)↓