How do I fix the npm error EACCES: permission denied? - linux

I tried to start a project with installing npm install -g pnpm as a requirement and I tried npm install but it does work. But npm install -g pnpmshows errors as follow; Below is from the terminal!
npm ERR! code EACCES
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/pnpm/bin/pnpm.cjs
npm ERR! dest /usr/bin/pnpm
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/pnpm/bin/pnpm.cjs' -> '/usr/bin/pnpm'
npm ERR! [OperationalError: EACCES: permission denied, symlink '../lib/node_modules/pnpm/bin/pnpm.cjs' -> '/usr/bin/pnpm'] {
npm ERR! cause: [Error: EACCES: permission denied, symlink '../lib/node_modules/pnpm/bin/pnpm.cjs' -> '/usr/bin/pnpm'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/pnpm/bin/pnpm.cjs',
npm ERR! dest: '/usr/bin/pnpm'
npm ERR! },
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/pnpm/bin/pnpm.cjs',
npm ERR! dest: '/usr/bin/pnpm'
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.

Back up your computer.
On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
To test your new configuration, install a package globally without using sudo:
npm install -g pnpm
source : https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

Related

How can I install NPM?

After writing npm install -g npm I get:
npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/npm
npm ERR! dest /usr/local/lib/node_modules/.npm-i9nnxROI
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -> '/usr/local/lib/node_modules/.npm-i9nnxROI'
npm ERR! [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -> '/usr/local/lib/node_modules/.npm-i9nnxROI'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'rename',
npm ERR! path: '/usr/local/lib/node_modules/npm',
npm ERR! dest: '/usr/local/lib/node_modules/.npm-i9nnxROI'
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/marc/.npm/_logs/2021-12-13T16_06_54_229Z-debug.log
It is a permission issue. If you are on Mac, use:
sudo npm install (whatever your package name is)
and then it will prompt you to type in your password to your computer. Doing this will fix it.
If you are not on Mac, I would find the equivalent of sudo for that.

NPM installation errors

I am trying to create a new project and wanted to install NPM in the directory of my project, but it showed these errors. I currently have the latest versions of both NPM and Node.js.
Naomis-Air:~ naomivictoriaobame$ npm -v
7.24.0
Naomis-Air:~ naomivictoriaobame$ npm install npm#latest -g
npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/npm
npm ERR! dest /usr/local/lib/node_modules/.npm-i9nnxROI
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -> '/usr/local/lib/node_modules/.npm-i9nnxROI'
npm ERR! [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/npm' -> '/usr/local/lib/node_modules/.npm-i9nnxROI'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'rename',
npm ERR! path: '/usr/local/lib/node_modules/npm',
npm ERR! dest: '/usr/local/lib/node_modules/.npm-i9nnxROI'
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/naomivictoriaobame/.npm/_logs/2021-10-16T20_55_50_627Z-debug.log
Naomis-Air:~ naomivictoriaobame$ npm cache clean --force
npm WARN using --force Recommended protections disabled.
Naomis-Air:~ naomivictoriaobame$
It looks as though the current user needs permission to make change to the npm directory. You can run the following command to give the current user access modify that directory:
sudo chown -R $(whoami) ~/.npm

How to npm install in AWS CloudShell?

I'm trying to install an npm package in the new AWS CloudShell (comes with pre-configured Node.js support) - but I'm getting a EACCESS error.
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules'
npm ERR! [Error: EACCES: permission denied, access '/usr/lib/node_modules'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/lib/node_modules'
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! /home/cloudshell-user/.npm/_logs/2021-01-06T02_18_33_584Z-debug.log
What's the best way to install npm packages in AWS CloudShell?
To get around this problem, I followed the instructions at https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally:
First:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
Then:
source ~/.profile

Unable to install npm packages

I have to install npm package hence used command - npm install -g <package-name> but it resulted into write access error so I changed permissions as follows with refernce to https://flaviocopes.com/npm-fix-missing-write-access-error/
sudo chown -R $USER /opt/node-v10.22.0-linux-x64/lib/node_modules/
and tried again but if failed again -
npm ERR! code EACCES
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/requirejs/bin/r.js
npm ERR! dest /opt/node-v10.22.0-linux-x64/bin/r.js
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/requirejs/bin/r.js' -> '/opt/node-v10.22.0-linux-x64/bin/r.js'
npm ERR! { [Error: EACCES: permission denied, symlink '../lib/node_modules/requirejs/bin/r.js' -> '/opt/node-v10.22.0-linux-x64/bin/r.js']
npm ERR! cause:
npm ERR! { Error: EACCES: permission denied, symlink '../lib/node_modules/requirejs/bin/r.js' -> '/opt/node-v10.22.0-linux-x64/bin/r.js'
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/requirejs/bin/r.js',
npm ERR! dest: '/opt/node-v10.22.0-linux-x64/bin/r.js' },
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, symlink \'../lib/node_modules/requirejs/bin/r.js\' -> \'/opt/node-v10.22.0-linux-x64/bin/r.js\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/requirejs/bin/r.js',
npm ERR! dest: '/opt/node-v10.22.0-linux-x64/bin/r.js' }
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.
also when I run command with sudo, it gives npm not found. Please guide what is wrong here.
you need read write permissions to /opt/node-v10.22.0-linux-x64/ as it creates a symlink there for you.
you can also check the destination in error it clearly says you don't have permission there.

Expo CLI issue on ubuntu

I have downloaded expo cli on ubuntu 20.04 in my root directory but I am unable to download it globally!
I want expo cli to be there globally since it's not recommended to open code editor through root directory.
This is what my terminal shows!
npm ERR! code EACCES
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/yarn/bin/yarn.js
npm ERR! dest /usr/local/bin/yarn
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/yarn/bin/yarn.js' -> '/usr/local/bin/yarn'
npm ERR! { [Error: EACCES: permission denied, symlink '../lib/node_modules/yarn/bin/yarn.js' -> '/usr/local/bin/yarn']
npm ERR! cause:
npm ERR! { Error: EACCES: permission denied, symlink '../lib/node_modules/yarn/bin/yarn.js' -> '/usr/local/bin/yarn'
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/yarn/bin/yarn.js',
npm ERR! dest: '/usr/local/bin/yarn' },
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, symlink \'../lib/node_modules/yarn/bin/yarn.js\' -> \'/usr/local/bin/yarn\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'symlink',
npm ERR! path: '../lib/node_modules/yarn/bin/yarn.js',
npm ERR! dest: '/usr/local/bin/yarn' }
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! /home/ritik/.npm/_logs/2020-07-03T19_44_34_737Z-debug.log
This is a permissions issue. you can use sudo to fix this but as a general recommendation never run npm install with sudo because it is a bad idea in general ,you can read more about this here
So To minimize this issue you can configure npm to use a different directory for global packages installations as mentioned in npm docs here
On the command line, In your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
Finnaly try to install expo-cli globally :
npm install -g expo-cli
Or if you want a quick fix you can try this
open a terminal and run :
sudo chown -R $USER:$USER /usr/local/lib/node_modules
try to install expo-cli , run :
npm i -g expo-cli
Also consider to avoid globall installs and use npx when possible read more here

Resources