npm commands not working on Bash on Ubuntu on Windows (WSL) - node.js

I am running latest stable windows with bash on Ubuntu running through cmder wslbridge (i tried running directly through bash but same errors). Moving to my windows based react-native folder and trying to run some cli like npm run android , react-native run-android , gradlew clean is always producing errors;
while surprisingly some other commands like npm start work properly...
which npm print out; /usr/local/bin/npm
i have npm installed on windows and inisde Ubuntu and whereis npm produce this
I am really confused here; when running npm commands inside the bash onto windows folders which npm should be triggered? the windows or WSL one?!
I've tried a lot of suggested solution, but nothing seems to be working...

As you have said you are unsure what is actaully running when you run the commands. if you run the following commands it might make it clear as to what is actually running.
➜ ~ whereis npm
npm: /usr/local/bin/npm /home/damo/.nvm/versions/node/v8.11.1/bin/npm /mnt/c/Program Files/nodejs/npm /mnt/c/Program Files/nodejs/npm.cmd
➜ ~ which npm
/home/damo/.nvm/versions/node/v8.11.1/bin/npm
➜ ~ npm --version
6.4.1
➜ ~ sudo find / -name npm
/home/damo/.npm/10.52.63.39_4783/npm
/home/damo/.npm/npm
/home/damo/.nvm/versions/node/v10.4.1/bin/npm
/home/damo/.nvm/versions/node/v8.11.1/bin/npm
/home/damo/.oh-my-zsh/plugins/npm
/mnt/c/Program Files/nodejs/node_modules/npm
/mnt/c/Program Files/nodejs/node_modules/npm/bin/npm
/mnt/c/Program Files/nodejs/npm
/mnt/c/Users/stangerd/AppData/Roaming/npm
➜ ~ echo $PATH
/home/damo/.nvm/versions/node/v8.11.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files/cmder/vendor/conemu-maximus5/ConEmu/Scripts:/mnt/c/Program Files/cmder/vendor/conemu-maximus5:/mnt/c/Program Files/cmder/vendor/conemu-maximus5/ConEmu:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Windows/System32:/mnt/c/Windows:/mnt/c/Windows/System32/wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0:/mnt/c/HashiCorp/Vagrant/bin:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/nodejs:/mnt/c/Program Files/Beyond Compare 4:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/Program Files/Microsoft VS Code/bin:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Users/ stangerd/AppData/Roaming/npm
➜ ~ nvm use 10
Now using node v10.4.1 (npm v6.1.0)
➜ ~ echo $PATH
/home/damo/.nvm/versions/node/v10.4.1/bin:/usr/local/sbin:/usr/local/bin:........
You can see that for me i also have many versions of npm, but that its my WSLs current nvm version that is being used (shown by running which npm). This is because when the path is searched the first one found will be used. the first instance of npm in all the folders in my path is in /home/damo/.nvm/versions/node/v8.11.1/bin. Notice that when using nvm the path will be changed when switching node versions and that conveniently it puts this new path on the front of all the existing path folders.
Its also worth checking the alias set as this gets evaluated before the path is even queried. Just run alias to get a list.
Hope that this helps to find what is actually running. I'm interested to hear how you get on.

Related

zsh: command not found: npm in the command line

