Day 2: Error During npm Installation – Error: `EACCES: permission denied`

RMAG news

Cause: This error occurs due to permission issues when trying to install npm packages globally. It often happens because npm tries to write to directories it does not have permission to access.

please subscribe to my YouTube channel to support my channel and get more web development tutorials.

Solution:

Step-by-Step Solution

Avoid using sudo for npm install:

Instead of using sudo npm install -g <package>, which can cause security issues and permission problems, it’s better to change the npm default directory to a location where your user account has write permissions.

Change npm Default Directory:

Step 1: Create a directory for npm global packages:
Open your terminal and run:

mkdir ~/.npm-global

Step 2: Configure npm to use the new directory path:

npm config set prefix ‘~/.npm-global’

Step 3: Update your system’s PATH variable:

For macOS/Linux:

Open or create a ~/.profile file:

nano ~/.profile

2. Add the following line to the file:
“`bash
export PATH=~/.npm-global/bin:$PATH
“`
3. Save the file and load the changes:
“`bash
source ~/.profile
“`
– For Windows:
1. Open the Start menu and search for `Environment Variables`.
2. Click on `Edit the system environment variables`.
3. In the System Properties window, click on the `Environment Variables` button.
4. Under `User variables`, find the `Path` variable and click `Edit`.
5. Click `New` and add the path to the new npm global directory. For example, `C:Users<YourUsername>.npm-globalbin`.
6. Click `OK` to save the changes and close all windows.
7. Restart your Command Prompt.

Verify the new setup:

Try installing a package globally without sudo:

npm install -g <package>

This should work without permission errors. You can test this by installing a common package, like npm install -g eslint.

Example

Let’s go through an example of installing the eslint package globally:

Create a directory for npm global packages:

mkdir ~/.npm-global

Configure npm to use the new directory path:

npm config set prefix ‘~/.npm-global’

Update your system’s PATH variable:

For macOS/Linux:

echo ‘export PATH=~/.npm-global/bin:$PATH’ >> ~/.profile
source ~/.profile

For Windows:

Add C:Users<YourUsername>.npm-globalbin to your system’s PATH variable via the Environment Variables settings.

Verify the new setup by installing eslint:

npm install -g eslint

Check if eslint is installed correctly:

eslint -v

This should display the installed version of eslint, indicating that the package has been installed successfully without any permission errors.

This solution will ensure you avoid permission errors during npm installations and maintain a secure environment for your Node.js development.

please subscribe to my YouTube channel to support my channel and get more web development tutorials.

Follow and Subscribe:

Website: Dipak Ahirav

Email: dipaksahirav@gmail.com

YouTube: devDive with Dipak

LinkedIn: Dipak Ahirav

Instagram: devdivewithdipak

Happy coding! 🚀