How to update npm in WSL 2 Ubuntu - node.js

I'm trying to update my ancient npm 3.5.2 that is running in the Windows Subsystem for Linux on my Windows 10 to the latest version.
The command I'm running is as follows:
sudo npm install -g npm#latest
However, it fails and all I'm getting is the following output:
WARN engine npm#7.20.5: wanted: {"node":">=10"} (current: {"node":"8.10.0","npm":"3.5.2"})
/usr/local/lib
└── (empty)
sudo apt install wsl
npm ERR! Linux 5.10.16.3-microsoft-standard-WSL2
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "-g" "npm#latest"atest
npm ERR! node v8.10.0
npm ERR! npm v3.5.2
npm ERR! path /usr/local/lib/node_modules/.staging/#npmcli/ci-detect-c7bf9552
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename
npm ERR! enoent ENOENT: no such file or directory, rename '/usr/local/lib/node_modules/.staging/#npmcli/ci-detect-c7bf9552' -> '/usr/local/lib/node_modules/npm/node_modules/#npmcli/ci-detect'
npm ERR! enoent ENOENT: no such file or directory, rename '/usr/local/lib/node_modules/.staging/#npmcli/ci-detect-c7bf9552' -> '/usr/local/lib/node_modules/npm/node_modules/#npmcli/ci-detect'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /mnt/d/Summbit/Humanatomy.Web/npm-debug.log
npm ERR! code 1
I'm clueless what npm is trying to tell me. Does anyone know what is going on here?

I found this article that detailed the steps that eventually enabled me to update npm. These are the steps that I followed:
Step:
sudo npm cache clean -f
output:
npm WARN using --force I sure hope you know what you are doing.
Step:
sudo npm install -g n
output:
/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
/usr/local/lib
└── n#7.3.1
Step:
sudo n stable
output:
installing : node-v14.17.4
mkdir : /usr/local/n/versions/node/14.17.4
fetch : https://nodejs.org/dist/v14.17.4/node-v14.17.4-linux-x64.tar.xz
installed : v14.17.4 (with npm 6.14.14)
Note: the node command changed location and the old location may be remembered in your current shell.
old : /usr/bin/node
new : /usr/local/bin/node
To reset the command location hash either start a new shell, or execute PATH="$PATH"
Step:
sudo n latest
output:
installing : node-v16.6.1
mkdir : /usr/local/n/versions/node/16.6.1
fetch : https://nodejs.org/dist/v16.6.1/node-v16.6.1-linux-x64.tar.xz
installed : v16.6.1 (with npm 7.20.3)
Step:
Close the shell and open a new one
Step:
npm --version
output:
7.20.3

There's a workaround:
npm install -g yarn
yarn global add npm
~/.yarn/bin/npm install -g npm
Some thing to note:
my nodejs was installed using nvm under ~/.nvm
you need to close vscode if you have it running in wsl mode, and restart bash. some file might be hold by vscode-server process.

Related

updating node and npm from npm 6 to 8 and node 14 to 16 or 18 on a react native project , fails on ENOENT

