Managing global npm packages when using nvm - node.js

I feel like I need to be able to install global npm packages separately into the NVM dir.
$ nvm install v0.11
######################################################################## 100.0%
Now using node v0.11.16
$ node-inspector
Node Inspector v0.9.2
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.
my node-inspector stopped working!
$ npm update -g
<bunch of updating, succeeds>
Maybe this will help... Nope! still broken.
Sigh. Perhaps... node-inspector doesn't work for a v0.11.16 (that's being run on nvm). Perhaps. Who knows. Well, time to abort.
$ nvm use system
Now using system version of node: v0.10.32.
$ node-inspector --version
dyld: lazy symbol binding failed: Symbol not found: _node_module_register
Referenced from: /usr/local/lib/node_modules/node-inspector/node_modules/ws/build/Release/bufferutil.node
Expected in: dynamic lookup
dyld: Symbol not found: _node_module_register
Referenced from: /usr/local/lib/node_modules/node-inspector/node_modules/ws/build/Release/bufferutil.node
Expected in: dynamic lookup
[1] 93845 trace trap node-inspector --version
Okay.... Well.... Shit.
So anyway, at this point I'm not looking for instructions on how to fix. I may well be hosed. The main question here is what am I supposed to do to manage these npm packages which are meant to be command line tools and which have compiled components that appear tied to specific versions? I understand that when I install nvm and incorporate it into my system, my shell is able to switch its $PATH so that when I call node and npm, they will run using the node version I picked.
But it appears as though the global npm packages get put into /usr/local/bin/ somewhere and they're just stuck there and become unable to follow what I do with nvm -- While it may not be the case for the "theoretically well-behaved node package", in practice (for something like node-inspector at least) it sure looks like it's liable to gleefully explode when not executed using the node that installed it.
In the meantime I have to basically npm remove -g <package> && npm install -g <package> any package that I find behaves strangely in this manner, every single time that I want to run that package under a new node version using nvm.
This seems wrong.
Is it wrong?
And, a corollary to this would be that every time I invoke nvm powers to test some given node.js app with a different version of node, I pretty much should do it by cloning it in a whole new directory and starting fresh, because otherwise I'll probably find out that I will need to rm -rf node_modules && npm install just to make it function at all...

UPDATE October 2020:
If you've already installed the desired Node version, according to the documentation this is also available:
nvm reinstall-packages <from-version>
Props to #rashi for pointing out the syntax above.
From the nvm help message:
nvm install [-s] <version> Download and install a <version>, [-s] from source. Uses .nvmrc if available
--reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>
--lts When installing, only select from LTS (long-term support) versions
--lts=<LTS name> When installing, only select from versions for a specific LTS line
The relevant flag is --reinstall-packages-from=<version>. Simply re-run your command (example):
$ nvm install v6.9.2 --reinstall-packages-from=v4.4.5

I may have misunderstood your situation, but it seems like you need to install all global packages you use separately for each node version you are using. The reason it works like that is that you may need different versions of packages for different node versions. So whenever you use nvm use ... it changes paths to global packages to the packages installed for the selected version of node.
Hope it helps.

Related

Updating to stable version of nodejs with n

this is my first time trying to update the nodejs version of a unix server. I want to use it to create a react app, and facing the following:
>npx create-react-app test
You are running Node 8.11.3.
Create React App requires Node 10 or higher.
Please update your version of Node.
So I have tried to update the version with the following commands:
>sudo npm cache clean -f
>sudo npm install -g n
>sudo n stable
installed : v14.15.4 to /usr/local/bin/node
active : v8.11.3 at /bin/node
However it seems that didn't upgrade the version, it installed a different version in a different path.
I am not sure if it is possible just to upgrade the version that is already active, I prefer not to do a workaround and just upgrade it directly.
As complementary information if I run n doctor I see the following:
>n doctor
Checking n install destination is in PATH...
'/usr/local/bin' is not in PATH
As I said previously, what I am looking for is a safe way to update the version already installed to be able to use create-react-app, being able to rollback to previous version easily if something doesn't work, I understand that the way is using n.
Thanks in advance.
I suggest you add /usr/local/bin to the start of your PATH (so it comes before /bin).
I do not recommend you try and overwrite /bin/node using n. Something else installed /bin/node in a system directory, and overwriting it using n could leave things in a mixed and confused state. n installs to /usr/local by default, but you can set N_PREFIX to install to your home folder, for example.
To avoid confusion with having two versions of node installed, you may wish to uninstall the /bin version, likely installed by your platform package manager.
Tip: Changing where node and npm are installed is likely to change where your global npm packages are installed. I suggest you list what you have installed now in case you want to reinstall then in the "new" location. npm list -g --depth=0

