Install npm packages without sudo

If you run the following command to install npm package:

  1. npm install <package_name>

and get an access error, you might be tempted to run this command with sudo and it'll work, but it's not a right way for sure. Npm modules should be installed without sudo. To fix the problem you need to do two things:

1) Set yourself as an owner of ~/.npm directory, like this:

  1. sudo chown -R $(whoami) ~/.npm

 and if error persisits, set yourself as an owner /usr/local/lib/node_modules directory too, like this:

  1. sudo chown -R $(whoami) /usr/local/lib/node_modules

 Now you can install npm packages without sudo.