Skip to Content
All memories

npm global on macOS

 — #nodejs#macos

Avoid getting permission errors that ultimately lead you to using sudo in your commands on Mac OS.

What is needed to be done is basically few simple steps:

  1. First, we want to use the official Node.js install. I mean, its official, right? And if we don't trust the Node folks with installing Node, then, well, what are we doing here anyway?
  2. Next we create a folder for the global npm packages. I made mine ~/.npm-packages.
  3. Then we use the npm config command to tell npm where we want them.
  4. Finally, we tell our computer where the paths are to the folder.

That's about it!

How it's done

  • Install Node.js from
  • Update to the latest version of npm npm install npm -g
  • Make a new folder for the npm global packages mkdir ~/.npm-packages
  • Tell npm where to find/store them npm config set prefix ~/.npm-packages
  • Verify the install
node -v
npm -v

Make a folder for your npm packages and tell your computer about it.

mkdir "${HOME}/.npm-packages"
echo NPM_PACKAGES="${HOME}/.npm-packages" >> ${HOME}/.bashrc
echo prefix=${HOME}/.npm-packages >> ${HOME}/.npmrc
echo NODE_PATH=\"\$NPM_PACKAGES/lib/node_modules:\$NODE_PATH\" >> ${HOME}/.bashrc
echo PATH=\"\$NPM_PACKAGES/bin:\$PATH\" >> ${HOME}/.bashrc
echo source "~/.bashrc" >> ${HOME}/.bash_profile
source ~/.bashrc

The instructions above are valid if you are using bash. If you are using zsh instead then .bashrc and .bash_profile should be replaced by .zshrc and .zsh_profile.