nvm / node / npm: newer npm with node 12 than with node 14? - node.js

I'm using nvm to build libraries that require specific (different) Node versions.
When I do:
nvm use 12
I get:
Now using node v12.21.0 (npm v8.14.0)
But when I do:
nvm use 14
I get:
Now using node v14.18.1 (npm v6.14.15)
I'm very surprised to see an older npm version with a newer Node version. Is that the way it's supposed to be?
When I do nvm use 14 I'd expect it uses the latest Node 14.x with whatever latest npm version is available for that Node version. Or am I misunderstanding how this works?

Node.js is distributed with a version of npm, but npm is still a seperate package that can be upgraded.
The distributed versions can be retrieved from node release info
node npm
v12.22.12 6.14.16
v14.20.0 6.14.17
v16.17.0 8.15.0
v18.9.0 8.19.1
npm can prompt you to upgrade when it detects an old version, and you will also to run into this instruction in the wild a fair bit:
npm install -g npm
This will grab whatever the latest version of npm is and is likely what happened in the nvm 12 environment.
Sometimes moving to a new major version of npm can be a source of trouble for projects that have already been setup with a previous major npm release and some behaviour changes. CI tasks often have a npm install -g npm and one day they start falling over for what seems like no change. I've also seen npm drop support for an old version of node, so a npm install -g npm ends up with a broken npm.
If you want to keep a fixed major release, like v6 that Node 12 was release with, use:
npm install -g npm#^6
or to use a specific version
npm install -g npm#6.14.16

Related

npm does not support Node.js v9.0.0