I downloaded nodejs from nodejs.org on my Mac. When I type node in the command line in the terminal I get the following:
davidd.christie#Davids-MacBook-Pro ~ % node
Welcome to Node.js v12.18.0.
Type ".help" for more information.
> .exit
davidd.christie#Davids-MacBook-Pro ~ % ls
Applications Documents Library Music Postman
Desktop Downloads Movies Pictures Public
davidd.christie#Davids-MacBook-Pro ~ % cd Desktop
davidd.christie#Davids-MacBook-Pro Desktop % ls
UBS Candidate Benefits Modeler.pdf my-express-server
intro-to-node node-course
davidd.christie#Davids-MacBook-Pro Desktop % cd intro-to-node
davidd.christie#Davids-MacBook-Pro intro-to-node % npm init
zsh: command not found: npm
davidd.christie#Davids-MacBook-Pro intro-to-node%
It is my understanding that npm automatically downloads once you download node. I tried to initiate npm and got the following message (please see the above code in typed in the command line):
zsh: command not found: npm
I am not sure what to do from here. I have been looking at some of the code on here. I am so confused as to what to do. I am fairly new to coding. Can someone please help and out step by step as to what I need to do? I would be forever grateful.
It depends on how you have installed node. First check the version of your node.
node --version
It should give your node version. In my case it is v10.15.0. Now to find where this node is coming from use command.
which node
It will give you output as /Users/samundra/.nvm/versions/node/v10.15.0/bin/node. It shows that my node is installed at /Users/samundra/.nvm/versions/node/v10.15.0/. Usually npm and node both are placed at same location $NODE_HOME/bin/node, $NODE_HOME/bin/npm. Here $NODE_HOME is /Users/samundra/.nvm/versions/node/v10.15.0.
If we further check where $NODE_HOME/bin/npm by using
ls -al /Users/samundra/.nvm/versions/node/v10.15.0/bin/npm
We find that it's linked to npm-cli.js
lrwxr-xr-x 1 samundra staff 38 May 11 12:08 /Users/samundra/.nvm/versions/node/v10.15.0/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js
So, npm is symlinked to npm-cli.js which can be found at $NODE_HOME/lib/node_modules/npm/bin/npm-cli.js.
With this information now you can figure out whether your node and npm has been installed properly or not. Furthermore, If this is your first time installing node, try closing and re-opening your terminal. Most terminals need to re-initiate sessions after populating environment variables which are used based on installation method that you have used.
I hope this has answered your curiosity. If you feel so then please do not forget to mark it as answer.
Suggestion:
When you feel you have understood how node and npm work. Then you can move to use Node Version Manager (Nvm). It allows you to install multiple versions of node on same machine. You can switch between them using commands like
nvm list: List available node version
nvm use {node_version}: Install Node version and use it
This is quite helpful when you will have to work on different projects that require different version of nodes.
I faced the same issue. Actually you need to edit your .zshrc file
Open .zshrc file - sudo nano ~/.zshrc
Add source $(brew --prefix nvm)/nvm.sh
Save file and restart terminal

`gulp` command doesn't work as expected on Linux

I am trying to set up the needed environment for a project I am gonna work on. I am not able to run gulp command as expected (as it is told on the getting started page of Gulp).
I am using Linux (PopOS) which is based on Ubuntu and Debian.
So, I have tried to fix it in the local project repo - which was unsuccessful.
Then I decided to create a new project directory and see whether it works. I have done these in the new project file:
$ node -v
v8.10.0
$ npm -v
5.6.0
$ npx -v
9.7.1
$ npm install --global gulp-cli
$ npm install --save-dev gulp
After all these, at the last step, when I do:
$ gulp
-bash: /usr/bin/gulp: No such file or directory
this is what I get.
However, if I do this,
$ node_modules/gulp/bin/gulp.js --version
CLI version: 2.2.0
Local version: 4.0.2
I get some result.
Does anybody see why gulp isn't getting in my binary directory? - Even though I have done the same exact steps that are presented on the official website?
This issue was about npm prefix that defines the directory where npm packages will be installed. Because the gulp binary wasn't in one of the directories that is in PATH variable bash wasn't able to find it. Here you can find details about how to fix.

Command not found even thought it is installed on Linux

