Updating to stable version of nodejs with n - node.js

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

Related

How Can Knex (or Any NPM Package) Run a Different Version of Node?

I'm trying to use Knex's seed:run command (ie. npx knex seed:run), and inside my seed file I've added this line, which logs the version of Node that it's being run with:
console.log(process.version);
Now, if I run that exact same line of code in the Node command prompt itself (node), I get:
v14.2.0
Similarly, when I run node --version it correctly returns v14.2.0.
However, when Knex runs my seed file, I instead see:
v11.15.0
Can anyone explain how/why Knex is using a different version of Node, and how I can make it stop?
P.S. I've tried wiping my node_modules folder out and re-installing via npm i, but Knex continues to use an old Node somehow. Knex itself is installed at the latest version (0.21.1).
This may happen if you have a global installation that runs in privileged account (root), and a local installation to current user.
It is possible you are using some node versioning tool to manage nodejs versions, it creates a symbolic link to a managed version of node that overrides the default configuration. There are many tools that does that like nvm, n.
Danizavtz's answer didn't solve thigns for me, but it did point me in the right direction. I realized that I did have two versions of Node: one installed by Linux (using apt-get), and one I'd installed myself (to get a newer version of Node).
The solution for me was to remove the system version of Node with:
sudo apt-get remove node
That left me with a working Node ... but it removed my npm. I guess I could have just re-installed everything, but since I already had node and just needed npm, I downloaded the latest Node installation and used the node in it to install npm. I did this both with and without sudo (I'm not sure if doing it without sudo was even necessary, as it didn't fix things):
sudo ~/node-v14.3.0-linux-x64/bin/node ~/node-v14.3.0-linux-x64/bin/npm i -g npm
After that I finally got the expected Node version when I ran npx/npm!

Changing from existing nodejs installation through apt to NVM

I am on Ubuntu 18.04 with existing NodeJS installed through apt.
(currently on version 9.11.1)
I want to uninstall this and switch to NVM without destroying other dependencies (like yarn) and preferrably my whole system. I need to do these cleanly and I don't want to risk missing a step. Can you please help me?
(nvm maintainer here)
You can install nvm on top of your system node, without losing anything (although node and npm themselves will be shadowed by the nvm-managed node).
Separately, you can run npm ls -g --depth=0 to get a list of your global modules, and then you can reinstall those after you've installed and activated an nvm-managed node.
If you installed yarn a different way than npm install -g yarn, then uninstalling your system node/npm shouldn't impact it.

How to uninstall n and all node versions installed by n

I have installed some Node.js versions with the tool n. I have uninstalled all Node.js versions using:
sudo n prune
except the current (activated) version. If I try to unsinstall the current version:
sudo n rm 6.10.2
I get the following error.:
Error: cannot remove currently active version (node/6.10.2).
I can not figure out, how to set the systems (Arch Linux) default Node.js version, which is already installed and was used to install n.
If uninstall n using:
sudo npm uninstall -g n
it leaves the current version on my computer.
ls /usr/local/n/versions/node/
6.10.2/
Do I need to manually delete the folder: /usr/local/n/? Or, is there an option in n to uninstall it and all the Node.js versions installed by n, so that I get the setting before I installed n?
You need to manually remove Node versions installed by n and set the correct Node system version.
sudo n prune
sudo npm uninstall -g n
sudo rm -r /usr/local/n
sudo rm /usr/local/bin/node
See the following issues for n on GitHub:
Cant uninstall n and node #327
Simple command to switch back to the system installation of Node #486
There have been some changes in n, so I'll provide an updated and longer answer.
First some context. n installs by default to /usr/local. If that is also the location of the "system" install of node and npm then it is overwritten. The cache of previously downloaded versions is kept in /usr/local/n.
Removing cached node versions downloaded by n:
n prune removes all but the version matching the active version of node
since n v3.0.0, deleting the active version is allowed using n rm <version>
(but just manually deleting /usr/local/n is easy and removes the top folder too!)
Removing installed node and npm and npx et al:
since n v4.1.0, there is n uninstall. (You will need to reinstall the system node and npm to keep using them if they were installed to same location and overwritten.)
Uninstalling n itself: if you installed it with npm install n then you uninstall it with npm uninstall n. This does not affect the cached versions of node or the installed version of node, just n. (And there is a small catch-22 if just deleted npm by running n uninstall!)
I'm on mac OSX, so I'm not sure if this is relevant to your use case or not, but it may help.
It looks like n may run into issues if you remove the current version, so I suggest that you delete the folder: /usr/local/n/
Then try to uninstall 'n'. In my case, running npm uninstall -g n did not remove n. Instead, I needed to find the current installation of n by running: which n, then removing that folder from my system.
Then, I would suggest that you use NVM instead.
I've found that it's the easiest and most reliable solution that I've found to date.
More specifically, to address your question about how to access the system installation of node, I don't know what OS you are running, but I don't know that there is a system node version.
Typically, you have to manually install node. If there were a system node version, I would expect that it would become outdated very quickly, considering node, and other tools, typically iterate through changes fairly quickly.
I hope this works for you.
Run n-uninstall to uninstall n as well as the Node.js versions that were installed with it.
n-uninstall -y skips the confirmation prompt - use with caution.

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..

