How to handle global npm packages after periodic brew upgrade? - node.js

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

Related

How to upgrade node to specific version in macOS Mojave, and update the active path for node?

I want to upgrade node to 12.10.0 and npm to 6.11.3, currently I am working on macOS Mojave version 10.14.5
I am using these commands to upgrade the node version to 12.10.0::
sudo npm cache clean -f (force) clear you npm cache
sudo npm install -g n install n (this might take a while)
sudo n 12.10.0 upgrade to the specific version
After running the last command it gives me an output like::
installing : node-v12.10.0
mkdir : /usr/local/n/versions/node/12.10.0
fetch : https://nodejs.org/dist/v12.10.0/node-v12.10.0-darwin-x64.tar.gz
installed : v12.10.0 to /usr/local/bin/node
active : v10.16.3 at /usr/local/opt/node#10/bin/node
When I am checking for node version node -v it still shows me old version 10.16.3
I have tried to add node path, but still, it gives me the same output.
I have used command sudo nano /etc/path and then added /usr/local/bin/node path to it.
Please suggest me how can I upgrade node to 12.10.0 and npm to 6.11.3 version?
Install nvm following below tutorial :
Install nvm with homebrew
As stated in the link above,
Start by :
brew update
brew install nvm
mkdir ~/.nvm
nano ~/.bash_profile
In your .bash_profile file (you may be using an other file, according to your shell), add the following :
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Back to your shell, activate nvm and check it (if you have other shells opened and you want to keep them, do the same) :
source ~/.bash_profile
echo $NVM_DIR
Then with the help nvm install any node version(example v12.14.1) you want :
nvm install v12.14.1
nvm use v12.14.1
to switch to 12.14.1 version.
Step 1: Clean npm cache
sudo npm cache clean -f
Step 2: Install node helper (n) globally using the following command.
sudo npm install -g n
Once node helper is installed. You can either get a specific version like I needed 16.14.1 then you can perform.
sudo n 16.14.1
OR You can get the latest stable version using
sudo n stable
Then you can confirm the version installed
node --version
install n (npm module) npm install -g n and run sudo n latest or sudo n <version>
An alternate way to upgrade w/o using brew -
Go to nodejs releases page. The table on this page list the various node release available for download.
Click on the downloads link for the version you need. This will take us to another page to download the node version depending on the OS you are using. For mac, we can download the .pkg format.
After the package is downloaded, we can run it by clicking the downloaded file and hitting 'Next' till we complete.

Upgraded node and npm via nvm, but old node is still used for global packages

I've been using node 9.3.0 for a long time, but I recently migrated to 10.12.0. Everything went fine, when I do node -v and npm -v I get the correct versions:
Paul-Bergs-Macbook:node paulrberg$ node -v
v10.12.0
Paul-Bergs-Macbook:node paulrberg$ which node
/Users/paulrberg/.nvm/versions/node/v10.12.0/bin/node
Paul-Bergs-Macbook:node paulrberg$ npm -v
6.4.1
Paul-Bergs-Macbook:node paulrberg$ which npm
/Users/paulrberg/.nvm/versions/node/v10.12.0/bin/npm
Howeven, when I'm trying to run any npm command, the old version is used. That is:
Paul-Bergs-Macbook:node paulrberg$ npm i truffle -g
/Users/paulrberg/.nvm/versions/node/v9.3.0/bin/truffle -> /Users/paulrberg/.nvm/versions/node/v9.3.0/lib/node_modules/truffle/build/cli.bundled.js
+ truffle#4.1.14
added 81 packages from 311 contributors in 1.715s
And:
npm list -g --depth=0
/Users/paulrberg/.nvm/versions/node/v9.3.0/lib
└── truffle#4.1.14
Not sure if this is some bash code still pointing to the last version, but I can't seem to find any proof for that. Running env and checking for 9.3.0 environment variables yields no result.
What I did so far:
Delete node 9.3.0 with nvm uninstall 9.3.0
Do a fresh install of nvm after deleting it and rebooting the computer
nvm reinstall-with-packages
Deleted ~/.nvmrc and set 10.12.0 in there afterwards
Check if I have an overlapping node from homebrew and I don't
What could the problem be?
After a few hours of painful Unix debugging, I realised the problem was that I set a prefix in npm config:
prefix = "/Users/paulrberg/.nvm/versions/node/v9.3.0"
To fix this, make sure to unset the prefix by doing:
npm config rm prefix
This is mentioned, albeit not necessarily shining in the nvm doc
If you have an ~/.npmrc file, make sure it does not contain any prefix
settings (which is not compatible with nvm)
It looks like you might need to run nvm reinstall-packages
https://github.com/creationix/nvm#migrating-global-packages-while-installing
which says
Migrating global packages while installing
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.
You can also install and migrate npm packages from specific versions of Node like this:
nvm install 6 --reinstall-packages-from=5
nvm install v4.2 --reinstall-packages-from=iojs
The other "solution" is not to use global packages. Particularly when using nvm and not being able to be sure that the global package is for the "current" version it can be better to install locally and use npx to run the local command
truffle installs a truffle command to ./node_modules/.bin when you npm install it so you can npx truffle to run the local one instead of truffle to run the global one
edit:
another thing to check is that node -v and nvm current don't necessarily report the same version.
I wonder if nvm current would report v9.3 for you?
ah, yep, on my machine I can install truffle globally in a different location than node -v reports
> node -v
v9.5.0
> nvm current
system
> nvm use v8
Now using node v8.4.0 (npm v5.3.0)
> node -v
v8.4.0
> nvm current
v8.4.0
> npm install -g truffle
/Users/pauldambra/.nvm/versions/node/v8.4.0/bin/truffle -> /Users/pauldambra/.nvm/versions/node/v8.4.0/lib/node_modules/truffle/build/cli.bundled.js
+ truffle#4.1.14
added 81 packages in 4.364s
So you might be missing an nvm use v10 command
I think that a more permanent solution is this section from the support docs.
Default global packages from file while installing
If you have a list of default packages you want installed every time you install a new version, we support that too -- just add the package names, one per line, to the file $NVM_DIR/default-packages. You can add anything npm would accept as a package argument on the command line.

