Error screenshot:

The cause of

Most likely, this is because you downloaded the Node.js installer from the node.js website and installed node.js through it.

NPM does not officially recommend using the Node.js installer to install Node.js.

Because the node.js installation process installs NPM to a local directory where permissions are required, doing so can cause permissions errors when you use NPM to install packages globally.

The solution

NPM officials offer two ways to solve this problem.

  • Re-install your NPM using the Node version tool (recommended)

  • Manually change the default NPM folder

We’re going straight to the official recommendation here.

The best solution

Re-install your NPM using the Node version tool. This is the best way to avoid permissions issues. You don’t need to remove the current version of NPM or Node.js.

We use NVM as a tool to manage Node.js.

Install the NVM

Due to network problems, the installation script provided by the NVM official website is likely to fail, so I recommend that you install NVM manually.

  1. On the cli, enter the following command:

    export NVM_DIR="$HOME/.nvm" && (
      git clone https://github.com/nvm-sh/nvm.git "$NVM_DIR"
      cd "$NVM_DIR"
      git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
    ) && \. "$NVM_DIR/nvm.sh"
    Copy the code
  2. Add the following code to ~/.zshrc:

    Note: This is in order to be able to use NVM after starting the terminal

    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
    Copy the code

    If there is no ~/.zshrc in the user directory (which is the default directory after opening the terminal), then you need to create this file:

    touch .zshrc
    Copy the code

    Then paste the code in, save it, and restart the terminal.

  3. Restart the terminal and enter

    nvm -v
    Copy the code

    If a string of numbers similar to the version number appears in the terminal, then congratulations on the installation

Install Node.js using NVM

Run the following command from the console:

nvm install 12
Copy the code

Note:

  • nvm install nodeYou are advised to download the latest version of Node
  • nvm install 12The number 12 refers to the version number. It is recommended to use an even number. Odd numbers are unstable

When you install Node, NPM is installed.

You can use the NPM –version command to see the version of NPM.

One last point of inquiry

Q: Finally, let’s explore why there are no permissions issues after installation using NVM.

A: Because the node.js path where NVM is installed is not in the path that requires permission

We can type in the terminal

open .nvm
Copy the code

The terminal will open the Finder

We can see that node.js is installed in the user root directory (i.e. ~).

reference

  • Resolving EACCES permissions errors when installing packages globally | npm Docs (npmjs.com)
  • nvm-sh/nvm: Node Version Manager – POSIX-compliant bash script to manage multiple active node.js versions (github.com)