How to use a specific version of NPM? - node.js

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

Related

nvm / node / npm: newer npm with node 12 than with node 14?

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

NPM stuck on an old version?

I was following some tutorial that was recorded a while ago, and it said for us to downgrade our npm version to 5.5.1 to avoid any differences (since that was the newest version at the time of the recording). So I did, using classic
npm install -g npm#5.5.1
However, now I'm stuck, I can't update to the actual present day latest version. I tried npm install -g npm#latest, I tried manually typing the latest version, I even tried uninstalling both Node and NPM, and installing the latest ones, fresh from the site, but for whatever reason my npm just gets stuck on 5.5.1 again. How do I fix this?
I'm using Windows btw. Also, I'm just learning about Node and NPM, so I guess there is a chance I'm missing some obvious solution?
Edit: whenever I try to install something, I get the following
npm WARN npm npm does not support Node.js v12.18.3
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,
npm WARN npm You can find the latest version at https://nodejs.org/
WARNING: You are likely using a version of node-tar or npm that is incompa
with this version of Node.js.
Please use either the version of npm that is bundled with Node.js, or a ve
of npm (> 5.5.1 or < 5.4.0) or node-tar (> 4.0.1) that is compatible with
s 9 and above.
and after that a long list of errors, including, but not limited to
1: 000000013F8A5EBF napi_wrap+114095
2: 000000013F850B46 v8::base::CPU::has_sse+66998
3: 000000013F850EC1 v8::base::CPU::has_sse+67889
4: 000000013F78F187 RSA_meth_get_flags+85639
5: 000000014001AAE0 v8::internal::Builtins::builtin_handle+323456
try this
Run PowerShell as Administrator
Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
npm install -g npm-windows-upgrade
npm-windows-upgrade
refer to this https://www.npmjs.com/package/npm-windows-upgrade or their GitHub repo for more help
if it doesn't help, try this
uninstall node (Settings -> Add or remove programs), then navigate to C:\Users\{User}\AppData\Roaming and delete the npm and npm-cache folder and finally install node again (using the installer).
You are missing the -g. You said:
I tried npm install npm#latest
but that that doesn't include the -g. The -g is key because it causes npm to be installed globally (best rhyme). See Downloading and installing packages globally. Your first command includes it, but not the follow up.
Because of this, you probably install npm as a local dependency (which you probably didn't mean to do). Check your package.json and see if there is any listing of npm. If you aren't directly using runtime pieces of npm, remove that dependency and just install the specific version you want globally with -g.
In your project root directory, run the update command:
npm update

npm vs node version numbers

I just downloaded the latest version of node.js, which at the time of this post is 8.11
Yet my npm version# was still on 5.x, so I ran
npm install -g npm
Now, when I run npm -v, I see 6.1.0
Are the version#'s for Node and npm usually different?
NPM is Node Package Manager. It is the official package manager for Node. That is why NPM is downloaded with Node, but they are 2 totally different pieces of software and evolve separately.
Are the version#'s for Node and npm usually different?
In short, yes, they aren't the same thing - NPM is a package manager and Node is a runtime and both are developed separately with different release cycles.

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 install a previous exact version of a NPM package?

I used nvm to download node v0.4.10 and installed npm to work with that version of node.
I am trying to install express using
npm install express -g
and I get an error that express requires node version >= 0.5.0.
Well, this is odd, since I am following the directions for a node+express+mongodb tutorial here that used node v0.4.10, so I am assuming express is/was available to node v0.4.10. If my assumption is correct, how do I tell npm to fetch a version that would work with my setup?
If you have to install an older version of a package, just specify it
npm install <package>#<version>
For example: npm install express#3.0.0
You can also add the --save flag to that command to add it to your package.json dependencies, or --save --save-exact flags if you want that exact version specified in your package.json dependencies.
The install command is documented here: https://docs.npmjs.com/cli/install
If you're not sure what versions of a package are available, you can use:
npm view <package> versions
And npm view can be used for viewing other things about a package too. https://docs.npmjs.com/cli/view
It's quite easy. Just write this, for example:
npm install -g npm#4.6.1
Or:
npm install -g npm#latest // For the last stable version
npm install -g npm#next // For the most recent release
First remove old version, then run literally the following:
npm install express#3.X
or
npm install express#4.X
and for stable or recent
npm install -g npm#latest // For the last stable version
npm install -g npm#next // For the most recent release
In my opinion that is easiest and fastest way:
$ npm -v
4.2.0
$ npm install -g npm#latest-3
...
$ npm -v
3.10.10
you can update your npm package by using this command:
npm install <package_name>#<version_number>
example:
npm install yargs#12.0.2
You can use the following command to install a previous version of an npm package:
npm install packagename#version
I have a general way to solve this type of problems, which could be helpful too, especially when cloning repositories to run them locally, but requires a little more analysis of the versions.
With the package npm-check-updates I verify the versions of the packages (according to the package.json file) that are not declared in their latest available versions, as shown in the figure (https://www.npmjs.com/package/npm-check-updates):
With this information we can verify the update status of the different packages and make decisions as to which packages to upgrade / degrade and which ones do not.
Assuming that we decided to update all the packages as they are listed, we can use the ncu -u command which only modifies your package.json file. Run npm install to update your installed packages and package-lock.json.
Then, depending on the requirements of the repository, we can refine what is needed, installing the specific versions with
npm view <package> versions and npm install <package>#<version>
The easiest way I found: add package name with the version in package.json and then run npm install
"next-seo": "^5.4.0",
"next-themes": "^0.1.1",
"nextjs-progressbar": "^0.0.14",
If you have to install an older version of a package, just specify it
npm install #
For example: npm install express#3.0.0
You can also add the --save flag to that command to add it to your package.json dependencies, or --save --save-exact flags if you want that exact version specified in your package.json dependencies.
The install command is documented here: https://docs.npmjs.com/cli/install
If you're not sure what versions of a package are available, you can use:
npm view versions
And npm view can be used for viewing other things about a package too. https://docs.npmjs.com/cli/view
Use npm config set save-exact=true if you want to install the exact version

Resources