Node command refers to older version

Hello when I use the node command on my Mac. It refers to an older version:
-bash: /usr/local/opt/node#14/bin/node: No such file or directory
I have removed node 14 and later installed the latest version of node with brew install node.
How can I make that the node command refers to the latest version?
Generally, this issue can be uncovered by brew doctor run.
But like Andreas said, it should be resolved by brew link --overwrite node to fix the node symlink.
If you just need node, you can use nvm to manage the different node versions for different applications, and you can also use .node-version to specify this metadata.
You might have some invalid symlink pointing to the old version. Especially with version-specific packages, this can be the case. You might first check where your node executable is referenced from by running which node to make sure if the symlink comes from brew (usually using /usr/local/bin/node) and list the details of the link by e.g. ls -la $(which node).
To fix the link to point to the package you want, you can run
brew link node
Maybe this requires some force...
brew link --force node.
For details check this answer on apple stackexchange

Mac/Homebrew - changing Node version does not change NPM version

On my Mac, I'd like to use Homebrew to install an earlier version of Node and NPM - I'm currently running Node/NPM 8.2.1/5.3.0, and I'd like to switch to 6.11.2/3.10.10, as listed in the documentation.
I ran
$ brew install node#6
$ brew unlink node
$ brew link node# --force
and while this appears to change my node version - $ node -v -> 6.11.2 - my version of NPM is still stuck at 5.3.0.
How can I switch NPM to this other version (and switch it back, if need be)?
Try to uninstall all node versions firstly: brew rm node, after that ensure that node -v and npm -v prints command not found.
After that install nvm or n to manage node versions. These version managers have one great advantage, they allow you to have a few node versions and easily switch between them.
nvm - Node Version Manager
n - Node version management
With nvm:
nvm install v6.11.2
Luckily there is a good way to do this if you are someone like myself who finds nvm to be overkill, especially if you only need one version for an app/api support (e.g. johnny-five). Instead of installing a node version manager, you can actually create a 2nd "node" version with another homebrew node.js install.
Start by installing the other version of node that you want to use, for instance node#4, and then unlink that version's symlinks (you can use any brew installed version).
$ brew install node#4 && brew unlink node#4
Once it's installed and unlinked, go to the folder /usr/local/bin, and add a custom symlink for the other version you want access:
$ cd /usr/local/bin
$ ln -s ../Cellar/node\#4/4.8.5/bin/node ./node4
Now you can use the command node for your previous version, and node4 for the node#4 version.
Of course we do not want to mix npm packages with the two versions, so let's fix that by making a new npm directory for the node#4 version:
$ mkdir ~/.npm4
$ npm config set prefix ~/.npm4
Now you are ready to install global packages for the second version! Each time you want to switch versions you can simply swap the prefix, and since we are looking for a simple process this probably won't happen too often.
Of course you may want something that does not require the need to be proactive when switching versions, and while you can always install a separate version of npm somewhere and symlink it the same way you did the node version, that may become confusing over time when it comes to upgrading npm.
A few other options:
script the prefix command
add the prefix command and/or script in package.json
use a specific bash terminal profile that runs the prefix command on start
So there are few various options, but I will leave that all up to you..

Updating node and npm using Homebrew

I want to upgrade npm but I need Node version >0.10.32
Typing $ brew upgrade node I get:
Error: node 7.2.1 already installed
but with $ node -v I get:
v0.10.24
Why are there two version formats and how can I get node updated to the required version?
I need
You have two different versions of Node installed in different paths.
The one in the directory closer to the front of your $PATH environment variable is 0.10.24.
The one installed by brew is 7.2.1.
If you want to use the brew managed one, then the easiest thing to do is to uninstall the other version.
Alternatively, use the full path to the brew managed one.

How to change to an older version of Node.js

