Fix your npm Installation on macOS

If you are new to macOS and just started a npm project, one of the case you might meet is that sometimes your npm can successfully install the package, but whenever you call it the terminal can never find it. For example:

$ npm install <packagename>
$       - terminal: installed package
$ <packagename> -v
$       - terminal: package not found 

This is very common for the starters, and there are multiple potential causes to be detected. First of all, you need to check if your package is installed globally or locally.

$ npm list          // gives all local installed packages
$ npm list -g       // gives all global installed packages

If you do notice that one of your package is not installed globally, you can reinstall it with -g flag.

$ npm uninstall <packagename>          
$ npm install <packagename> -g

Afterwards you can try call the package again. But if it still shows like not found, it’s probably your system’s PATH doesn’t get npm binary’s path included. Its also very simple to fix.

$ npm root -g                   // gives directory to your global npm binary, copy this
$ vim ~/.bash_profile           // open config profile of your system and setup PATH for npm binary
$ // within the .bash_profile, add the following line
$ export PATH=$PATH:/<your global npm binary directory>
$ // then save and quit
$ source ~/.bash_profile        // reload your profile to activate

Then if you call the package, it should be there. There’s also a local alternative that you can add the local path to profile by not adding the -g flag when getting the root. The result will be the same for your current project, but if you start a new project, you’ll need to add or change the PATH in .bash_profile again.

本作品采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 (CC BY-NC-ND 4.0) 进行许可。