How to upgrade node.js on Windows?

I already have Node.js v0.8.0 running on Windows. Can I just run the latest installer to upgrade it to v0.8.4? I am afraid it will break existing third party modules on my machine.
Yes, you just install the latest version. Generally you shouldn't have any compatibility problems if you are already using the same major version (e.g. Version 0.8.x). If you are concerned about changes, you can always check the changelog for each version (link to changelog is on node.js download page at nodejs.org). That should tell you of any big changes (i.e API changes, etc).
For the record, I have just gone through the process, and it is painless even if you upgrade to another major version.
I have moved from 0.8 to 0.10, using the .msi package, overwriting the one installed on my system. Package problems were all fixed with npm update -g. Worked like a charm.
In case it does not work like a charm:
npm cache clean usually fixes the problem. Once the cache is empty, just run npm update -g again.
In case you really run into trouble:
Delete the modules you have installed globally, then reinstall them. Here's how:
Take stock of what you have:
npm list -g --depth=0 lists all top-level packages, with version numbers.
npm list -g --parseable --depth=0 > npm-global-modules.txt writes them to a file in your cwd.
Any strange stuff you didn't install yourself has probably been installed by another module (rare, but I have seen it happen). Remove those modules from the list. Also remove the module "npm".
In an editor, format the output for the command line by replacing \n?[^\n]+[\\/] (regex) with a single space.
(I didn't get this to work with findstr in a pipe, hence the roundtrip to the editor. You can also do it manually, of course ;)
Delete all modules. On Windows, delete (or rename) the %appdata%\npm directory. For other OS, see Command to remove all npm modules globally?
Reinstall the modules with npm install -g [your module list here]. Don't forget to npm cache clean before you do it.
I don't have experience with node on Windows, but I have just upgraded node & modules on my Mac, so this is just a general answer:
If you install v0.8, you might break your existing node modules, if they use deprecated functions, etc. The problem is that npm only checks your version of node while modules are being installed, not at run-time.
To be on the safe side, you need to find the global node_modules folder on your machine, back it up to somewhere, then delete and reinstall the modules. You will need to do the same thing for the node_modules folders in the apps you are using. (Assuming you have package.json files, reinstalling these should be easy.)
In practice, I don't think any of the modules I was using were actually incompatible. Good luck.
Yes. You can upgrade your node.js version to the latest by running the installer for latest node.js version at https://nodejs.org/en/. I upgraded mine from 4.4.4 to 8.11.2 running the installer.
Unless you're using a module that relies on an actual bug that was present in 0.8.0 and was fixed by 0.8.4, you're OK. There were no API changes in between those two versions (and the node team is too smart to introduce such changes in a minor release).
Currently upgrading to 4.4.*. I just used to installer from the nodejs.org website and that upgraded everything works just fine.
Just go to nodejs site & download it. You can install it directly without any hesitation. If you have any dependency on earlier version then check change logs.
JUST GO TO
nodejs.org
INSTALL THE LATEST STABLE VERSION (Recommended For Most Users)
and then run
npm install node
Now, you are good to go
You can even check the version of NodeJS using command
node --version
Best way to install node on windows is by using nvm-windows, so you can quickly switch between versions if you need to. This is analogous to the best way of installing node on linux and max, ie with nvm.
But Benjen is right (how could he not be with all his scouting experience) that you can just install a different version of node, and your version will update. Your npm dependencies may have to be reinstalled, and any extensions that aren't managed by npm may need to be recompiled, but this will be true no matter how you change your node version.

Resources