简体   繁体   中英

Error in installing 'nodemon' using 'npm'

I recently started working with 'npm' and while installing 'nodemon' using command -

npm i -g nodemon

but it is showing ERR!

npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/nodemon
npm ERR! dest /usr/local/lib/node_modules/.nodemon-SfftGed4
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/nodemon' -> '/usr/local/lib/node_modules/.nodemon-SfftGed4'
npm ERR!  [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/nodemon' -> '/usr/local/lib/node_modules/.nodemon-SfftGed4'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/usr/local/lib/node_modules/nodemon',
npm ERR!   dest: '/usr/local/lib/node_modules/.nodemon-SfftGed4'
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/rishabhsingh/.npm/_logs/2023-01-28T12_41_30_475Z-debug-0.log

I tried using sudo to install nodemon, clearing cache using npm cache clear even with --force but no help, also tried what terminal was showing me could be the erroe but turned out nothing helpful.

Be careful with this stuff!!

Don't arbitrarily run sudo commands. Some answers online will tell you to run chmod or chown in your /usr directory. This is not a good solution and may potentially break your whole system!

The problem is that npm is trying to install global packages in your /usr folder, but your user doesn't have access to this folder. What you need to do is tell npm to install global packages in a folder which you do have access to.

  1. Create a directory for global npm packages:

mkdir "${HOME}/.npm-packages"

  1. Tell npm where to store globally installed packagesL

npm config set prefix "${HOME}/.npm-packages"

  1. Ensure npm will find the location by adding it to your system's PATH variable. This can be done by editing .bashrc or .zshrc in your home directory and adding the following code to the bottom of the file.
NPM_PACKAGES="${HOME}/.npm-packages"

export PATH="$PATH:$NPM_PACKAGES/bin"

# Preserve MANPATH if you already defined it somewhere in your config.
# Otherwise, fall back to `manpath` so we can inherit from `/etc/manpath`.
export MANPATH="${MANPATH-$(manpath)}:$NPM_PACKAGES/share/man"
  1. Close and re-open your terminal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM