DEV Community

Cover image for Day 2: Error During npm Installation - Error: `EACCES: permission denied`
Dipak Ahirav
Dipak Ahirav

Posted on

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

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

  1. 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.
  2. 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:

      1. Open or create a ~/.profile file:
        nano ~/.profile
      
   2. Add the following line to the file:
Enter fullscreen mode Exit fullscreen mode
      ```bash
      export PATH=~/.npm-global/bin:$PATH
      ```
Enter fullscreen mode Exit fullscreen mode
   3. Save the file and load the changes:
Enter fullscreen mode Exit fullscreen mode
      ```bash
      source ~/.profile
      ```
Enter fullscreen mode Exit fullscreen mode
 - 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-global\bin`.
   6. Click `OK` to save the changes and close all windows.
   7. Restart your Command Prompt.
Enter fullscreen mode Exit fullscreen mode
  1. 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:

  1. Create a directory for npm global packages:
   mkdir ~/.npm-global
Enter fullscreen mode Exit fullscreen mode
  1. Configure npm to use the new directory path:
   npm config set prefix '~/.npm-global'
Enter fullscreen mode Exit fullscreen mode
  1. 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-global\bin to your system's PATH variable via the Environment Variables settings.
  1. Verify the new setup by installing eslint:
   npm install -g eslint
Enter fullscreen mode Exit fullscreen mode
  1. Check if eslint is installed correctly:
   eslint -v
Enter fullscreen mode Exit fullscreen mode

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:

Happy coding! πŸš€

Top comments (0)