'sudo npm install -g npm' fails - node.js

When I try to update npm by sudo npm install -g npm, Ubuntu gives me the error
The OS version is Ubuntu, running under WSL Version 20H2 (OS build 19042.746), I haven't encountered the problem in an older version of Win10.
root#DESKTOP-D03G7FK:~# sudo npm update npm -g
npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser
npm ERR! dest /usr/lib/node_modules/.staging/yargs-parser-b095ed45
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser' -> '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'
npm ERR! [OperationalError: EACCES: permission denied, rename '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser' -> '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'] {
npm ERR! cause: [Error: EACCES: permission denied, rename '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser' -> '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'rename',
npm ERR! path: '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser',
npm ERR! dest: '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'
npm ERR! },
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'rename',
npm ERR! path: '/usr/lib/node_modules/.staging/npm-18aca6f9/node_modules/yargs-parser',
npm ERR! dest: '/usr/lib/node_modules/.staging/yargs-parser-b095ed45'
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! /root/.npm/_logs/2021-02-12T12_53_38_994Z-debug.log
The problem hasn't been solved, I switched to WSL2, since the WSL has the Linux kernel and all the problems were solved, I think it's the bug of WSL.

I had this same problem for the past 2 days but alas, I managed to find a solution.
First, you will want to install nvm. You can find the latest version and download here: nvm for windows
Choose the nvm-setup.zip then proceed and follow the installation process.
After that open your windows terminal then try type nvm -v to see if it successfully installed and have the latest version.
Then if all is good, type nvm install node.
This will update your node to the latest available version.

Use sudo /usr/bin/npm install -g npm to update npm... This will bootstrap using older version of npm that was installed using Ubuntu's package manager.
When you have multiple versions of npm or nodejs installed, always use full path when starting as otherwise you might get errors while running.

Related

EACCES error every time I try and do something with npm

Whenever I do anything related to npm, including uninstalling npm itself, I get this error:
npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/bin/corepack
npm ERR! dest /usr/local/bin/.corepack-0KY6b4OJ
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/bin/corepack' -> '/usr/local/bin/.corepack-0KY6b4OJ'
npm ERR! [Error: EACCES: permission denied, rename '/usr/local/bin/corepack' -> '/usr/local/bin/.corepack-0KY6b4OJ'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'rename',
npm ERR! path: '/usr/local/bin/corepack',
npm ERR! dest: '/usr/local/bin/.corepack-0KY6b4OJ'
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.
This didn't use to be the case as I have had no issues in the past, although the last time I tried to use an npm-related command was about a year ago.
I've tried many solutions, including sudo, nvm and uninstalling Node completely but nothing seems to work. I haven't used Node much before so I'm not very familiar with it unfortunately, so any help would be greatly appreciated.
run npm package as administrator by using the command
sudo npm install -g npm#latest
Turns out it was an issue with my antivirus software! After disabling the software and installing NVM everything now works fine.

How do I install express without an error?

I am trying to install express for a project, but when i run npm i express it shows this error:
npm ERR! code EPERM
npm ERR! syscall symlink
npm ERR! path ../mime/cli.js
npm ERR! dest /media/pi/HMMM/Programming/nodejs/myFirstHTTPS/node_modules/.bin/mime
npm ERR! errno -1
npm ERR! Error: EPERM: operation not permitted, symlink '../mime/cli.js' -> '/media/pi/HMMM/Programming/nodejs/myFirstHTTPS/node_modules/.bin/mime'
npm ERR! [Error: EPERM: operation not permitted, symlink '../mime/cli.js' -> '/media/pi/HMMM/Programming/nodejs/myFirstHTTPS/node_modules/.bin/mime'] {
npm ERR! errno: -1,
npm ERR! code: 'EPERM',
npm ERR! syscall: 'symlink',
npm ERR! path: '../mime/cli.js',
npm ERR! dest: '/media/pi/HMMM/Programming/nodejs/myFirstHTTPS/node_modules/.bin/mime'
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/pi/.npm/_logs/2021-12-31T21_49_55_020Z-debug.log
I am running node and npm on a raspberry pi 4 with the newest release of raspberry pi OS. Please explain what is going wrong and how to fix it
Looks like you're trying to install on your SD card or USB drive, which I assume is formatted as FAT32. Unfortunately, FAT32 doesn't support symlinks.
You could try this:
npm install --no-bin-links express
If that doesn't work, you'll have to use a filesystem other than FAT32.
Seems to be a permission issue.
if you're on mac, run:
sudo npm install express
if you're on windows I am not sure of the equivalent, but you have to run as administrator.

npm ERR! Error: EACCES: permission denied

I'm having this famous issue while trying to install Typescript in my react app. Concretely, I am running this: npm install --save typescript #types/node #types/react #types/react-dom #types/jest
and I am getting this response:
npm ERR! code EACCES npm ERR! syscall rename npm ERR! path
/mnt/c/Users/asier/dev/nftgram/web/node_modules/#types/node npm ERR!
dest
/mnt/c/Users/asier/dev/nftgram/web/node_modules/#types/.node-8MzkXP42
npm ERR! errno -13 npm ERR! Error: EACCES: permission denied, rename
'/mnt/c/Users/asier/dev/nftgram/web/node_modules/#types/node' ->
'/mnt/c/Users/asier/dev/nftgram/web/node_modules/#types/.node-8MzkXP42'
npm ERR! [Error: EACCES: permission denied, rename
'/mnt/c/Users/asier/dev/nftgram/web/node_modules/#types/node' ->
'/mnt/c/Users/asier/dev/nftgram/web/node_modules/#types/.node-8MzkXP42']
{ npm ERR! errno: -13, npm ERR! code: 'EACCES', npm ERR!
syscall: 'rename', npm ERR! path:
'/mnt/c/Users/asier/dev/nftgram/web/node_modules/#types/node', npm
ERR! dest:
'/mnt/c/Users/asier/dev/nftgram/web/node_modules/#types/.node-8MzkXP42'
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/asier/.npm/_logs/2021-04-29T10_48_16_436Z-debug.log
I've checked the ownership of that folder: ls -l/mnt/c/Users/asier/dev/nftgram/web/node_modules/#types/node, and I am the owner of all the files.
I've also tried to install it executing npm set unsafe-perm true
but didn't work.
I've installed npm and node with NVM and I am using Windows 10 with
Ubuntu on WSL.
npm -v: 7.11.1
node -v: v15.14.0
nvm --version: 0.34.0
The weird thing is that I can install other packages with no issues, I just installed react-hook-form for example.
I don't really know which can be the issue and I would be very happy if you guys can give me a clue!
Thank you beforehand for the help! :)
As I saw an error is related to permission. So you need to execute the command with the help of "sudo" like,
sudo npm install --save typescript #types/node #types/react #types/react-dom #types/jest
I had the same problem and the way to fix it was to stop my code that was running (webpack serve). In Windows you can't edit/delete files when those are in use. I think wsl may behave the same as it is a linux subsystem "inside" Windows.

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

npm - EPERM: operation not permitted on Linux

I'm trying to do the react tutorial on my local machine (centOS)
I ran
sudo npm install -g create-react-app
After running that command I got the following error
npm ERR! Linux 3.10.0-514.el7.x86_64
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "-g" "create-react-app"
npm ERR! node v6.10.3
npm ERR! npm v3.10.10
npm ERR! path /tmp/npm-1422-84cdbe70/registry.npmjs.org/tar/-/tar-2.2.1.tgz
npm ERR! code EPERM
npm ERR! errno -1
npm ERR! syscall open
npm ERR! Error: EPERM: operation not permitted, open '/tmp/npm- 1422-84cdbe70/registry.npmjs.org/tar/-/tar-2.2.1.tgz' while getting shasum for /tmp/npm-1422-84cdbe70/registry.npmjs.org/tar/-/tar-2.2.1.tgz
npm ERR! From: https://registry.npmjs.org/tar/-/tar-2.2.1.tgz
npm ERR! at Error (native)
npm ERR! { Error: EPERM: operation not permitted, open '/tmp/npm-1422-84cdbe70/registry.npmjs.org/tar/-/tar-2.2.1.tgz' while getting shasum for /tmp/npm-1422-84cdbe70/registry.npmjs.org/tar/-/tar-2.2.1.tgz
npm ERR! From: https://registry.npmjs.org/tar/-/tar-2.2.1.tgz
npm ERR! at Error (native)
npm ERR! errno: -1,
npm ERR! code: 'EPERM',
npm ERR! syscall: 'open',
npm ERR! path: '/tmp/npm-1422-84cdbe70/registry.npmjs.org/tar/-/tar-2.2.1.tgz',
npm ERR! parent: 'tar-pack' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
I tried going into the directory and untarring the .tgz file and changing around some of the permissions and nothing worked. I followed some solutions online with similar problems but still no luck.
I also ran
npm cache clear
Can anyone explain why this is happening? Any suggestions?
Running npm cache clear or even npm cache clear -fdoes not always clean all the cache.
Sometimes you should remove the cache files manually.
To do so go to the default NPM cache folder and remove its content:
Default: ~/.npm on linux, or %AppData%/npm-cache on Windows.
Then you can attempt to install your desired package with npm install -g.
Please, avoid using sudo npm install -g because you can mess up the npm permissions. Instead change the permission to npm's default directory, or change npm's default directory to another directory. Further details here:
https://docs.npmjs.com/getting-started/fixing-npm-permissions
WARNING: If npm's default path is just /usr, changing the directory permissions will be harmful.
Try to use $: sudo npm install

Resources