Unable to install npm global packages

Sometimes when you try to install global packages in npm , you might came across following error message as in screenshot

EACCES: permission denied, mkdir ‘/userdirectory/node_modules/<package you are trying to install>’

Now we can easily solve this issue by following below commands

  1. mkdir ~/.npm-global
  2. npm config set prefix ‘~/.npm-global’
  3. export PATH=~/.npm-global/bin:$PATH
  4. source ~/.profile
  5. npm install -g jshint

Lets understand what it does

  1. On the command line, in your home directory, create a directory for global installations
  2. Configure npm to use the new directory path
  3. In your preferred text editor, open or create a ~/.profile file
  4. On the command line, update your system variables
  5. Try to do it without sudo, it will work like charm

Source

--

--