node 5.5.0 already installed but node -v fetches with "v4.2.1" on OS X & homebrew? - node.js

I'm trying to update my nodejs v4.2.1 to the latest version and successfully got that. But node -v still gives me '-v4.2.1'.

Probably the older Node is in your PATH before the newer one.
You can run in your shell:
which node
to see where is the Node binary that is run by default (v4.2.1 in your case). You can see what is your PATH by running:
echo $PATH
It will show something like:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Those are directories that are searched in order whenever you type "node" or any other command. If your PATH doesn't have the directory where you have your new Node installed, or if it is after the directory where you have your old Node, then the new Node will not be run. Fixing the problem may be as simple as running:
PATH="/usr/local/bin:$PATH"
if your new Node is installed in /usr/local/bin (or with some other path if it is installed somewhere else). You need to add this line in .profile or .bashrc in your HOME to have the PATH set up correctly every time you log in start a new shell.
To see if you have the correct Node version in /usr/local/bin run:
/usr/local/bin/node -v
Update
Looking at your comment and updated answer my bet would be that you have installed Node 4.2.1 manually (not with brew) and now brew doesn't update the binary in /usr/local/bin.
What I would recommend is to install it manually and have control over the versions. I'll show you the commands to download the source, configure, build and install in a versioned directory, and update the PATH.
According to the Node download page the current version is v6.1.0 but if you want specifically 5.5.0 or any other version (the latest 5.x is v5.9.1) then just change the commands below to the verson that you want. (All versions are listed here.)
# change dir to your home:
cd ~
# download the source:
curl -O https://nodejs.org/dist/v6.1.0/node-v6.1.0.tar.gz
# extract the archive:
tar xzvf node-v6.1.0.tar.gz
# go into the extracted dir:
cd node-v6.1.0
# configure for installation:
./configure --prefix=/opt/node-v6.1.0
# build and test:
make && make test
# install:
sudo make install
# make a symlink to that version:
sudo ln -svf /opt/node-v6.1.0 /opt/node
and finally add PATH="/opt/node/bin:$PATH" to your .profile or .bashrc (The node-v6.1.0 directory in your HOME and the .tar.gz can be removed or kept for later use).
At this point which node should return /opt/node/bin/node and the version is the one that you want (6.1.0 in this example). If you want to test another version then install it in another directory under /opt/node-vXXX and update the symlink. You won't have to update PATH, just the symlink.
This is more work than with brew but you have total control over what gets installed and where. What I like about this solution is that your versions never get mixed or confused and you can install/remove versions any time and quickly set any version as default.

Your path contains /usr/local/bin before /usr/local/Cellar. Since the version of node you're using is the homebrew one, fix it with:
PATH="/usr/local/Cellar:$PATH"
Next time you have a problem with brew, run brew doctor, it would have told you what to do

Related

Node and npm not found after restarting macOS

I am using my corporate's laptop and am a new mac User (used Ubuntu before) :
OS: macOS Monterey Version 12.2
There are two accounts, administrator and mine - I don't have sudo rights. To install homebrew without administrator rights, I followed this Installation.
To install the node - I used brew install node. Both node -v and npm -v were working. When I restarted the laptop, I cannot find node/npm.
On running $ node -v, I get -bash: node: command not found (I changed my default terminal from zsh to bash and the output is the same for both of them)
I tried this solution but couldn't find nvm in the system. Am new to mac and I believe nvm is some kind of package manager like homebrew so this solution is not applicable to me (correct me if I am wrong).
How can I install things in my system without sudo rights and keep them permanently(like node)?
EDIT:
(After adding brew to the PATH) On running - brew list|grep node, I got - node
$ echo $PATH gives /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/parthkapadia/homebrew/bin
EDIT2:
Adding brew to path solved the issue, now I can even access node and npm (even after restarting). I used this site to add homebrew/bin to path (in zsh terminal)
The issue was homebrew's path. It was not added to the PATH variable.
When I restarted the system, homebrew was no longer in the PATH (as it was temporarily added probably when I installed it). As homebrew was not added to PATH, the terminal didn't recognize brew or any package installed using it like node or npm.
I solved it by adding Users/username/homebrew/bin to PATH. The steps I followed are -
cd - to move to the home directory
touch .zshrc to create .zshrc file as it didn't exist
nano .zshrc to open the file for editing
Added export PATH=$PATH:/Users/yourusername/homebrew/bin in the file (this appends homebrew/bin to the PATH variable)
Now the terminal can recognize brew and hence node and npm too.
Refer this for more detailed explanation on how to add to PATH in macOS.
Thanks to all the people who helped in the comments.