I recently installed nodejs in a remote centos server following the instruction on https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-a-centos-7-server for installing a package from the node site, since the nodejs that was installed using yum was v 0.something.
The installation went well, and node seems to be working, but only when I execute
/usr/local/bin/node or npm for that matter.
If I try just
node or npm I get the following error:
-bash: /usr/bin/node: No such file or directory
This is a little strange because my PATH does have /usr/local/bin/, and if I do
which node
I get
/usr/local/bin
Here is a copy/paste from the server
[root#v468996 local]# echo $PATH;
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root#v468996 local]# /usr/local/bin/node -v
v11.5.0
[root#v468996 local]# which node
/usr/local/bin/node
[root#v468996 local]# node
-bash: /usr/bin/node: No such file or directory
Note I know I can just make symlink on /usr/bin, so I'm not really asking about that, I want to know why node won't run even though it is installed, and in a location within PATH
As #tink said, this seemed to be the shell remembering the old node location.

cordova is installed but not starting up

I installed node.js 8.9.3 and a successfully installed with following message
This package has installed:
• Node.js v8.9.3 to /usr/local/bin/node
• npm v5.5.1 to /usr/local/bin/npm
Make sure that /usr/local/bin is in your $PATH.
I run following command in terminal mac ...
$ $PATH
And i get following return..
-bash: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin: No such file or directory
Q1. What does that last line mean? No such file or directory?
.. after that i run following commands..
$ which node
And following is response
/usr/local/bin/node
and similar for
$ which npm
And following is response
/usr/local/bin/npm
I think everything is fine till now..
Now i installed ...cordova typing..
$ sudo npm install -g cordova
It is installed and the next lines after completing installation i see ..
/Volumes/macHDD/Users/samz/.npm-packages/bin/cordova -> /Volumes/macHDD/Users/samz/.npm-packages/lib/node_modules/cordova/bin/cordova
+ cordova#8.0.0
updated 1 package in 27.977s
Q2. is it successfully installed? if so.. why is this not as global?
when i test typing
$ which cordova
terminal says nothing.. I also tested typing
$ cordova -version or $ cordova --version .. or even if i type
$ cordova create hello com.example.hello HelloWorld
I am "slapped" by terminal :(
-bash: cordova: command not found
...
in the last night. i was successful in installing cordova, and I created 2 test apps.. i was able edit them, even I run those apps on android emulator and on my ios device+emulator.
during building/preparing one of my app, I tried to create one more app project through a seperate terminal instance. And there I saw .. like no cordova.
I terminated first instance where cordova was working.. and reopened terminal to test if this is an issue.. and yes.. it was .. I could never work on cordova again.
Can somebody tell me what is the issue. ?.
it's very irritating now.
Thank you
I don't know how this is resolved..
I reinstalled node, npm, java, cordova (several times), but nothing could resolve.
and today .. i upgraded node9.3.0 from node8.9.3.. but still the same errors.
Now i had..
Muhammads-MacBook-Pro:colors muhammadfarooqi$ node --version
v9.3.0
Muhammads-MacBook-Pro:colors muhammadfarooqi$ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
Muhammads-MacBook-Pro:colors muhammadfarooqi$ npm -version
5.5.1
Muhammads-MacBook-Pro:colors muhammadfarooqi$ cordova -version
8.0.0
i was about to kill myself.. but i found something on the internet with some other issue.. and i run the same command.
sudo rm -rf node_modules
sudo npm install
and then i installed cordova.
and wow... cordova is there :)
i dont' know what exactly was the solution .. I dont' know what this line of code did.. I know the above line removed node_modules and 2nd lines installed npm.. and now I am seeing cordova is running.
Please somebody, if my solution works for anybody, kindly comment. thank you
Your first issue is because you typed just $PATH. This is then expanded to its value, and then the shell attempts to execute it, which is obviously not possible.
To check your current PATH, type echo $PATH instead.
Your second issue is probably due to a partial configuration to use a user-specific global package directory.
By default, packages are installed in /usr/local/lib/node_modules, with symlinks for binaries in /usr/local/bin, which requires root access (sudo). You have at one point configured npm to install in a different directory (~/.npm-packages), but did not include the bin sub-directory in your PATH.
So you need to either finalise the configuration (see https://docs.npmjs.com/getting-started/fixing-npm-permissions for details), or revert to using /usr/local.

NPM not found when using NVM

I have installed node/npm using the nvm documentation.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash
Then:
nvm install node
At this point node is working but the npm command result with:
npm: command not found
How can I have npm to work correctly ?
I found out that this was a conflict with a previous versions of npm that have not been removed properly despite a apt-get remove node.
I solved it by reinstalling npm from scratch:
rm -R ~/.npm ~/.nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
nvm install node
I found the solution here.
If you run NVM-Windows, don't forget to run nvm on. (this solve the problem as title for me.)
I fixed this by doing this command:
$ command -v npm
and then reopen the shell window.
I solved it by uninstalling all problematic node versions (e.g. v14 below) and reinstalling it.
The problem:
node --version; npm --version;
v14.17.1
Command 'npm' not found, but can be installed with:
sudo apt install npm
The solution:
nvm deactivate
echo "All versions BEFORE:"
nvm_ls
# uninstall all 14.* versions
for v in $(nvm_ls 14); do nvm uninstall $v; done
echo "All versions AFTER:"
nvm_ls
# reinstall version 14
nvm install 14
# and now it has npm too
node --version; npm --version
which node; which npm
# v14.17.1
# 6.14.13
# /home/user/.nvm/versions/node/v14.17.1/bin/node
# /home/user/.nvm/versions/node/v14.17.1/bin/npm
If you use Windows OS, make sure you removed the existing nodejs and npm.
In my case, it worked well after I remove the C:/Program Files/nodejs.
Reference is here.
During nvm installation, make sure the selected path must NOT exist.
This problem especially happens in windows which happens because of missing admin rights for cmd.
If you are using Git bash
Go in installation directory e.g C:\Program Files\Git
Right click properties -> compatibility.
Tick the checkbox with label -> Run as administrator.
Run the git bash again & execute npm list and then npm use 'version_to_be_used'
Same goes for Cmd
One possible reason is the NVM symlink is invalid.
But first, check if both NVM_HOME & NVM_SYMLINK is already set in environment path.
If not, maybe some problem with your nvm installation and u might want to reinstall.
Using explorer, open the symlink folder to check if the folder is valid.Default Symlink path in Windows: C:\Program Files\nodejs. Symlink appears as a normal shortcut in Windows explorer.
If you see node files in there, then you're fine.
If the folder is invalid, delete the symlink.
Then, execute nvm ls and nvm use <desired node version>, this step will re-create the correct symlink.
Restart CMD and test nvm current, node -v, npm -v
For Windows:
nvm creating symlink from installed node path like c:\program files\node to the c:\users<your user>\AppData\nvm<node ver>
So check:
Your basic node path in the PATH variable.
Your npm is inside c:\users<your user>\AppData\nvm<node ver>\nmp and this path is also int the PATH variable.
You could also run
source ~/.bashrc
and try to run again on the same bash terminal where you downloaded the install.sh the command:
npm -v
I went through a similar issue recently and solved it by setting the npm mirror to npm_mirror https://github.com/npm/cli/archive/refs/tags/
The default npm mirror (https://github.com/npm/cli/archive) was a broken link.
so run
nvm npm_mirror https://github.com/npm/cli/archive/refs/tags/
I had the same issue while any new terminal instance started up the message 'npm not found' was shown. I noticed that I had defined (probably) custom paths to npm and node in ~/.bashrc. Deleting them (keeping the paths for nvm) resolved the problem.
This helped me: https://github.com/coreybutler/nvm-windows/issues/548#issuecomment-768297716 Adding quotes to NVM_SYMLINK environment variable: "C:\Program Files\nodejs" instead of C:\Program Files\nodejs.
Install node using node source distribution:
curl -sL https://deb.nodesource.com/setup_[version].x | bash -
apt-get install -y nodejs
[version] = the wanted version. See the repository to choose the correct: NodeSource Node.js Binary Distributions

Resources