Super detailed step-by-step instructions to teach you four solutions to completely solve MAC npm install -g error reporting permission denied

In the daily development process, when we use MAC to execute npm install -g to download the installation package, we often encounter the following error: permission denied

Error details

xxx@CN_C02xxxxx6M ~ % npm install -g yarn
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/yarn
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/yarn'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/xxxxx/.npm/_logs/2022-12-06T11_15_27_852Z-debug-0.log

The solution is as follows:

Solution 1: (for computers with normal permissions)

sudo npm i yarn -g

Solution to the error report 2: (Only under the strict control of computer permissions, the permissions cannot be obtained)

step1: View the npm global file installation address

XXX@CN_CXXXMD6M ~ % npm list -g                    
/usr/local/lib
├── @quasar/[email protected]
├── [email protected]
├── [email protected]
└── [email protected]

step2: From the same directory on other people's computers, copy two files, one is the shortcut file of the command, and the other is the path file pointed to by the environment variable, paste it to the same location on your computer

  • command snapshot file


  • The file pointed to by the global command

step3: Open the environment variable configuration file and configure the environment variable

vim ~/.bash_profile

Paste the following content into the environment variable file,

export PATH=/usr/local/lib/node_modules/yarn/bin/:$PATH

Save the edit of .bash_profile and execute the file

source ~/.bash_profile

Problem solved, you're done

Re-open the command line, you can use yarn

yarn -v

Solution 3: (It is recommended that everyone use this solution)

step1: Open the environment variable configuration file and configure the environment variable

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'

step2: Open the environment variable configuration file and configure the environment variable

vim ~/.bash_profile

Paste the following content into the environment variable file,

export PATH=~/.npm-global/bin:$PATH

Save the edit of .bash_profile and execute the file

source ~/.bash_profile

step3: problem solved, you're done

Re-open the command line, you can use yarn

Solution four:

step1: prerequisite - homebrew has been installed

(If it is not installed, you can open the terminal and enter the following command to install Homebrew)

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

step2: Check the version of brew installed

brew --version 

step3: Use brew to install the packages we need, here we take yarn as an example

brew install yarn

principle

The root cause of this error is that the default path of the npm global installation module does not have permission. Usually, there are several ways to solve it:

  • Modify the permissions of the global installation path (Scheme 1, open permissions)
  • Modify the default installation path (this is what is used in scheme three)
  • Install with other tools (Scheme 4)
  • Other ways (Option 2)

I will write here today~

  • Friends, ( ̄ω ̄( ̄ω ̄〃 ( ̄ω ̄〃)ゝ See you tomorrow~~
  • Everyone be happy every day

Everyone is welcome to point out where the article needs to be corrected~
Learning is endless, cooperation is win-win

insert image description here

Welcome the little brothers and sisters passing by to put forward better opinions~~

Guess you like

Origin blog.csdn.net/tangdou369098655/article/details/129394751