Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a specific version of NPM?

Tags:

node.js

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

like image 853
Rachel D Roy Avatar asked Nov 08 '12 02:11

Rachel D Roy


2 Answers

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 [email protected]
like image 146
steel Avatar answered Nov 02 '22 14:11

steel


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 [email protected] 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 [email protected]

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

like image 29
Puzzlefactory Avatar answered Nov 02 '22 14:11

Puzzlefactory