Development Tips
Resolving Permission Errors
EACCES
permission errors can occur when packages are installed globally. If this is the case, npm may need to be set up to operate without elevated permissions.
Using sudo
with npm is not recommended because it can lead to further complications.
This guide offers two options for resolving permission issues. See the npm docs for full documentation and additional options.
Option 1
The best way to avoid permission issues is to reinstall NodeJS and npm using a node version manager.
This guide will document nvm installation and usage. See the nvm docs for full documentation. See the npm docs for additional options and instructions for Windows.
-
Install nvm.
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
-
New terminals will now use nvm. To verify, open a new terminal and run the following. If something prints, the installation was successful.
$ command -v nvm
-
To download and install the latest LTS release of NodeJS, run:
$ nvm install --lts
-
Set the newly installed NodeJS as the default environment:
$ nvm alias default lts/*
-
New terminals will now use the nvm-controlled NodeJS. To verify:
$ node -v # will print the version installed above
$ which npm # will print a path somewhere within the ~/.nvm folder
Global packages will now be installed in the ~/.nvm
directory, so permission errors should no longer occur as long as npm
is used without sudo
.
Option 2
Does not apply to WindowsChange the owner of npm's directories to the current user:
$ sudo chown -R $(whoami) /usr/local/{lib/node_modules,bin,share}
$ sudo chown -R $(whoami) /usr/lib/node_modules
$ sudo chown -R $(whoami) ~/.npm ~/.npmrc
Since these global directories are no longer owned by root
, packages can be installed globally without sudo
.
Updating Dependencies
To update an npm dependency, run the following, where <package-name>
is the package to update:
npm install <package-name>@<version|latest> --save