npm global install return and access error (EACCES error) asking for sudo command in order to install global packages, this happens due to the fact that permissions has been wrong setup.
this article applies for macOS ans ZSH users.
The default directory for npm is /usr/local/ you can check in your terminal
npm config get prefix # it should return /usr/local
To check the permissions for node_modules use the following command:
ls -la /usr/local/lib/node_modules # you should get something like this: # total 0 # drwxr-xr-x 4 root 128 ... # drwxr-xr-x 4 root 128 ... # drwxr-xr-x 7 root 128 ... corepack # drwxr-xr-x 13 root 128 ... npm
this means that the only user that can read and write under the folder is root that’s why you need to use the sudo command.
But you can install npm packages without sudo command by changing the default directory to a different one
Before change the current directory we need to create a new one under the home directory:
go to your home directory and to double check in which directory you are, use the command
pwd # return: /Users/cesargomez (home directory)
create a new folder insode the home directory and you can named as you preffer, i will named as npm-global
move to the new npm-global folder by using
setup the new directory to be the npm directory with the following command:
npm config set prefix /Users/user/new_folder # example: npm config set prefix /User/cesargomez/npm-global
now we need to update the system $PATH in this case the zsh Path, so from you home directory open your zsh preferences with VScode
and modifie the like that start with export PATH = … with you new path and add /bin:$PATH at the end like this
the we need to update the zsh variables by typing:
this will fix you permissions setup, now you will be able to install npm global packages without having to use the sudo command.