Getting an error after running npm install,
It seems that npm is not pathed right for this file or something is cached?
I'm using homebrew and nvm to switch between versions, but the only version that works is node 14 and npm 6.
Need to install the following packages:
pod#0.9.0
Ok to proceed? (y) y
npm ERR! syscall chmod
npm ERR! path /Users/me/.npm/_npx/a0ca5f5666585aa2/node_modules/pod/node_modules/pm2/bin/pm2
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, chmod '/Users/me/.npm/_npx/a0ca5f5666585aa2/node_modules/pod/node_modules/pm2/bin/pm2'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/me/.npm/_logs/2022-08-04T19_23_48_147Z-debug-0.log
npm ERR! code 254
npm ERR! path /Users/me/repos/MyApp/MyMobileApp
npm ERR! command failed
npm ERR! command sh /var/folders/d2/rdhvx2fd24vbsfrtf3lddq9m0000gq/T/postinstall-a0c95179.sh
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/me/.npm/_logs/2022-08-04T19_23_45_192Z-debug-0.log
```. i get User defaults from command line:
IDEPackageSupportUseBuiltinSCM = YES
error: Could not delete `/Users/me/repositories/MyApp/ios/build` because it was not created by the build system.
note: To mark this directory as deletable by the build system, run `xattr -w com.apple.xcode.CreatedByBuildSystem true /Users/me/repositories/MyApp/ios/build` when it is created.
error: Could not delete `/Users/me/repositories/MyApp/ios/build` because it was not created by the build system.
note: To mark this directory as deletable by the build system, run `xattr -w com.apple.xcode.CreatedByBuildSystem true /Users/me/repositories/MyApp/ios/build` when it is created.
** CLEAN FAILED **
Is now something that happens when I run xcodebuild -alltargets clean
This may be due to cache. Try below step and check if it works
Run rm -rf ~/.npm
Run npm cache clean --force
Remove node_modules (run rm -rf node_modules)
Remove package-lock.json (run rm -rf package-lock.json)
It happens when npm install conflict with peer dependencies
--legacy-peer-deps that ignores all peer dependencies when installing.
So the best approch is to use --legacy-peer-deps
npm install --legacy-peer-deps

npm i Running command - failed! (exit code 254)

Ionic :3.20.0
node: v8.10.0
npm: 5.6.0
npm i
✖ Running command - failed!
[ERROR] An error occurred while running npm i (exit code 254):
npm ERR! path
/Users/lucaszimmermann/Desktop/myApp/node_modules/tsickle/src/main.js
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall chmod
npm ERR! enoent ENOENT: no such file or directory, chmod
'/Users/lucaszimmermann/Desktop/myApp/node_modules/tsickle/src/main.js'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR!
/Users/lucaszimmermann/.npm/_logs/2018-04-04T16_41_39_270Z-debug.log
I faced a similar issue after doing the below actions this issue solved.
Delete package-lock.json file.
Open cmd/terminal in administrator mode.
run command: npm install
run command: npm update
Node version is likely incompatible. There'd be an expected version actual version. Use nvm or similar to install and change node versions.
Delete node_modules and try install again. You probably get a message similar to:
error ts-jest#26.1.2: The engine "node" is incompatible with this module. Expected version ">= 10.21.0". Got "10.20.1"
error Found incompatible module.
You wan't try to install npm packages.
You have two options to install some packages:
First Option:
npm install <package>
npm i <package>
Second Option:
If you wan't to create an automated installation process without each package, you must provide an packge.json - Here are all depencies listed. You can only run npm i or npm install without an package name, if you have an package.json
A full documentation about that is on NPM: https://docs.npmjs.com/files/package.json

npm broken after installing via brew

So i'm installing node, npm and cordova+ionic on my mac (High Sierra) and it's giving me this issue:
Julime-Air:~ julime$ brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-9.3.0_1.high_sierra.bottle.tar.gz
Already downloaded: /Users/julime/Library/Caches/Homebrew/node-9.3.0_1.high_sierra.bottle.tar.gz
==> Pouring node-9.3.0_1.high_sierra.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
🍺 /usr/local/Cellar/node/9.3.0_1: 5,125 files, 49.6MB
Julime-Air:~ julime$ npm install npm#latest -g
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
/usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
+ npm#5.6.0
updated 1 package in 16.966s
Julime-Air:~ julime$ npm -v
5.6.0
Julime-Air:~ julime$ npm install -g cordova ionic
npm WARN deprecated node-uuid#1.4.8: Use uuid module instead
npm ERR! path /usr/local/lib/node_modules/cordova/node_modules/npm/node_modules/ansistyles
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename
npm ERR! enoent ENOENT: no such file or directory, rename '/usr/local/lib/node_modules/cordova/node_modules/npm/node_modules/ansistyles' -> '/usr/local/lib/node_modules/cordova/node_modules/npm/node_modules/.ansistyles.DELETE'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/julime/.npm/_logs/2017-12-15T22_50_54_958Z-debug.log
tried reinstalling but no luck
Edit:
Running as sudo does not help.
Edit2:
Installing Node via mac installer does also not help.
After having some trouble removing that mac-installer-node, i think this fixed my problem:
sudo chown -R $USER /usr/local
brew doctor
brew link --overwrite node
If you had the same issue, please comment if this helped.

NPM won't install any package on Mac. New, clean build. `EACCES` & other errors

I've just rebuild my Mavericks (Mac OS X Version 9.4) machine from scratch. I am the administrator and only user of this machine.
I installed Git via their git-2.0.1-intel-universal-snow-leopard.pkg (http://git-scm.com/downloads) in their default /usr/local/git/bin/git location.
I installed Node via their node-v0.10.30.pkg (http://nodejs.org/download/) in their default /usr/local/bin/node location.
The final step of Node's installer package included a note that they also installed NPM in their default /usr/local/bin/npm location.
My $PATH echoes /usr/local/git/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin (it contains /usr/local/bin).
Running npm install -g bower results in:
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/bower'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/bower']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/bower',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/bower',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:107:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Darwin 13.3.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "bower"
npm ERR! cwd /Users/Home
npm ERR! node -v v0.10.30
npm ERR! npm -v 1.4.21
npm ERR! path /usr/local/lib/node_modules/bower
npm ERR! fstream_path /usr/local/lib/node_modules/bower
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/bower'
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/Home/npm-debug.log
npm ERR! not ok code 0
Running similar installations (npm install -g yo, npm install -g grunt, npm install -g nvm, npm install -g npm) results in what appears to be the same list of errors.
Several people online suggest forcing the installation via sudo, but several others warn this could cause later issues. The manuals for these packages seem not to mention requirement of sudo, and in some cases, they have advised contacting NPM when this issue arises.
Running npm cache clear does not correct the issue.
Running ls -la in the home (~) directory shows my Mac OS X username as the owner of my .npm directory: drwxr-xr-x 15 admin staff 510 Aug 11 23:53 .npm
Does anyone know what has gone wrong and how to correct this? Thank you.
By the way, I solved this issue by changing the owner of /usr/local to be myself, in order to allow NPM to modules in its default location without further requiring sudo for each package's installation.
sudo chown -R `whoami` /usr/local
EACCES is an error of not having access on doing an operation.
Taken the line: npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/bower' we can see that npm was unable to create a directory in the given location.
The reason is very likely that you are not running the command as super user. In fact, the error log is even suggesting that:
npm ERR! Please try running this command again as root/Administrator.
To run as administrator, you have to prefix those commands with sudo. That is:
sudo npm install -g bower. You will be required to type in a password for security reasons.
to start fresh remove prior node.js and npm installs as well as these :
~/.npmrc
~/.npm
~/tmp
~/.npm-init.js
to install nodejs and npm as yourself NOT root do these commands (linux) :
mkdir ${HOME}/bin
download source from : http://nodejs.org/download/
cd v0.10.30/
./configure --prefix=${HOME}/bin/nodejs
make -j8
make install
which puts it into dir defined by above --prefix
export PATH=${HOME}/bin/nodejs/bin:$PATH
NODE_PATH so node can find dir for modules otherwise
npm install xxx will put newly installed module into dir in curr dir :
export NODE_PATH=${HOME}/bin/nodejs/lib/node_modules
do above AND use syntax : npm install xxxxx -g
always use the -g for global
nodejs install gives you npm as well :
ls -la ${HOME}/bin/nodejs/bin
I'd recommend that you install Node.js using a version manager such as nvm. This way, you kill two birds with one stone:
First, you can manage multiple versions of Node.js on the same machine (which you will want to do sooner or later).
Second, you get around all the access problems you experience when installing Node.js without it. At least that's my experience.
Basically it's as easy as running
$ curl https://raw.githubusercontent.com/creationix/nvm/v0.24.1/install.sh | bash
from the command line.
(SOLUTION ANSWER)
Hi Guys,
I had the same problem on my MacBook Pro 2018. After trie installing any dependency globally (with "-g" or "--global") I did receive an issue "EACCES" error (it's about permissions)
You could simply add "sudo" before your commands what will force it.
For example:
sudo npm install gulp -g
or
sudo npm install browser-sync -g
Enjoy!
It's generally not advised to run sudo unless absolutely necessary.
For issues like this, NPM can solve many issues, and allow multiple node versions to be used, and selected depending upon your use case.
I had a similar issue setting up a new machine, and installing NPM took care of the issue.

How to install jasmine-node (without using 'sudo' ideally)?

So I'm a pretty spoiled rubyist and basically never have to install anything using sudo anymore. I've installed node.js and npm (granted, using the Mac 64-bit .pkg, which could have done gosh knows what on my system) and they work fine.
Now, executing the following:
npm install jasmine-node -g
Doesn't work and says, "Please try running this command again as root/Administrator." which I take to mean sudo npm install jasmine-node -g
Doing: npm install jasmine-node (not globally) works fine, but doesn't setup my path correctly, doesn't run from the command line, etc.
How should I install jasmine-node? cd to /usr/local/bin and install it (without the '-g' option) there? Use homebrew? Or use the sudo ... command I listed above?
Thanks.
/UPDATE: As mentioned above, I installed node.js & npm on my Lion OS X Mac (64-bit) using the universal installer and here are my node/npm listings in /usr/local/bin, in case they're of help:
-rwxr-xr-x 1 24561 wheel 355 Apr 11 2012 /usr/local/bin/node-waf
-rwxr-xr-x 1 24561 wheel 18930304 Oct 25 14:07 /usr/local/bin/node
lrwxr-xr-x 1 root admin 38 Nov 12 10:00 /usr/local/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js
Also, I found this answer, which is one other option (in addition to the three I presented above).
Please let me know which method is recommended (again, ideally without using 'sudo' is my preference).
/END UPDATE
Here is what doing it with -g (globally) returns (not using sudo):
npm http GET https://registry.npmjs.org/jasmine-node
npm http 200 https://registry.npmjs.org/jasmine-node
npm http GET https://registry.npmjs.org/jasmine-node/-/jasmine-node-1.0.26.tgz
npm http 200 https://registry.npmjs.org/jasmine-node/-/jasmine-node-1.0.26.tgz
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/jasmine-node'
npm ERR! { [Error: EACCES, mkdir '/usr/local/lib/node_modules/jasmine-node']
npm ERR! errno: 3,
npm ERR! code: 'EACCES',
npm ERR! path: '/usr/local/lib/node_modules/jasmine-node',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: '/usr/local/lib/node_modules/jasmine-node',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ 'DirWriter._create (/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23)',
npm ERR! '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR! 'Object.oncomplete (fs.js:297:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! System Darwin 11.4.2
npm ERR! command "node" "/usr/local/bin/npm" "install" "jasmine-node" "-g"
npm ERR! cwd /Users/brad/play/troles
npm ERR! node -v v0.8.14
npm ERR! npm -v 1.1.65
npm ERR! path /usr/local/lib/node_modules/jasmine-node
npm ERR! fstream_path /usr/local/lib/node_modules/jasmine-node
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/jasmine-node'
npm ERR! fstream_stack DirWriter._create (/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23)
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:297:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/brad/play/troles/npm-debug.log
npm ERR! not ok code 0
When not using the -g (global install) option, "executables go in ./node_modules/.bin/".
I don't use the -g myself. If you really don't want to install it globally, you can install jasmine-node in your $HOME and add $HOME/node_modules/.bin to the $PATH variable.
I just changed the ownership of all the folders node was concerned with.
sudo chown -R my_account_name /usr/local/lib/node_modules/
sudo chown -R my_account_name /usr/local/lib/node/
sudo chown -R my_account_name /usr/local/include/node/
I don't really know if that's bad practice, but I don't really give a damn.
When you run npm install -g somepackage, you may get an EACCES error asking you to run the command again as root/Administrator. It's a permissions issue.
It's easy to fix, open your terminal (Applications > Utilities > Terminal)
sudo chown -R $USER /usr/local
** I strongly recommend you to not use the package management with sudo (sudo npm -g install something), because you can get some issues later **
Reference: http://foohack.com/2010/08/intro-to-npm/
** Recommended way **
The reason is because this can cause permissions problems with lots of other apps, so I'd suggest not doing this.
A better solution when you are installing in global:
sudo chown -R whoami ~/.npm
So it seems that the node and npm community has finally (in the past year) worked out a method for installing npm and node without needing to use sudo.
Since I'm on Mac OS X, I found these instructions worked for me to uninstall node & npm, then I just used brew to install npm (which automatically gives one node as well). You may need to sudo uninstall, btw, if you sudo'ed to install.
If the above linked instructions don't work, try the top-voted (1500+ votes) answer to do it a bit more manually.
Now having done that, jasmine-node installs just fine w/o sudo, any chowning, or any permissions wrangling. Just run npm install jasmine-node -g and all is well.

Resources