Set up nvm to properly work with npm and grunt - node.js

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

Related

npm deletes on its own after closing the ubuntu terminal?

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"

Global Node Packages Installed to Wrong Directory

I'm trying to install Hexo globally using npm. When I run
npm install -g hexo-cli
I'm informed that it was installed to /Users/myusername/.node/bin/hexo -> /Users/myusername/.node/lib/node_modules/hexo-cli/bin/hexo
The problem comes in when I run hexo init blog and the hexo command is not found.
I installed Node and npm with Homebrew, so when I run which node and which npm, the results are /usr/local/bin/node and /usr/local/bin/npm respectively.
I'm thinking that I still have leftover files and directories from when I installed Node without homebrew, but I don't want to start deleting things without fully knowing the repercussions. Would I be safe to delete all files located in the /Users/myusername/.node/ directory? I can't figure out why npm is not installing to the proper directory.
After a little digging, I found that my npm prefix variable was pointing do the wrong directory, left behind by the old Node installation. I ran npm config get prefix to see where it was pointing.
I set the new prefix value using npm config set prefix /usr/local. Homebrew is symlinked with this directory via /usr/local/bin. I uninstalled hexo-cli and reinstalled through npm, and now it works perfectly.
For NVM users
Run nvm use --delete-prefix v10.13.0 --silent replacing v10.13.0 with whatever version of node you're using.
A simple way to cope with environment variables/path problems on Windows:
Run command:
npm install -g hexo
Using node.js command prompt rather than cmd windows provided by Windows itself.

How do I copy global modules between Node installations using nvm?

I manage my Node installations using nvm. I installed the grunt-cli module globally in my previous installation of node. This allowed me to run the command grunt on in the terminal.
When I installed a new version of Node (5.7.1), I got this error whenever I tried to execute the grunt command:
zsh: command not found: grunt
I discovered that the grunt-cli package had not been installed for the new version of Node.
I could install the grunt-cli package again but I would prefer to do this automatically whenever a new version of Node is installed using nvm.
Is there some way to install all the global modules from a previous version of Node when using nvm ?
This can be achieved using the --reinstall-packages-from option when executing nvm install. From the nvm documentation:
If you want to install a new version of Node.js and migrate npm packages from a previous version:
nvm install node --reinstall-packages-from=node
This will first use "nvm version node" to identify the current version you're migrating packages from. Then it resolves the new version to install from the remote server and installs it. Lastly, it runs "nvm reinstall-packages" to reinstall the npm packages from your prior version of Node to the new one.
If your prior version of Node is 4.3.0, the command will be executed thus:
nvm install v5.7.1 --reinstall-packages-from=4.3.0

can't revert to older version of node, can't uninstall node, OSX

Have installed node 0.12.2, but need to revert to 0.10.33 to run the applications I need to.
I have tried installing 0.10.33 and switching versions using
set 0.10.33 to default with 'nvm alias default 0.10.33'
But node runs previous version (0.12.2) when started again.
I have tried uninstalling node via various methods, but each one has been blocked:
uninstall node
"You must be in root to run this tool"
(I am the only user (with Admin access) on my machine
I tried
cd /
then
uninstall node
But still got
"You must be in root to run this tool"
Tried
sudo uninstall node
"Uninstall Failed...
Reason: ErrorMissingBundle"
I may have installed node with Homebrew - it was a while back, can't quite remember. So tried:
brew uninstall node
"Error: No such keg: /usr/local/Cellar/node"
Other information I can give:
which node
/usr/local/bin/node
I see node and npm in
/usr/local/bin
Any help most appreciated - I'm not a command line expert, and fairly new at Node.
To switch from different node.js version with nvm just use nvm use 0.10.33
It looks like you might be using NVM (node version manager). If you are you can install different versions using
nvm install version_no
and then you can switch versions using the command
nvm use version_no
If you have not used NVM to install node you can uninstall node using the instructions in this answer - How do I completely uninstall Node.js, and reinstall from beginning (Mac OS X).
Then if you need different versions of node I would recommend using NVM, which allows you to install and use different versions of node on the same system. https://github.com/creationix/nvm

How to handle global npm packages after periodic brew upgrade?

Updating node on OS X if node is set up with Homebrew basically appears to nuke my npm -g global packages.
For example my Vim's Syntastic usually runs jshint on js files, but once I update node this will usually just start failing silently.
I'm not really sure what I'm supposed to do about this. I'd like at the very least for there to be some kind of alert that says
Here is the list of global npm packages you currently have installed, these will all be gone after I am done updating node.js!
To fix this kind of problem, I use nvm(Node Version Manager), not brew's node.
brew uninstall node
brew install nvm
Add these line to .bashrc or .bash_profile
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Install node using nvm
nvm install 0.10
nvm use 0.10
nvm alias default 0.10
You can see this result
$ node -v
v0.10.30
$ which node
/Users/yourid/.nvm/v0.10.30/bin/node

Resources