I am running Node.js version v0.5.9-pre on Ubuntu 10.10.
I would like to be using version v0.5.0-pre.
How do I roll back to the older version of node?
*NIX (Linux, OS X, ...)
Use n, an extremely simple Node version manager that can be installed via npm.
Say you want Node.js v0.10.x to build Atom.
npm install -g n # Install n globally
n 0.10.33 # Install and use v0.10.33
Usage:
n # Output versions installed
n latest # Install or activate the latest node release
n stable # Install or activate the latest stable node release
n <version> # Install node <version>
n use <version> [args ...] # Execute node <version> with [args ...]
n bin <version> # Output bin path for <version>
n rm <version ...> # Remove the given version(s)
n --latest # Output the latest node version available
n --stable # Output the latest stable node version available
n ls # Output the versions of node available
Windows
Use nvm-windows, it's like nvm but for Windows. Download and run the installer, then:
nvm install v0.10.33 # Install v0.10.33
nvm use v0.10.33 # Use v0.10.33
Usage:
nvm install [version] # Download and install [version]
nvm uninstall [version] # Uninstall [version]
nvm use [version] # Switch to use [version]
nvm list # List installed versions
One way is to use NVM, the Node Version Manager.
Use following command to get nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
You can find it at https://github.com/creationix/nvm
It allows you to easily install and manage multiple versions of node. Here's a snippet from the help:
Usage:
nvm install <version> Download and install a <version>
nvm use <version> Modify PATH to use <version>
nvm ls List versions (installed versions are blue)
Update: Updated the answer because previous one was not relevant.
Follow below steps to update your node version.
1. Install nvm For this run below command in your terminal
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
Note: If above command did not work for you, you can choose another option to install it. I have added an answer for this here
2. Install specific node version using nvm
For this run
Replace 12.14.1 with your node version
nvm install 12.14.1
Note: If you are getting error of NVM not recognised then run below command and then run above again
source ~/.nvm/nvm.sh
3. Make the installed version default
Note: Replace 12.14.1 with your installed version.
nvm alias default 12.14.1
and
nvm use 12.14.1
4. Check node version
node -v
And that's it.
Extra
To check list of installed node versions -- run below command
nvm ls
Why use any extension when you can do this without extension :)
Install specific version of node
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
Specific version : sudo n 4.4.4 instead of sudo n stable
With package.json - The Maintainable and Portable Way 🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉
Lets each project specify its own version
You can add node as a dependency in package.json and control which version is used for a particular project. Upon executing a package.json "script", npm (and yarn) will use that version to run the script instead of the globally installed Node.js.
The node package accomplishes this by downloading a node binary for your local system and puts it into the node_modules/.bin directory.
You can also do this with the npm (or yarn) packages but you'll need to set your PATH up specifically or use something like npx that handles it for you.
Ubuntu - The Official Way (manually) 😵
If you're on node 12 and want to downgrade to node 10, just remove node and follow the instructions for the desired version:
# Remove the version that is currently installed
sudo apt remove -y nodejs
# Setup sources for the version you want
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# (Re-)Install Node
sudo apt-get install -y nodejs
Windows - The Official Way (manually) 😵
I found myself wanting to downgrade to LTS on Windows from the bleeding edge. If you're not using a package manager like Chocolatey or a node version manager like nvm or n, just download the .msi for the version you want and install it. You might want to remove the currently installed version via "Add or remove programs" tool in Windows.
Windows Package Manager - winget 🎉🎉
The Open Source Windows Package Manager Way
winget install -e --id OpenJS.Nodejs -v 14.9.0
Chocolatey - The Independent Package Manager Way 🎉
Chocolatey is good for keeping installations up to date easily and it is a common way to install Node.js on Windows. I had to remove the bleeding edge version before installing the LTS version:
choco uninstall nodejs
choco install nodejs-lts
Node Version Manager - The "Screw it, I'll do it myself!" Way 😢😢😢😭😭😭😭😭
While not very portable or easily maintainable, some developers like manually switching which global version of node is active at any given point in time and think the official ways of doing this are too slow. There are two popular Npm packages that provide helpful CLI interfaces for selecting (and automatically installing) whichever version you want for your system: nvm and n. Using either is beyond the scope of this answer.
I highly recommend staying away from this option. Even though it's popular, it's an anti-pattern that is sure to cause headaches in the future. Sure, .nvmrc exists, but this is reinventing something that's already a part of Npm. Just npm i node the version you want.
Windows
Downgrade Node with Chocolately
Install Chocolatey. Then run:
choco install nodejs.install -version 6.3.0
Chocolatey has lots of Node versions available.
Downgrade NPM
npm install -g npm#3.10.3
the easiest way i have found is to just use the nodejs.org site:
go to https://nodejs.org/en/download/releases/
find version you want and click download
on mac click the .pkg executable and follow the installation instructions (not sure what the correct executable is for windows)
be happy now that you are on the version of node you wanted
nvmw is no longer maintained, but I found another source that seems to be up to date (as of 1/4/17).
nvm-windows
It works. Allowed me to downgrade to 6.3.1
You need to install NVM for this.
For Mac
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.
Then install the required version using the below command.
nvm install v14.18.0
If you want to check the installed node versions on your system. Use the below command.
nvm ls
If you want to use the specific version from the installed node versions. Use below command
nvm use 12.14.4
Another good library for managing multiple versions of Node is N: https://github.com/visionmedia/n
On windows 7 I used the general 'Uninstall Node.js' (just started typing in the search bottom left ,main menu field) followed by clicking the link to the older version which complies with the project, for instance:
Windows 64-bit Installer: https://nodejs.org/dist/v4.4.6/node-v4.4.6-x64.msi
I had node version 6.4.0 .
As i am need of the older version 6.3.0 , i just installed the 6.3.0 version again in my system. node version downgraded automatically.
So, to downgrade the node version ,
Just install the older version of node js . It will get downgraded automatically from the higher version.
I tried in osx . It works like a charm .
Now the v0.5.0-pre has reached end of life. We can use npx to use any stable older version of node. E.g. the earliest stable version is 4.9.0.
npx -p node#4.9.0 -- node -v
run this:
rm -rf node_modules && npm cache clear && npm install
Node will install from whatever is cached. So if you clear everything out first, then NPM use 0.10.xx, it will revert properly.
Easiest way i found -
Uninstall current version
Download the appropriate .msi installer (x64 or x86) for the desired version from https://nodejs.org/download/release/
Use following commnad with your version number
nvm install v8.9
nvm alias default v8.9
nvm use v8.9
For some reason Brew installs node 5 into a separate directory called node5.
The steps I took to get back to version 5 were: (You will need to look up standard brew installation/uninstallation, but otherwise this process is more straightforward than it looks.)
Install node5 using Brew standard installation, BUT don't brew link, yet.
Uninstall all other versions of node using brew unlink node and brew uninstall node. You might need to use --force to remove one of the versions.
Find the cellar folder on your computer
Delete the node folder in the cellar.
Rename the node5 folder to node.
Then, brew link node
You should be all set with node 5.
If you are like, you already install node version you want but can't seem to switch to it, try this:
nvm use --delete-prefix <version>. npm shows the lates version installed but can't switch to it. If so, this is the output you will see:
You need to run "nvm install v16.2.0
Then run:nvm install <type the version you wish to use here>Your output should look like this:
Downloading and installing node v16.2.0... Downloading https://nodejs.org/dist/v16.2.0/node-v16.2.0-linux-x64.tar.xz... ####################################################################### 100.0% Computing checksum with sha256sum Checksums matched! Now using node v16.2.0 (npm v7.13.0) Creating default alias: default -> v16.2.0
You are done! You can see the latest version by running:
node -v
if you want to go to the LTS version of node which is the stable one from the latest/current version simply run this in your directory.
npm i node
this will install the LTS version of node in your directory.
I faced a lot of issue with this and the only solution that worked for me is :
source ~/.nvm/nvm.sh
nvm isntall node_version_you_want
nvm use node_version_you_want
nvm install 0.5.0 #install previous version of choice
nvm alias default 0.5.0 #set it to default
nvm use default #use the new default as active version globally.
Without the last, the active version doesn't change to the new default. So, when you open a new terminal or restart server, the old default version remains active.
follow the steps if u already install curl its work for me
sudo snap remove curl
sudo apt install curl
nvm install 12.2.0
As i can see this was asked a long time ago, but maybe it can be helpful for someone else.
I can recommend this utility (NVS - Node Version Switcher) to someone who is searching for a way to switching between different versions of NodeJS:
https://github.com/jasongin/nvs
The Easiest way I find to install node.js with a specific version:
First uninstall your node.js
Go to, https://nodejs.org/en/download/releases/ and find your desired version.
Select your version and click on the download button.
Download .msi file

Resources