NPM command not found after installing Node via PKG (no brew) - node.js

I tried to remove NPM and Node and reinstall them both. I'm a complete noob in the process as you'll notice.
I sadly can't remember the commands I used.
After I removed them, I ran node -v and it returned command not found and I ran npm -v and it returned command not found, so I thought the process was successful.
I then went to Nodejs website and downloaded the latest package for MacOS (v12.11.1) and ran the install. It said it was successful. I check node -v and it returned 12.11.1, as expected, but then I checked npm -v and it sadly returned npm not a command!!
If I run which node I get /usr/local/bin/node. If I run which npm I get nothing returned.
I've since tried lots of ways of removing them both again just in case something was missed but I get the same thing every time - no NPM!
Most of the answers on StackOverflow talk about Brew but I have not used Brew and I have tried so many of the other answers but just can't get NPM back.
The PKG installer mentions this: 'Make sure that /usr/local/bin is in your $PATH.' but not entirely sure what that means and if that's my problem or not.
Does anybody know a surefire way to remove all traces of both, so when I reinstall the PKG from Node, I get both Node and NPM installed and working correctly?
UPDATE:
I seem to have had some success since posting this question. I followed instructions at https://docs.npmjs.com/misc/removing-npm.html to manually remove 'everything NPM-rleated' and ran the installer again. Now when I check npm -v it returns to the latest version number 6.11.3.
I will test things a bit more before deleting/answering my own question.

To fix this problem I ran the following command as suggested by NPM:
To remove everything npm-related manually:
rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*
All was OK after I reinstalled Node and checked NPM's version using npm -v.

Googling 'Adding to $PATH mac' returns this as the top result:
https://www.architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/
Open up Terminal and run the following command:
sudo nano /etc/paths
Enter your password, when prompted.
Go to the bottom of the file, and enter the path you wish to add.
Hit control-x to quit.
Enter “Y” to save the modified buffer.
That’s it! To test it, in new terminal window, type:
echo $PATH

this is what I did on mac
rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/npm*
brew uninstall node
brew install node

Related

Where is the npm directory supposed to be?

The Node version is 17.7.1. When I run npm -v I get:
bash: /usr/local/bin/npm: No such file or directory
But when I run npm root -g I get:
/usr/local/lib/node_modules
Running npm list -g also gives me the same message as running npm -v.
So, where is it supposed to be and how can I fix this?
There are a few issues as to why this is occuring.
It appears that your computer cannot find the following path (below) to the npm folder, but it can find the global node_modules folder.
/usr/local/bin/npm
This may occur for a number of reasons. Luckily, there are many solutions to these issues.
1. Reinstall Node.js and npm^
The first solution is pretty obvious, and it is to reinstall Node.js and npm^.
The installation process is very simple.
If you are using only the command line, you can install Node.js using the following command. The npm command comes installed with Node.js.
$ sudo apt-get install nodejs
Or, if you want to use the installer, download and run the installer from nodejs.org.
Check the versions of Node.js and npm to see if it has worked.
$ node -v
$ npm -v
The helpful thing about reinstalling Node.js and npm in this case is that, if Node.js and npm weren't installed in the first place (highly unlikely), it will install them.
However, if they were already installed, but then something happened to the installation directory (e.g. it was renamed, moved, deleted or corrupted), then the new installation will most likely have fixed that issue.
2. Add /usr/local/bin/npm to the path (solution worked for OP)
If the first solution didn't work for you, then the next issue that might be occurring might be that /usr/local/bin/npm isn't in your path, which means that bash cannot find it.
To add npm to your path, follow the steps below.
Normally, to get the path with npm, you need to run the following command.
$ npm bin
Obviously, we have to use a workaround. But, we can just use an assumed path for where the binaries of npm are stored, which is...
/usr/local/bin/npm/bin
However, this may vary, so, to be sure that this exists, you can simply type it into the command line.
$ cd /usr/local/bin/npm
Then, you can run the ls command, and see if there are any bin folders or .sh shell scripts that might be used for the path.
$ ls
Make sure you save the binary part for later!
Open the home directory using the following command. This is because the .bashrc file is in the home directory (used in the next step).
$ cd $HOME
Open the .bashrc file using vi (or any file editor of your choice, but vi will be used in this example).
$ vi .bashrc
Then, press the i key for Insert Mode. Type the following in the file.
export PATH=/usr/local/bin/npm/bin:$PATH
In vi, press Esc, then press the following keys: :, w, q, !, Enter.
:wq!
The file is now saved and the editor is closed.
Normally, the .bashrc file is only read when you log in each time. To avoid doing that, you can force bash to read the .bashrc file and update itself. To do this, run the following command.
$ source .bashrc
The bash shell should now be restarted.
These were quite a few steps (four, but many were compressed into one), but if a situation occurred where for some reason npm wasn't adding itself to the path, this solution should fix it.
3. Denied read/write access
Another issue is that your user profile doesn't have the right permissions to read/write to usr/bin/local/npm, or you didn't give npm permissions.
The fix to this solution is very simple.
Run the npm commands you want to run using sudo.
$ sudo npm -v
$ sudo npm list -g
If you want, you can make the commands you run automatically run with sudo.
$ sudo -s
$ npm -v
$ npm list -g
Warning: If you don't have the password for sudo, you can't run any of the commands using sudo above.
This should fix your issue, if denied read/write access is the problem.
There are most likely many more solutions^^, but they might be added later.
^ This is a very usual process, but it has to be added here so that I can be sure that this solution has been tried.
^^ These solutions may be found later and edited to this answer. Edited once.
use this command :
npm list -g
you should see your npm directory at first line