What would be the way now to install node with npm via brew

I've installed node on my macOS
brew install node
After doing this, node is installed correctly:
$ node -v
v8.4.0
But running
$ npm -v
gives me -bash: /usr/local/bin/npm: No such file or directory
What would be the way now to install node with npm via brew?
brew install node uses by default --without-npm
I had the same problem. I ran $ brew doctor to make sure node was linked first. Then I ran
$ brew postinstall node
$ npm -v should now show the version number.
I stepped into a similar issue, too. My problem was that I still had not a
~/.bash_profile file in place and therefore no place where to actually link my bash command to npm.
Don't use Homebrew to install node.
I like the Node Version Manager (NVM), and there is n. These are better options on a Mac for node, to avoid certain. issues. later. Plus it avoids this question, as these node versions include npm.
Note, you can install nvm and n via Homebrew. (brew install nvm or brew install n).

Install io.js and npm without node via Homebrew on OSX

As $title says, I want to install io.js with brew on Yosemite.
In my system there is no need for Node.js, and I want to avoid unnecessary programs.
But.. When I run brew install iojs I see it will be built with --without-npm option, and as the post-install text clarifies, it needs a patched npm.
Although I searched for the solution, the only thing that came across is how to run io.js and Node.js side-by-side, and that's not what I'm looking for.
I see there is an npm package for brew, but it's part of the node package.
How can I install iojs+npm without node?
Install nvm (node version manager) using brew:
brew update
brew install nvm
source $(brew --prefix nvm)/nvm.sh
Add the last command to the .profile, .bashrc or .zshrc file to not run it again on every terminal start. So for example to add it to the .profile run:
echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.profile
Using nvm you can install any version of node or io.js you want. So to install the latest version of iojs do:
nvm install iojs
npm is shipping with iojs so you don't need to install it manually.
Related question: What is the suggested way to install brew, node.js, io.js, nvm, npm on OS X?
I've had the same problem: homebrew doesn't seem to properly explain how to get that "patched" npm in a clean way.
Anyway, nothing has worked for me better than getting that pkg from iojs.org, which includes npm.
(I'm referring to iojs v2.0.0)
It might be late but you can just use brew unlink node && brew link iojs --force
I avoided nvm because it was way too slow for me

how to install express js on ubuntu?

I am using ubuntu 14.04 LTS.
I installed nodejs as mentioned here, and then I installed express by running sudo npm install -g express#3 it got installed but when I try to create an app or even try to look at the version by running express -v or express -V it doesn't give any sort of output.
Any help is much appreciated, Thanks in advance!
Installation of node in ubuntu is fairly a straight forward process. I don't know what gone wrong with your earlier attempt.anyway you can install it again if you wish. there are two ways to install node.
Download and install from nodejs.or
Use NVM(node version manager)
I always prefer the NVM method because it not only allows you to switch between versions but also avoids some of issues that otherwise you may face later. example, can't install npm packages globally without sudo.
Before you start remove your old installation
sudo rm -r ~/.npm
Now install nvm
curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | bash
To activate nvm
source ~/.nvm/nvm.sh
Then install node
nvm install 0.10
nvm use 0.10
To set a default Node version to be used in any new shell
nvm alias default 0.10
Check everything done properly
node --version
Then install express
npm install -g express

Resources