Node won't upgrade beyond v6.11.2

When I run node -v I see:
node -v
v6.11.2
I run brew update, brew upgrade, and update all npm modules every morning when I turn on my MacBook. I have npm 6.4.1.
I went to the Node downloads website, downloaded the installer, and installed v10.13.0. node -v still says v.6.11.2. I restarted my computer, ran the install again, no change.
I think I see the problem. When I run echo $PATH is see:
/Users/TDK/.rvm/gems/ruby-2.2.3/bin:
/Users/TDK/.rvm/gems/ruby-2.2.3#global/bin:
/Users/TDK/.rvm/rubies/ruby-2.2.3/bin:
/Users/TDK/.nvm/versions/node/v6.11.2/bin:
/usr/local/mysql/bin:/Users/TDK/google-cloud-sdk/bin:
/usr/local/heroku/bin:
/usr/local/bin:
/usr/bin:
/bin:
/usr/sbin:
/sbin:
/Users/TDK/.rvm/bin
/usr/local/bin is the path to Node, so that's good. But above it is a path to /Users/TDK/.nvm/versions/node/v6.11.2/bin. I need to get rid of that path. I opened /etc/paths and saw:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
It looks like I have another paths file somewhere. All those paths above /usr/local/bin were installed when I was in coding bootcamp, I can get rid of all of them. Where might this other paths file be? I tried Spotlight to search for the first path, it couldn't find any file containing that string.
It looks like these are all path for one specific user /Users/TDK/ (that's me), not global paths for all users. Is there somewhere in /Users/TDK/ with paths? I commented out and then deleted lines involving RVM in the following files, without fixing the problem.
~/.bashrc
~/.bash_profile
~/.profile
I checked these files but didn't see anything suspicious:
/etc/profile
/etc/bashrc
Another thought is that nvm is causing the problem. The path
/Users/TDK/.nvm/versions/node/v6.11.2/bin:
is the problem. Maybe instead of removing this path I can alter nvm to point to the latest version of node?
Do you want to remove node 6.11.2 from your computer?
If not, just run nvm install node. That will install the lastest node, if you want to install a specific version, run nvm install 10.10.0 instead
Then run nvm use node or nvm run node --version

Installing nodejs and npm on MSYS2

My OS is win7, and I using MSYS2(version:MSYS_NT-6.1), Please give advice how to install nodejs and npm on this terminal, Thanks!
I found a solution for resolving the problem,
64bit env.
pacman -S mingw-w64-x86_64-nodejs
32bit env.
pacman -S mingw-w64-i686-nodejs
after installed, Open terminal
$ node -v
v6.11.0
As of 2020, the package mingw-w64-x86_64-nodejs is not available any more. The simplest way to have Node.js, npm and git installed on a Windows machine is using the official Windows installers:
Git: https://git-scm.com/download/win
Node.js (npm is shipped with it): https://nodejs.org/en/download/
After installation, open a command prompt (click on start, and then type cmd and [ENTER]) and verify that all three tools are there:
git --version
node --version
npm --version
Later on, to update Node.js, simply reinstall it from the same source.
I wasted a lot of time on this. My solution is:
Download the Windows Binary (.zip) from nodejs site (https://nodejs.org/en/download/current/)
Extract it to some folder
Add that folder to the PATH env variable
It does work to use the Windows installer, and Node.js helpfully provides bash-script versions of npm and npx in C:\Program Files\nodejs\ to help streamline the process.
However, contrary to Cerclanism's comment # jmgonet's answer, you should not use --full-path with MinGW, no matter what terminal you're using, since that will by default bring the entire Windows path into your MinGW environment.
(Assuming you're a typical Windows developer with things like MSVC, Windows Python, and etc. install dirs on your path, containing plenty of names that clash with MinGW path members, you can see how that might bite you at some point down the road. My full Windows CMD.exe %PATH% is 1236 characters! I don't want all that sucked into MinGW.)
Instead, you should add the nodejs install dir to your MinGW shell $PATH, say by using everyone's favorite ~/.profile/~/.zprofile $PATH-munging trick:
# Append node.js to path
case ${PATH} in
*"/c/program files/nodejs"*)
;;
*)
export PATH="$PATH:/c/program files/nodejs:"
;;
esac
You'll probably also want to set some configuration, since by default Windows npm will use ${APPDATA}/npm for prefix, ${LOCALAPPDATA}/npm-cache for cache, C:\Windows\system32\cmd.exe for shell, etc.
# To view the full config including all defaults and overrides
npm config ls -l
# To view the active config for the specified environment
npm config list -L {global,user,project}
Maybe I was just confused, but to me it seemed, from what the configs show/say, that setting prefix= in my user config would override even local installs. (The project-specific ones where you npm install without --global, directly into a node_modules subdir of the current dir.) But after testing, happily I can report that's not the case, so it's safe to override the builtin prefix= from your $HOME/.npmrc.
Whether or not you move the cache= or let it stay at C:\Users\<you>\AppData\Local\npm-cache\ is your call. I'm sure it'll work that way. (Well, maybe not from an MSYS shell, but from MinGW it should be fine.)
There are minor differences I haven't overcome, but the only one that comes to mind right now is:
npm help <command> opens a browser window to HTML documentation, instead of displaying man page content directly in the terminal like it does on Linux. (Makes sense, as I don't think the manpages are even installed on Windows. Still disconcerting, though.)
You can simply install nvm, then install nodejs through there. In your MSYS2 shell just run the following to download and install nvm. Its better to go directly here and copy the download commands as the version numbers will change in the url.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
then run the following to setup nvm on your bash path:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
After running those commands you can use nvm install 16 or whatever node major version number you want. Type just nvm to get a list of available commands.

NPM and NODE command not found when using NVM

Seen other questions, but I think they are not my case.
I think the problem is over here, but I don't know how to solve it:
I do have latest Node version installed (I followed the official github page instructions)
> nvm install v7.3.0
v7.3.0 is already installed.
Now using node v7.3.0
I check node and npm versions installed
> which node
~/.nvm/versions/node/v7.3.0/bin/node
> which npm
~/.nvm/versions/node/v7.3.0/bin/npm
I check the PATH is right and it actually is
> echo $PATH
~/.nvm/versions/node/v7.3.0/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
But it still fails and when I browse through nvm folders... I find this, which I don't know how to change or solve:
> ls -a .nvm/versions/node
.
..
.DS_Store
v6.4.0 // WTF???
And I promise I didn't do anything, I mean... this is a clean install, .nvm folder didn't exist before installing nvm.
If you want to install the version you want in the place you want then you can follow my tutorial here on GitHub:
https://gist.github.com/rsp/edf756a05b10f25ee305cc98a161876a
It's about version 6.7.0 but you can change it to any other version. It show you how to install either from source or from binary packages and following that tutorial you will always know which version is where, because you have full control over the installation instead of relying on tools that do that automatically for you.
If you want to have Node 7.3.0 in /usr/local for example the it is just:
wget https://nodejs.org/dist/v7.3.0/node-v7.3.0.tar.gz
tar xzvf node-v7.3.0.tar.gz
cd node-v7.3.0
./configure --prefix=/usr/local
make && make test && echo OK || echo ERROR
sudo make install
Edit .bash_profile using the below command.
nano .bash_profile
And add the following lines to .bash_profile
export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
Save it. Exit the terminal and check the magic.

Why isn't Node Version Manager (NVM) recognized on Windows?

I am trying to downgrade my version of node
I ran:
npm install nvm
and I exported the bin folder to my Windows path variable,
C:\Program Files (x86)\nodejs\node_modules\npm\bin
but I still get:
'nvm' is not recognized as a an internal or external command.
Should I be adding another path to my path variable?
nvm was designed for Linux. nvmw, which is completely different, broke around node v0.10.30. Try NVM for Windows.
NVM can be used to manage various node version :
Step1: Download
NVM for Windows
Step2: Choose nvm-setup.zip
Step3: Unzip & click on installer.
Step4: Check if nvm properly installed, In new command prompt type nvm
Step5: Install node js using nvm :
nvm install <version> : The version can be a node.js version or "latest" for the latest stable version
Step6: check node version - node -v
Step7(Optional)If you want to install another version of node js - Use STEP 5 with different version.
Step8: Check list node js version - nvm list
Step9: If you want to use specific node version do - nvm use <version>
NVM Installation & usage on Windows
Below are the steps for NVM Installation on Windows:
NVM stands for node version manager, which will help to switch between node versions while also allowing to work with multiple npm versions.
Install nvm setup.
Use command nvm list to check list of installed node versions.
Example: Type nvm use 6.9.3 to switch versions.
For more info
As an node manager alternative you can use Volta from LinkedIn.
I created a universal nvm that works on both Unix (bash) and Windows, base on another simple nvm.
It doesn't need admin on Windows, but requires PowerShell 4+ and the right to execute scripts.
https://www.npmjs.com/package/#jchip/nvm#installation
The first thing that we need to do is install NVM.
Uninstall existing version of node since we won’t be using it anymore
Delete any existing nodejs installation directories. e.g. “C:\Program Files\nodejs”) that might remain. NVM’s generated symlink will not overwrite an existing (even empty) installation directory.
Delete the npm install directory at C:\Users[Your User]\AppData\Roaming\npm
We are now ready to install nvm. Download the installer from https://github.com/coreybutler/nvm/releases
To upgrade, run the new installer. It will safely overwrite the files it needs to update without touching your node.js installations. Make sure you use the same installation and symlink folder. If you originally installed to the default locations, you just need to click “next” on each window until it finishes.
Credits
Directly copied from : https://digitaldrummerj.me/windows-running-multiple-versions-of-node/
I will list two ways. You can choose one Whichever works for you.
1. Using installer
Download nvm-setup.zip and unzip the file and install it, keeping the configurations default.
1. Use curl
Copy the below command and run it in your terminal
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
After this reopen/open terminal and check the nvm version runing below command.
nvm -v
And that's it.
If someone is looking for install on Window 11! Not directly relevant here, but might be useful.
It is immaterial if you install NVM (version 1.1.9.) say after the node (16.15.1) is already installed. During the nvm installation process, it asks for the right to manage the existing node version and symlinks that.
Get the version from the GitHub repo, I opted for the zip version.
https://github.com/coreybutler/nvm-windows/releases
Double click the application and it is just a few steps.
1.downlad nvm
2.install chocolatey
3.change C:\Program Files\node to C:\Program Files\nodejsx
emphasized textThe first thing that we need to do is install NVM.
website :
https://learn.microsoft.com/en-us/windows/nodejs/setup-on-windows
So this answer is for windows users that are using git bash or some other console emulator like cmder ... if you're using CMD this solution will not work for you also why? why are you still using CMD?
I know this is a pretty old post but I just achieved this yesterday and wanted to add my answer for anyone looking to do the same.
First check if you have .bashrc profile in your home directory by typing ls -alh ~ (by default this doesn't exist)
if it doesn't exist type this command to generate a .bashrc profile with default values in it cat /etc/bash.bashrc > ~/.bashrc (if it does exist skip this step)
Download and run the nvm install script as provided in the nvm docs page curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash (make sure you do this in your home directory)
then edit the new generated .bashrc profile file you created above; use nano/vim to do that nano ~/.bashrc and add the following to the bottom of the file export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm and save your .bashrc file with the changes.
lastly source your .bashrc file by typing source ~/.bashrc
verify installation nvm --version
and now you have nvm installed and you can use the commands as per https://github.com/nvm-sh/nvm#usage
First off, I use nvm on linux machine.
When looking at the documentation for nvm at https://www.npmjs.org/package/nvm, it recommendations that you install nvm globally using the -g switch.
npm install -g nvm
Also there is a . in the path variable that they recommend.
export PATH=./node_modules/.bin:$PATH
so maybe your path should be
C:\Program Files (x86)\nodejs\node_modules\npm\\.bin
An alternative to nvm-windows, which is mentioned in other answers would be Nodist.
I've had some issues with nvm-windows and admin privileges, which Nodist doesn't seem to have.
I know I'm late here but this may help in the future if someone looking for NVM to install in Windows or linux
run this command in cmd
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

Resources