How to fix checkPermissions Missing write access to /usr/local/lib/node_modules error ?

If you are working in a Node JS environment and without root access or sudo access, you might get an error as checkPermissions Missing write access to /usr/local/lib/node_modules like below

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules'
npm ERR! }
npm ERR!

one solution to this problem is changing the permission of the folder /usr/local/lib/node_modules for the current user, but thats not recommended and what if its not in your hands to change the permission.
One thing we can do to address this problem is modify the location where nodejs can store modules, for your user account.
Follow below steps in given order to configure your node_modules folder for your user account with out harming others!!

# Create a directory named npm-global as hidden in your home directory
mkdir ~/.npm-global

# Then set it as your node dir 
npm config set prefix '~/.npm-global'

# Update Path in your .bashrc or .bash_profile or .profile files as below
export PATH="~/.npm-global/bin:$PATH"

# once saved load the changes
source ~/.bashrc

And thats it, we are good. Now you can install any module and you wont be getting any permission related error as above.
Hope it helps.

0 comments:

Post a Comment