Updating to a particular version of node: paths being strange? - linux

I'm trying to update node (on Ubuntu) and have been using the n package to do this.
I've run the following:
$ sudo npm install -g n
% sudo n 0.10.21
This appeared to run OK, but now I get the following:
$ which node
/usr/local/bin/node
$ node -v
0.6.4
$ /usr/local/bin/node -v
0.10.4
which is baffling to me.
How can I get rid of 0.6.4 and ensure that 0.10.4 is what runs when I type node?

This is actually a shell issue. I've only seen it in bash, but here goes:
Bash caches where it has found commands in the past. This means that if you place an executable (with the same name as something you've run before) earlier in the PATH, which will correctly find the new one, but bash will hit the cache and run the old one.
The easiest fix is simply to close your shell and open a new one. However, you can aldo use the command hash -d node to clear node from the cache (or type node to see whether it is in the cache and what it is pointing to).

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

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

NPM command not found after installing Node via PKG (no brew)

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

How to install node.js

I did the following:
user#machine:~$ sudo n stable
installed : v12.13.0 (with npm 6.12.0)
user#machine:~$ node --version
v8.10.0
user#machine:~$ which node
/usr/local/bin/node
user#machine:~$ /usr/local/bin/node --version
v12.13.0
I was expecting v12.13.0 to be installed, but node --version reveals v8.10.0.
What did I do wrong? How do I get v12.13.0 installed? FYI I am on Jetson Nano and I'm really just trying to get LTS Nodejs installed and don't know how.
BTW, if I do sudo apt-get install nodejs I only get v8.10.0.
MORE INFO:
user#machine:~$ node --version
v8.10.0
user#machine:~$ sudo node --version
v12.13.0
Seems sudo is affecting this somehow?
(I'm not very familiar with linux, but I'm learning ... sorry if I'm missing "obvious" stuff.)
I worked around the problem by forgoing the use of n.
Here's how to install directly from the dist at nodejs.org.
wget https://nodejs.org/dist/v12.10.0/node-v12.13.0-linux-arm64.tar.gz
sudo tar -xf node-v12.13.0-linux-arm64.tar.gz --directory /usr/local --strip-components 1
Can't comment but try replacing /usr/bin/node with a symbolic link to /usr/local/bin/node. Had similar issue some time ago that ate my brain. Solved it like this.
P.S don't forget to backup
It's very strange...
Could you try something?
mv /usr/local/bin/node /usr/local/bin/node.back
which node
mv /usr/local/bin/node.back /usr/local/bin/node
I just want to see what produce which if you remove the old node version binary.
Also provide more info about file /usr/local/bin/node, is it a symbolic link? If true, to what?
1) Try opening a new shell and checking what version of node you see there.
n installs the node binary by default to /usr/local/bin/node. You may already have had a node binary installed to /usr/bin/node, and your shell cache of known commands (paths) may be running the old one.
Example of problem with bash: https://github.com/tj/n/issues/588
2) If your OS has the command, you can run which -a node to see if you have multiple versions and where they are installed. (Uninstalling the ones you don't want reduces potential for confusion.)
3) Check the location of the intended version is in your PATH. If you are using n, try running n doctor. There are some extra checks that the version of node found is the one that is the one that n installed.

Sudo user not using same node version

I have a strange issue on my server, when ever i try to install packages with npm that require sudo I run into issues and i have discovered that my su and regular user use different node versions.
$ node -v i get node version 0.10.x
But when i do:
$ sudo node -v i get node version 0.6.x
My su user for some reason is using a different node version than what i normally use and this causes compatibility issues when I npm install packages that require sudo.
I have tried sudo apt-get upgrade nodejs at no luck. how do i make su user use same node version as my regular user.
run ll /usr/bin/node if this file exist, simply run rm /usr/local/bin/node from the regular user
explanation
If you'd run which node from regular user you will probably see it points to the user local bin directory
which node
/usr/local/bin/node
this means that the regular user installed another node version locally.
to let the same node version apply to all users, this command should show you usr bin (not local).
which node
/usr/bin/node
by deleting the link from /usr/local/bin/node it will automatically start using /usr/bin/node
Try running sudo with the environment of the user:
sudo -E node -v
I experienced this problem right after upgrading to a newer version of node (6 -> 7)
The above solution did not work for me as I do not the file /usr/local/bin/node but I was in fact getting different verions from running npm --version and sudo npm --version
https://askubuntu.com/questions/1036278/npm-is-incorrect-version-on-latest-ubuntu-18-04-installation
The answer states that bash was caching the older version of node.
simply running hash -d npm

Resources