npm deletes on its own after closing the ubuntu terminal? - node.js

I am running Ubuntu 16.04 Xenial on VirtualBox. The problem is that once I install npm using the command:
nvm install v8.1
then it recognizes commands like npm.
But if I close this particular session of terminal and reopen it again, then it fails to recognize npm command. So everytime I need to use npm, I have to reinstall it for that particular session of terminal. And when I reinstall it using nvm install v8.1, It says,
v8.1.4 is already installed.
Now using node v8.1.4 (npm v5.0.3)
But now it recognizes npm command. What is the problem?

When node is installed via nvm, it's actually expected that you would type nvm use 8 before using node. You shouldn't have to re-install it (and, based on your output, it looks like you didn't).
If you want to default to node v8, you can do that by typing:
nvm alias default 8.1
Note that you may be missing the appropriate .bashrc entry (in your ~/.bashrc file) that loads nvm when your terminal starts up. It should look something like this:
export NVM_DIR="$HOME/.nvm"
. "$NVM_DIR/nvm.sh"

Related

Node version not updating after "nvm use" on mac

I am trying to update my local node version from 8.9.0 to 8.10.0 using nvm. But it's not getting reflected. Here's what I tried:
node -v
-> v8.9.0
nvm use 8.10.0
-> Now using node v8.10.0 (npm v5.5.1)
node -v
-> v8.9.0
I am unable to get why it won't change. Please let me know what have I done wrong.
Adding few notes about my experience on debugging this problem.
First, I started out with the command provided by Jonathan which -a node and got the same output as op:
/usr/local/bin/node
/Users/<my-user>/.nvm/versions/node/v8.10.0/bin/node
Moving on to the next comment by Jonathan: Have you installed Node via Homebrew as well?, I tried uninstalling node by brew uninstall node and got and error that said it could not uninstall node as there was another program dependent on it. yarn.
And then realised that brew had installed its own version of node because I installed yarn through brew, which was overwriting the node version that I wanted to use through nvm.
Uninstalling brew's version of yarn+node fixed my problem.
I still needed yarn, and now I have to install yarn globally for each node version managed by nvm.
Inconvenient, I agree. But so is maintaining twenty thousand versions of node for each project so not very different in my opinion.
All of this was done on M1 MacOS and your output might vary based on your system.
Please ensure that when you input arch on command prompt, then output is i386.
This is achieved using Rosetta.
Following sequence of commands on terminal worked: -
nvm install 8.10.0
nvm install --lts
nvm ls
brew upgrade
arch -arm64 brew install n
n
Now select node version 8.10.0 using up or down arrow keys in keyboard and then quit from command prompt using command+q
Please do not try brew uninstall --ignore-dependencies node
Just check you environment variables first. It might be mapping to node#8.9.0.
You need to remove the environment variable first.
If you are also using volta. Volta may take precedence.
Use which -a node to verify.
If that is the case, use volta pin node#version to switch.
First check your nvm version installed on your local system. go to command prompt and type "nvm list" If you don't see the versions 8.10.0 in the list then install using command : "nvm install 8.10.0" and then try "nvm use 8.10.0".
For reference : http://www.tutorial-points.com/2018/11/nvm-installation-on-windows.html

Use node from homebrew

Running node -v in the terminal returns 0.12.2, while running brew info node to check homebrew's version of node, it returns 6.7.0
How can I work using homebrew's node? (6.7.0 instead of 0.12.2)
I've tried reinstalling everything, but it didn't work.
Two options...
Either specify the full path to where hombrew installed node each and every time you use it:
/usr/local/bin/node -v
Or, change your PATH in your login profile to put /usr/local/bin ahead of everything else:
export PATH=/usr/local/bin:$PATH
Don't use homebrew, use nvm. https://github.com/creationix/nvm
Not only is it easier to install and use, it also allows you to have multiple versions of node/npm installed and switch between them effortlessly (e.g. nvm install v6.7.0 and nvm use v6.7.0)

NPM Command Not Found After Installing Node

I am having a very hard time getting the npm command to work, and unfortunately my knowledge of unix isn't good enough to solve this on my own. All I've done is brew install node, and I get the following errors:
When I type npm I get zsh: command not found: npm
Looking into this issue more I found this stack overflow answer:
Command not found after npm install in zsh
Following its advice I tried adding export PATH=/usr/local/share/npm/bin:$PATH to my .zshrc file. Still get the same error when typing npm
I can confirm it is in the path echo $PATH yields /Users/nicholashaley/.rbenv/shims:/Users/nicholashaley/.rbenv/bin:/Applications/Postgres.app/Contents/Versions/9.3/bin:/usr/local/heroku/bin:/usr/local/share/npm/bin:/usr/local/bin:/Applications/Postgres.app/Contents/Versions/9.3/bin:/usr/local/heroku/bin:/Users/nicholashaley/.rbenv/shims:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
I can also confirm that node has been installed node -v yields v0.12.2
Any ideas? I'm stumped
Not an answer, but maybe a solution...
Uninstall Node via brew: brew uninstall node
Then download Node from https://nodejs.org/en/download/ and install.
npm should then "just work".
I came across to this thread with the same problem but finally below is what worked for me which is without sudo
$ brew update
$ brew uninstall node
$ brew install node
$ chown -R YourUserName /usr/local/lib/node_modules/
$ brew postinstall node #This what the important step
Ok this is what worked for me:
$ brew update
$ brew uninstall node
$ brew install node
$ sudo brew postinstall node #This what the important step
In case this never got solved, this is what worked for me:
brew uninstall node
brew install node
brew doctor and see what needs to be pruned. My problem was there was a non-brew place where I had changed my install location for npm packages because I was trying to get an npm package to install for me. That was a mistake (having forgotten I initially installed node and npm through brew but now brew could no longer reach the npm install directory nor could it change or delete it). So brew prune didn't work even though the symlinks were supposedly deleted but it reminded me that I had changed the npm install location. And that brings us to:
Delete those directories
brew postinstall node
Hope it helps.
I found this article helpful with step-by-step details:
https://www.dyclassroom.com/howto-mac/how-to-install-nodejs-and-npm-on-mac-using-homebrew
Additionally, after all the steps in above article, node was working but my machine was still giving "command not found: npm". I then ran the following two commands and npm was working.
sudo chown -R myusername:myusergroup /usr/local/lib/node_modules
brew postinstall node
Anyone on the LTS release use the following command instead:
brew postinstall node#6
Issue Details in Gist and how I fixed it
1. Installed nodeJs via nvm using Git Bash
2. Installed hyper terminal & WSL
3. Installed Ubuntu (windows Store) & configured zsh shell.
4. Faced the command not found all over in the zsh shell
5. The same commands are working nicely in the bash shell
Tried sourcing the (node, nvm, npm ) paths in the .zshrc and .profile file &
modified the System & Environment Variables in Windows. None of these steps helped me to resolve the issue.
I think the issue is zsh and bash terminals treat things differently. When Installed in bash terminal the default location for the nodeJs in windows will be set to
C:\Users\Program Files\nodejs
The above one is symlinked to C:\Users\<username>\AppData\Roaming\nvm
No matter how much I tried to alias these things in zsh, only node and nvm are working with aliases. npm is failing to locate the necessary lib files.
The only solution that worked for me is Re-installing node in zsh terminal
Removed the node versions installed via nvm in Bash terminal.
Removed the nvm in windows via control Panel
Installed the nvm in the zsh shell
Installed the node latest LTS version from which npm is also installed.
Post Installing the node via nvm in Zsh I observed the following things.
Paths for the node, nvm, npm & npx have been automatically added to the $PATH variable
Node directory is not available neither in C:\Users\Program Files\nodejs
nor the C:\Users\{username}\AppData\Roaming\nvm
Instead it is configured differently at /home/<username>/.nvm/versions/node/v14.15.1/bin/node
This video really helped me in re-installing the node in ZSH: https://www.youtube.com/watch?v=kL8iGErULiw
Usually npm should be found in the same directory as that of the node executable, which can be determined by which node. Check if this directory is part of your PATH. If node was installed through a node version managers such as nodenv and nvm, check the documentation for how to add these executables to the path.
With nodenv, this can be done with:
eval "$(nodenv init -)"

NVM - All The Packages I Installed with NPM Suddenly Stops Working

I installed Node with NVM as I would not like to use sudo for access permission purposes.
I got the installation of nvm, installed node, installed npm and some npm packages.
The next morning I power on my mac and everything is not working anymore.
I guess ~/.nvm/v0.10.33/bin is not on my $PATH, but I'm confused as it was totally working fine yesterday.
P.S., The command nvm still works on my terminal though.
Nvm(nevermind). I got it working by setting the default to my node version by:
nvm alias default v0.10.33
So everytime I open a new terminal, node and other command just works.

Set up nvm to properly work with npm and grunt

I'm somewhat new to web app development, and ran through the Yeoman tutorial, which uses yo, grunt, bower, and angular.
I had used homebrew to install node and npm, but kept getting errors with npm due to some permissions and whatnot. I uninstalled node using brew, and instead used nvm to install node and npm, which no longer gives me errors.
BUT, nvm is somewhat annoying. I understand it's a node version manager, and I'm fine with having to nvm use 0.10 every time I open a shell. But now, whenever I want to grunt serve my web app from a new shell, I need to install grunt-cli:
nvm use 0.10
npm install -g grunt-cli
# ...
grunt serve
Is grunt-cli not installing globally? Or is it meaningless, because npm doesn't 'stick around' between different shells?
Basically, I'm fine with just using one version of node right now. How do I set up nvm, npm, and grunt so I don't have to re-install grunt every time I open a new shell?
Go to your command prompt and try "nvm alias default ". If you want to use version "0.12.7" then your command will be "nvm alias default 0.12.7" and to cross check if the version is used globally open a new command prompt and use command "node -v".
Instead of using nvm use everytime you could leave an .nvmrc file
As from the docs:
echo "0.12" >> .nvmrc
Now the next time you enter that directory nvm will read that file and load that specific version.
You can check if its working with
nvm use

Resources