What does the npx command install?

This question is about understanding how the npx command works and what it exactly does under the hood. (The documentation does not really help - see below)
From the documentation:
Installation-less command execution
There is another great feature of npx, which is allowing to run
commands without first installing them.
Great. Let's try running some command without installation:
npx node#6 -v
And in result one gets:
Need to install the following packages:
node#6
Ok to proceed? (y)
So there is anyway installation going on. And one could see that something gets installed with npm cache ls.
If we run the command npx node#6 -v again, there won't be a prompt asking for installation. That means the installation (that didn't happen in the first place) persists. And now one could execute the command even without connection to the internet/npm.
For a person who does not know all nuts and bolts of the npm it is kind of strange to find "installation-less execution", which asks for installation.
Could some one give more logical explanation like: when you execute npx node#6 -v this and that happens under the hood, this package will be downloaded and unpacked there ... then executed ... then deleted/or persists for so long ... and so on.
What if I run npx package today (and it gets "installed") and I run the command in 3 months? Would npx check if there is a newer version or would it call the "installed" one?
Is there a documentation on that?

How to fully delete a NPM package from a mac?

I have installed a tool called eas-cli from Expo.
I have ran all the commands I could think of, and even went to the root directory of npm and deleted the folder of it, but I still see results when I type eas --version in terminal. How can I fully delete this tool from my mac? is there a directory im not looking at ?
npm uninstall -g eas should uninstall the item from your path, but if it doesn't, then which eas will tell you where it is in your path, and command -v eas will tell you how your shell is invoking it. (Here's the explanation of command -v vs. which.)

Is nvm is relevant for node these days in windows, should I use it? and if yes why?

When I tried installing nvm from website -
https://github.com/coreybutler/nvm-windows
everything went good, except the nvm use, even when I execute this command this doesn't seem to do its job properly though no error is thorwn by the terminal(cmd). When I try to use
node -v
it doesn't show the version number of node, that means node is not installed. And same goes witht the
npm -v
, this too doesn't show the version number, it hasn't been finding the executable. Everything works fine, that I install nvm install too. nvm list says there it is, and it donwloads and saves it the its user/appdata/nvm folder,(I checked it). But still cmd can't find the executable. Do I manually need to set the path for the node folder that's located in the roaming/nvm/node folder.
https://github.com/coreybutler/nvm-windows/issues/221#issuecomment-262363222
This comment solved the problem with one go, I guess the key was to use powershell, now node and npm works like smoothie. ;D

Express command not found after setting installing globally

When I run sudo npm install -g express-generator:
/Users/myname/npm/bin/express -> /Users/myname/npm/lib/node_modules/express-generator/bin/express-cli.js
/Users/myname/npm/lib
└── express-generator#4.15.5
And when I run express:
express not found
Also, I thought it was interesting that when I run which npm:
/usr/local/bin/npm
That is the path.
It seems that whenever I try to install something like yeoman or this express generator globally, it never works.
It should also be noted that I am on an Mac running zsh.
Most likely your npm bin directory is not in the path. Try to list the files in that directory by ls -l /usr/local/share/npm/bin/.
If you find the express file, you can add that directory to the path by export PATH=/usr/local/share/npm/bin:$PATH. If not, most likely something went wrong with your installation and you can try installing the module again.
Running this in the command line:
export PATH=/usr/local/bin/npm:$PATH
Fixed the issue...for now. I'm not totally sure why that worked.
This affects Homebrew users using Node.js & npm
When you install Node.js using Homebrew it does not put npm on the PATH for you, however it suggests that you should do so.
Homebrew doesn't modify the user's environment or dotfiles. However, brew install node does print a message suggesting the user add that path to their PATH.
Source

Resources