1184

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?

ux.engineer
  • 10,082
  • 13
  • 67
  • 112
stewart99
  • 14,024
  • 7
  • 27
  • 42
  • 7
    Why don't you just update your Node version? Pretty sure there should be many more good additions than broken behavior that you will find. – Fabrício Matté Apr 08 '13 at 23:58
  • 1
    Sometimes that's not an option. If he used nvm to downgrade, there might be a reason, such as some other group controls the executable node version he has to use. – fool4jesus Jun 24 '19 at 12:35

10 Answers10

1952

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

Bret Copeland
  • 22,640
  • 1
  • 25
  • 25
  • 5
    I believe this will install the nearest major version that matches, so it might not be what you expect http://stackoverflow.com/a/22345808/1074400 – A F Jun 17 '15 at 18:33
  • 2
    @AakilFernandes if you specify an exact version, an exact version will be installed. If you specify a semantic version range, then you might get a non-exact match. There's nothing unique about the install command in that respect. – Bret Copeland Jun 20 '15 at 18:49
  • Also worth noting that when you run 'npm install express' you are actually running 'npm install express@latest' – brad oyler Aug 07 '16 at 04:55
  • 7
    `npm view versions -json` to see every single version, avoiding the ellipsis at the end of a list with many versions. – Chunky Chunk May 17 '17 at 06:47
  • 13
    If you use `npm install express@3.0.0`, you won't get the exact version 3.0.0, you'll get the latest 3.x.x version. To get the specific version, you have to use `npm install express@3.0.0 --save-exact`. See this blog post: https://60devs.com/npm-install-specific-version.html – Patrick Hund Jan 10 '18 at 09:53
  • 7
    @PatrickHund no, `npm install express@3.0.0` will get you _exactly_ version 3.0.0. `npm install express@^3.0.0` would get you the latest 3.x.x. `--save-exact` affects how it's written to packages.json, which I already covered in my answer. Also note, `--save-exact` has to be used in combination with either `--save` or `--save-dev` - it's not enough to use it on its own. – Bret Copeland Jan 25 '18 at 18:48
  • 2
    i can confirm that you will NOT get exact version with ^1.2.3, our build pipeline then tried to pull in 1.2.10 (latest sub version) and the build failed. 'npm i 1.2.3' puts the '^' which will not guarantee an exact match when the project builds especially in another location. – JesseBoyd Mar 12 '21 at 17:27
133

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
inaps
  • 1,516
  • 1
  • 9
  • 14
  • 2
    Thanks for the `latest` and `next` version tags! – Jimmy Adaro May 15 '19 at 16:35
  • 9
    @inaps you *might* add a note that the `-g` flag is specifically for packages you want installed globally as a lot of users will get to this page and merely copy/paste without realizing how they are about to impact their package ecosystem. We've all been "that guy" – Jacksonkr Apr 26 '20 at 15:27
  • this will install the exact version locally but will put '^4.6.1' in package.json which means other developers or build tools may get another subversion which may not be what you want and cause a build to fail. – JesseBoyd Mar 12 '21 at 17:34
110

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
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78
  • 19
    Is that a literal `X` or a stand-in for some numeric version number? – Keith Thompson Apr 19 '16 at 18:32
  • 8
    That was an either/or question, not a yes/no question. I tried `npm install express@3.X`, and it seemed to work. Is that a feature or an accident of the way `npm` parses the version number? – Keith Thompson Apr 20 '16 at 15:06
  • 4
    @KeithThompson Yes, it is! Hehe, just kidding... It's the way `npm` parses it, see: https://docs.npmjs.com/misc/semver#x-ranges-12x-1x-12- – gonz May 30 '16 at 21:31
  • 7
    @gonz: So it's a literal `X`. – Keith Thompson May 30 '16 at 21:34
  • 2
    I just wanted to address why did that work for you. I don't know Saurabh's original intention or what you are trying to do. 3.X would mean >= 3.0 and < 4.0. – gonz May 30 '16 at 21:43
  • 1
    This answer needs to be edited to explain the `.X`. – Wyck Sep 24 '21 at 20:37
46

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

9

you can update your npm package by using this command:

npm install <package_name>@<version_number>

example: npm install yargs@12.0.2

MBehtemam
  • 7,865
  • 15
  • 66
  • 108
Mehedi Abdullah
  • 772
  • 10
  • 13
3

You can use the following command to install a previous version of an npm package:

npm install packagename@version
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Pradeepa
  • 107
  • 4
3

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):

enter image description here

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>

2

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",
raqibnur
  • 59
  • 1
  • 7
0

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

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 10 '22 at 07:38
0

Use npm config set save-exact=true if you want to install the exact version