Day 2: Error During npm Installation - Error: `EACCES: permission denied`
Tags: node, webdev, learning
Published: 2024-07-04 12:07:48

**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](https://www.youtube.com/@DevDivewithDipak?sub_confirmation=1
) 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:
     ```bash
     mkdir ~/.npm-global
     ```
   - **Step 2: Configure npm to use the new directory path**:
     ```bash
     npm config set prefix '~/.npm-global'
     ```
   - **Step 3: Update your system's PATH variable**:
     - For macOS/Linux:
       1. Open or create a `~/.profile` file:
          ```bash
          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-global\bin`.
       6. Click `OK` to save the changes and close all windows.
       7. Restart your Command Prompt.

3. **Verify the new setup**:
   - Try installing a package globally without `sudo`:
     ```bash
     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:
   ```bash
   mkdir ~/.npm-global
   ```

2. Configure npm to use the new directory path:
   ```bash
   npm config set prefix '~/.npm-global'
   ```

3. Update your system's PATH variable:
   - For macOS/Linux:
     ```bash
     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.

4. Verify the new setup by installing `eslint`:
   ```bash
   npm install -g eslint
   ```

5. Check if `eslint` is installed correctly:
   ```bash
   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](https://www.youtube.com/@DevDivewithDipak?sub_confirmation=1
) to support my channel and get more web development tutorials.

### Follow and Subscribe:
- **Website**: [Dipak Ahirav] (https://www.dipakahirav.com)
- **Email**: dipaksahirav@gmail.com
- **YouTube**: [devDive with Dipak](https://www.youtube.com/@DevDivewithDipak?sub_confirmation=1
)
- **LinkedIn**: [Dipak Ahirav](https://www.linkedin.com/in/dipak-ahirav-606bba128)
- **Instagram**: [devdivewithdipak](https://www.instagram.com/devdivewithdipak)


Happy coding! 🚀