I just upgraded my project to use Node.js v9.0.0. When I start my project using npm, it tells me I should upgrade to a newer Node version, but I believe this is the newest version. I also believe I'm on the newest version of npm (5.5.1).
Do I need to worry about this warning? Is there a good way to resolve the issue?
Here's the full warning:
npm WARN npm npm does not support Node.js v9.0.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8.
npm WARN npm You can find the latest version at https://nodejs.org/
Now using node v9.0.0 (npm v5.5.1)
I've just upgraded to Node v9 and ran into the same issue. There's a Github issue for this here.
Following the advice to uninstall a global instance of npm has resolved the issue for me, using:
npm uninstall -g npm
With Node 10.6.0 on Windows 10 with Git Bash installed, I did this and all worked well:
1. Launch Git Bash terminal.
2. cd ~/AppData/Roaming
3. rm -rf npm
4. rm -rf npm-cache
5. npm install -g npm#latest
Check version and test on an existing project to verify (cd to project dir)...
6. npm -version
7. npm install
NB: Performing steps 2 through 4 via Windows Explorer could be faster.
9.0.0 is indeed the latest version available at the moment. The reason that NPM shows you this message is because NPM hasn't updated their unsupported.js yet to accept 9.0.0 as a supported version.
I would say that you can probably use it without problems, but if you stumble upon one, know that it's probably caused by a breaking change in Node.js 9.0.0 and will very likely be fixed by the NPM team soon.
npm does not support Node.js v9.1.0
Uninstall did not work for me
npm uninstall -g npm
Node.js v9.1.0 comes bundled with npm v5.5.1. The latest version of npm is also v5.5.1, however, the version bundled with Node.js isn't exactly the same as the original version from npm.
The following is what I needed to do on MacOS
To completely uninstall node + npm, do the following:
1. go to /usr/local/lib and delete any node and node_modules.
2. go to /usr/local/include and delete any node and node_modules directory.
3. download and install node from nodejs.org
Latest update Jan 2018: NPM has released version 5.6.0 to work with this.
How to upgrade npm:
Note if you are using node version 9 you will not be able to upgrade npm directly, since npm does not support version 9, so using nvm, downgrade node to latest stable release, that will make npm work, then use commandnpm install -g npm#latest to upgrade npm.
Install nvm:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash\n
export NVM_DIR="$HOME/.nvm"\n[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Download and use node X version:
nvm install 4.4.2
nvm use 4.4.2
node -v
[sudo] npm install -g npm#5.3.x remove this problem for me. ubuntu 16.10

Downgrade npm to an older version

I tried updating npm to see if it would solve some dependency problems we were having, and now I want to downgrade to the version the rest of the development team is using. How can I install an older version?
I updated npm according to the instructions on the About npm CLI versions:
The latest release of npm
The latest release of npm is the most recent stable version. When you install Node.js, npm is automatically installed. However, npm is released more frequently than Node.js, so to install the latest stable version of npm, on the command line, run:
npm install npm#latest -g
Just replace #latest with the version number you want to downgrade to. I wanted to downgrade to version 3.10.10, so I used this command:
npm install -g npm#3.10.10
If you're not sure which version you should use, look at the version history. For example, you can see that 3.10.10 is the latest version of npm 3.
Just need to add version of which you want
upgrade or downgrade
npm install -g npm#version
Example if you want to downgrade from npm 5.6.0 to 4.6.1 then,
npm install -g npm#4.6.1
It is tested on linux
npm install -g npm#4
This will install the latest version on the major release 4, so no need to specify version number. Replace 4 with whatever major release you want.
Even I run npm install -g npm#4, it is not ok for me.
Finally, I download and install the old node.js version.
https://nodejs.org/download/release/v7.10.1/
It is npm version 4.
You can choose any version here
https://nodejs.org/download/release/

How to have a different version of npm for a particular version of Node.js?

If the requirement is to use Node.js v4.x, and npm v3.x, I was able to use
nvm install --lts=argon
to install Node v4.8.3 LTS. But on the Node / npm release page, it says Node v4.8.3 comes with npm 2.15.11. And npm 3.x comes with only Node v5.x.
In this case, how can we make npm v3.x while keeping Node at version 4.x?
(and since npm v3.x can be 3.3.6 to 3.10.10, which version should be installed?)
I think I found the answer. It is to use npm install to upgrade itself:
npm install npm#3.10.10 -g
(or any version you want).

npm -v still showing version 2.x.x after installing npm3

I am starting to use Angular 2, which requires npm 3.x.x, so I installed it with npm install -g npm3. The Angular 2 tutorials tell me to verify that I have npm 3.x.x by running npm -v, however when I run that, it still says I have version 2.15.8. It shows that I have version 3.10.7 if I run npm3 -v, but not if I run npm -v. Is this going to cause errors? Do I need to it to say I'm using version 3.x.x when I run npm -v? If so, what do I need to do?
EDIT:
Node.js version: node -v reveals that I am using v4.4.7 (Angular2 requires something between 4.4.x - 5.x.x, so I should be good here)
OS: I am currently using Windows 10 on a PC, but have had the same issue with my Mac.
Upgrade npm using this command - npm install -g npm
Maybe this helps:
https://www.google.de/search?q=update+npm+windows
Updating npm with npm itself will store npm.cmd in ../appdata/roaming/.
To avoid conflicts with an older npm version you need to remove npm.cmd from C:\Program Files\nodejs\npm.cmd

How to use a specific version of NPM?

How can I switch which version of npm I'm using?
Currently:
$ npm -v
1.1.65
But I need: 1.0.x
I tried but got an error:
$npm version 1.0
npm ERR! version No package.json found
Anyone know how to use a different version of NPM? Thanks
Your NPM version is tied to your NodeJS version. As far as I can tell you can only have one NPM version per Node version. Using something like nodenv or, my favorite, asdf, you can define your node version per folder.
Per Node version (e.g. per folder) you globally install the version of NPM that you want to use.
$ npm install -g npm#x.x
Edit: You can also now specify NPM version in your package.json "engines" key, though it takes a little more work to enforce that declaration.
You can update npm without installing another version of node.js and npm is not tied to versions of node.js specifically.
Of course, as node.js advances and adds features so too does npm so there is some limit for npm depending on node.js but you SHOULD update npm as much as you can because there are important security patches and bug fixes.
This is a best practice for all software. Stability is not worth ignoring a security risk.
The command is npm install npm#latest -g to install it globally. This will install the latest version that will run with the node.js you have installed.
Additionally you can install a specific version of npm to your package.json in a project like this npm install npm#6.14 and you can use it locally. What is even more interesting is you can install a local version of node.js in a project and use it too!
Example:
I am running node 16.8.0 and I have npm 7.21.0 but I want to use an earlier version of npm but just for a project:
In the project directory
npm i --save-dev npm#6.14
This will put the earlier version of npm into the node_modules/.bin which will let you run it in relation to this project. If you type npm -v at this point you will get your global version.
If you type node_modules/.bin/npm -v you will get 6.14.15
Since you have made this version of npm the one for this project, when you use npm in your scripts it will use 6.14.15 and not the global version.
The same is true of node.js.
You can install an older version of node.js like this: node_modules/.bin/npm install --save-dev node#lts which will then add this version of node.js to the project.
You can test this by entering node_modules/.bin/node -v and in my example you will see the locally installed version number, in my example v14.17.3 although my global is v16.8.0
If you do this, your project scripts in package.json scripts will run the locally installed versions rather than the global versions.
You can test this out by creating a script in your package.json.scripts like this: "what:version": "npm -v && node -v",
Then if you run npm run what:version you will get
> what:version
> npm -v && node -v
6.14.15
v14.17.3
What value is this?
It is a way for you to package node and npm with your project. This can be especially valuable for large projects where you have many people working on the same project, so you have consistency across the board or if you have a build server that has a specific version of node + npm. Additionally if you have corporate requirements, etc.
Additionally there is npx which allows you to do so much more. Using npx you can even try something out without installing it.
Here is the documentation for npm: https://docs.npmjs.com/about-npm-versions
If you find you are needing to switch between different versions of node.js and npm you might want to use nvm https://docs.npmjs.com/downloading-and-installing-node-js-and-npm
When you install npm you only get the latest package, so you need to install specific versions individually, e.g:
npm install -g npm#5.6.0
Once you've done that, run a version check and you should see the version you've just installed:
npm -v
5.6.0
You can install specific version by bellow command.
npm install -g npm#6.14.11
For a specific version, use
npm install -g npm#x.y.z
And for the latest version, use
npm install -g npm#latest

Resources