npm check and update package if needed
Asked Answered
S

22

891

We need to integrate Karma test runner into TeamCity and for that I'd like to give sys-engineers small script (powershell or whatever) that would:

  1. pick up desired version number from some config file (I guess I can put it as a comment right in the karma.conf.js)

  2. check if the defined version of karma runner installed in npm's global repo

  3. if it's not, or the installed version is older than desired: pick up and install right version

  4. run it: karma start .\Scripts-Tests\karma.conf.js --reporters teamcity --single-run

So my real question is: "how can one check in a script, if desired version of package installed?". Should you do the check, or it's safe to just call npm -g install everytime?

I don't want to always check and install the latest available version, because other config values may become incompatible

Strongminded answered 13/5, 2013 at 15:12 Comment(2)
There is an online update/analyze tool pkgui.com/npm see this answer about it stackoverflow.com/a/76342980Alitta
npm upgrade... I don't know what "Team City" is, but for anyone else just using Node.js, you can run npm upgrade.Barthol
W
1282

To check if any module in a project is 'old':

npm outdated

'outdated' will check every module defined in package.json and see if there is a newer version in the NPM registry.

For example, say xml2js 0.2.6 (located in node_modules in the current project) is outdated because a newer version exists (0.2.7). You would see:

[email protected] node_modules/xml2js current=0.2.6

To update all dependencies, if you are confident this is desirable:

npm update

Or, to update a single dependency such as xml2js:

npm update xml2js

To update package.json version numbers, append the --save flag:

npm update --save
Whitethorn answered 13/5, 2013 at 23:40 Comment(6)
Be careful with npm update especially with npm update -g ... it does not what most peaole expect it to do! See: github.com/npm/npm/issues/6247 and gist.github.com/othiym23/4ac31155da23962afd0eFalsity
@Falsity As of [email protected], npm -g update is safe to use again. github.com/npm/npm/issues/6247#issuecomment-92182814Tutelary
Please be aware that npm update will not update your package.json file as stated by the answer from @Erik Olson.Forklift
As of [email protected], 'npm update' will change package.json to save the new version as the minimum required dependency docs.npmjs.com/cli/update.htmlSwirl
just did npm update on my npm 5.6.0 and it broke all code; luckily I backed up my files before doing thatTransvestite
I'll be that guy and say that while npm update is dumb and doesn't update your package.json (what re you supposed to do, find each version number somewhere in your package-lock?), pnpm update works exactly as expected pnpm.io/cli/update. Pnpm is also way faster than npm and handles global packages nicely.Yachtsman
B
566

npm outdated will identify packages that should be updated, and npm update <package name> can be used to update each package. But prior to [email protected], npm update <package name> will not update the versions in your package.json which is an issue.

The best workflow is to:

  1. Identify out of date packages with npm outdated
  2. Update the versions in your package.json
  3. Run npm update to install the latest versions of each package

Check out npm-check-updates to help with this workflow.

  • Install npm-check-updates with npm i npm-check-updates -g
  • Run npm-check-updates to list what packages are out of date (basically the same thing as running npm outdated)
  • Run npm-check-updates -u to update all the versions in your package.json (this is the magic sauce)
  • Run npm update as usual to install the new versions of your packages based on the updated package.json

Or more simply using npx, so you don't have to install anything globally:

npx npm-check-updates -u
npm install
Brunhild answered 1/6, 2014 at 13:13 Comment(12)
npm outdated will show ALL packages.. even inside other packages.. but those won't get updated with this procedure so they will always appear.. so just use npm-check-updates (as you actually recommended) which only shows main packages from package.json ... this is relevantRinghals
With yarn this is much easier just type 'yarn upgrade'.Squinch
Why must I install an update manager to manage my package manager? Do we not agree this is silly? It should be as simple as npm install --all-outdated but it isn't...Extort
You can always run npm update --save package_name to save the latest change to package.json.Interlope
@Extort because npm-check-updates makes major updates, according to semver, which might have breaking changes, whereas npm update does only safe minor and patch updates. See: https://mcmap.net/q/41215/-update-package-to-a-major-release-with-npmHillside
@JoãoPimentelFerreira I understand it's only safe to do minor patches, but eventually devs want to test the latest versions in a controlled way. There's a common need and desire to do this, as demonstrated by the existence of tools to do it, therefore I think the function should just be built in. You don't think it should be built in? I think a flag that was named --force-update-max would be very useful. If they want, they could give it a name to warn people, like how react uses "dangerouslySetInnerHTML" to set raw html. It's a bit patronizing, but it waives their liability for damages.Extort
@Extort yes, I agree with you that it should be something built in, but not by defaultHillside
@JoãoPimentelFerreira Yes, I didn't say anything about making it the default option, I was suggesting to add the flag --all-outdated which is a flag I made up which doesn't exist. The point was that I think there should be a flag that does this. I gave it a better name in my other response to you, --force-update-max, but my intention was the same in my original comment. A flag to update everything to the max, that would be great.Extort
@Extort yes, I agree that --force-update-max would be a better name because it should be clear that breaking changes may occur according to semver. According to semver major updates are not backward compatible. Although the majority of the developers disregards these specifications.Hillside
npm update <packagename> --save-dev // for dev dependenciesEglantine
Running npm update --save after npm outdated doesn't save the new versions in my package.json file. Nor does it install them. How do I update all outdated packages and save the new versions in package.json file?Winthorpe
@Extort because node is a community-based framework, and some group of people are motivated and willing to develop programs that does a better work than the original package manager that is half proprietary? Don't get me wrong but I think this is dope that you have to actually install something from the manager to enhance it (call that a plugin), you can feel that the community is motivated to help each other out.Vicechairman
S
221

There is also a module called npm-check:

npm-check

Check for outdated, incorrect, and unused dependencies.

screenshot of npm-check

It also provides a convenient interactive way to update the dependencies with npm-check -u.

Shedevil answered 14/12, 2014 at 7:7 Comment(0)
K
154

One easy step:

$ npm i -g npm-check-updates && ncu -u && npm i

That is all. All of the package versions in package.json will be the latest major versions.

Edit:

What is happening here?

  1. Installing a package that checks updates for you.

  2. Use this package to update all package versions in your package.json (-u is short for --updateAll).

  3. Install all of the new versions of the packages.

Kashgar answered 13/4, 2017 at 18:51 Comment(4)
@imnickvaughn ncu stands for node-check-updates and -a is the 'upgradeAll' option. Find all options here: npmjs.com/package/npm-check-updatesJaneanjaneczka
And what if I want to do it in one line without using another package like ncu?Extort
Or without the global install, npx -p npm-check-updates ncu -uOdontograph
npm-check-updates makes major updates, according to semver, which might have breaking changes. Use it carefully: https://mcmap.net/q/41215/-update-package-to-a-major-release-with-npmHillside
G
85
  • To update a single local package:

    1. First find out your outdated packages by:

      npm outdated

    2. Then update the package or packages that you want manually as:

      npm update --save <package_name>

This way it is not necessary to update your local package.json file manually.

Note that the above command will update your package to the latest version.

  • If you write some version in your package.json file and do:

    npm update <package_name>

    In this case you will get just the next stable version (wanted) regarding the version that you wrote in your package.json file.

And with npm list <package_name> you can find out the current version of your local package.

Ginni answered 2/9, 2015 at 21:55 Comment(0)
P
52

You can try either of these options:

  1. Check outdated packages

    npm outdated
    

    npm outdated

  2. Check and pick packages to update

    npx npm-check -u
    

    npx npm-check -u

Perturbation answered 11/9, 2018 at 8:50 Comment(0)
W
35

No additional packages, to just check outdated and update those which are, this command will do:

npm install $(npm outdated | cut -d' ' -f 1 | sed '1d' | xargs -I '$' echo '$@latest' | xargs echo)

Washington answered 28/3, 2019 at 20:51 Comment(2)
This is a great answer because it can be put in any shell script to automate this step without relying on having any further package installed.Prospective
@Prospective yes, for those who have this problem, I used all the answers above with more likes but haven't solved the bug for me. in this case, this answer solved it really well! so just using this is a very great answer (tested on bash terminal)Greeley
S
27

NPM commands to update or fix vulnerabilities in some dependency manifest files

  • Use below command to check outdated or vulnerabilities in your node modules.

    npm audit

  • If any vulnerabilities found, use below command to fix all issues.

    npm audit fix

  • If it doesn't work for you then try

    npm audit fix -f, this command will almost fix all vulnerabilities. Some dependencies or devDependencies are locked in package-lock.json file, so we use -f flag to force update them.

  • If you don't want to use force audit fix then you can manually fix your dependencies versions by changing them in package-lock.json and package.json file. Then run

npm update && npm upgrade

Subacid answered 23/10, 2018 at 2:24 Comment(1)
I ran npm audit fix --force. Now I have more vulnerabilities than before. I can run it again, but it will not change. Now what?Iata
P
8

When installing npm packages (both globally or locally) you can define a specific version by using the @version syntax to define a version to be installed.

In other words, doing: npm install -g [email protected] will ensure that only 0.9.2 is installed and won't reinstall if it already exists.

As a word of a advice, I would suggest avoiding global npm installs wherever you can. Many people don't realize that if a dependency defines a bin file, it gets installed to ./node_modules/.bin/. Often, its very easy to use that local version of an installed module that is defined in your package.json. In fact, npm scripts will add the ./node_modules/.bin onto your path.

As an example, here is a package.json that, when I run npm install && npm test will install the version of karma defined in my package.json, and use that version of karma (installed at node_modules/.bin/karma) when running the test script:

{
 "name": "myApp",
 "main": "app.js",
 "scripts": {
   "test": "karma test/*",
 },
 "dependencies": {...},
 "devDependencies": {
   "karma": "0.9.2"
 }
}

This gives you the benefit of your package.json defining the version of karma to use and not having to keep that config globally on your CI box.

Plunger answered 13/5, 2013 at 16:17 Comment(2)
what's in the test script? Can you please give me a clue how you install it with a script.Strongminded
Look at the package.json. Under the "scripts" property, you can define another property, "test" whose value is a command you want to be run when you type npm test. npm docs are pretty good here: npmjs.org/doc/scripts.htmlPlunger
W
7

As of [email protected]+ you can simply do:

npm update <package name>

This will automatically update the package.json file. We don't have to update the latest version manually and then use npm update <package name>

You can still get the old behavior using

npm update --no-save

(Reference)

Wagoner answered 8/7, 2019 at 18:28 Comment(2)
I have [email protected] and it is not automatically updating my package.json. Running npm update <package name> --save did not help either!Octennial
I have [email protected] and can confirm that this is not the default behavior. This is also mentioned explicitly in the documentation.Oxcart
I
7

Just do this to update everything to the latest version - npx npm-check-updates -u

Note - You'll be prompted to install npm-check-updates. Press y and enter.

Now run npm i. You're good to go.

Incinerate answered 26/8, 2021 at 8:12 Comment(0)
K
6

A different approach would be to first uprade the package.json file using,

ncu -u

snapshot of the terminal with the above command and then simply run,

npm install

to update all the packages to the latest version. ps: It will update all the packages to the latest version however if the package is already up to date that package will not be affected at all.

Kastner answered 24/4, 2021 at 6:1 Comment(0)
L
6

3 simple steps you can use for update all outdated packages

First, check the packages which are outdated

sudo npm i -g npm-check-updates

Second, put all of them in ready

ncu -u

Results in Terminal will be like this:

enter image description here

Third, just update all of them.

npm install

That's it.

Lucilius answered 13/7, 2021 at 3:41 Comment(0)
N
5

To really update just one package install NCU and then run it just for that package. This will bump to the real latest.

npm install -g npm-check-updates

ncu -f your-intended-package-name -u
Nucleus answered 28/12, 2018 at 10:21 Comment(0)
F
5

You can do this completely automatically in 2022

  1. Install npm-check-updates

  2. Run the command

    ncu --doctor -u

  3. It will first try every dependency you have and run tests, if the tests fail it will update each dependency one by one and run tests after each update

Filar answered 17/9, 2022 at 11:8 Comment(0)
D
3

If you want to upgrade a package to the latest release, (major, minor and patch), append the @latest keyword to the end of the package name, ex:

npm i express-mongo-sanitize@latest

this will update express-mongo-sanitize from version 1.2.1 for example to version 2.2.0.

If you want to know which packages are outdated and which can be updated, use the npm outdated command

ex:

$ npm outdated
Package             Current   Wanted  Latest  Location                         Depended by
express-rate-limit    3.5.3    3.5.3   6.4.0  node_modules/express-rate-limit  apiv2
helmet               3.23.3   3.23.3   5.1.0  node_modules/helmet              apiv2
request-ip            2.2.0    2.2.0   3.3.0  node_modules/request-ip          apiv2
validator           10.11.0  10.11.0  13.7.0  node_modules/validator           apiv2

Dias answered 21/7, 2022 at 9:6 Comment(0)
V
3

Simple and efficient solution which doesn't require any additional dependencies and allows upgrading all packages to latest versions even if major versions changed:

npm outdated --parseable | awk -F: '{ printf("%s ", $4); }' | xargs npm install

Explanation:

Stage 1: npm outdated --parseable Produces following output (as an example):

/path/to/my_app/node_modules/pakage1:[email protected]:[email protected]:[email protected]:my_app
/path/to/my_app/node_modules/@pakage1/module1:@pakage1/[email protected]:@pakage1/[email protected]:@pakage1/[email protected]:my_app
/path/to/my_app/node_modules/@pakage1/module2:@pakage1/[email protected]:@pakage1/[email protected]:@pakage1/[email protected]:my_app
/path/to/my_app/node_modules/pakage2:[email protected]:[email protected]:[email protected]:my_app

Stage 2: Using awk and specifying : as field separator for it, we extract the 4th fields, which contain latest versions of all packages, and combine them into single line, separating them with space: awk -F: '{ printf("%s ", $4); }', produces following output:

[email protected] @pakage1/[email protected] @pakage1/[email protected] [email protected]

Stage 3: Conducts single call to npm install with output from stage 2 used as additional arguments: xargs npm install produces following command:

npm install [email protected] @pakage1/[email protected] @pakage1/[email protected] [email protected]
Vagrancy answered 5/2 at 20:20 Comment(0)
S
2

One more for bash:

npm outdated -parseable|cut -d: -f5|xargs -L1 npm i
Simla answered 6/7, 2021 at 12:10 Comment(1)
Beautiful solution! Simple and efficient! Just need to update -f5 to -f4 for npm v10 in 2024Vagrancy
C
1

I'm just interested in updating the outdated packages using the semantic versioning rules in my package.json.

Here's a one-liner that takes care of that

npm update `npm outdated | awk '{print $1}' | tr '\n' ' '`

What it does:

  1. takes the output from npm outdated and
  2. pipes that into awk where we're grabbing just the name of the package (in column 1)
  3. then we're using tr to convert newline characters into spaces
  4. finally -- using backticks -- we're using the output of the preceding steps as arguments to npm update so we get all our needed updates in one shot.

One would think that there's a way to do this using npm alone, but it wasn't here when I looked, so I'm just dropping this here in case it's helpful to anyone 😀.

** I believe there's an answer that MikeMajara provides here that does something similar, but it's appending @latest to the updated package name, which I'm not really interested in as a part of my regularly scheduled updates.

Chrisse answered 14/11, 2021 at 15:49 Comment(0)
K
1

If you have multiple projects with the same node-modules content, pnpm is recommended. This will prevent the modules from being downloaded in each project. After the installation the answer to your question is:

pnpm up
Kith answered 4/12, 2022 at 14:1 Comment(0)
C
0

For me, it generally works with using npm-check-updates with peer option

Advantages:

  • Only compatible version of packages will be updated

Installation:

  • [Only first time] npm install -g npm-check-updates

CI/CD (First of second, working in the same manner)

  • [optional] npm i

  • [Any options is ok, both are working in the same way]

    1. npx npm-check-updates --upgrade --peer

    2. ncu --peer

enter image description here

enter image description here

P.s more detailed information can be found there https://www.npmjs.com/package/npm-check-updates

Chassis answered 14/10, 2023 at 14:47 Comment(0)
A
-2

You can use an online tool https://pkgui.com/npm

It edits the package.json with the major versions and allows manual changes via dropdown.

enter image description here

Alitta answered 2/6, 2023 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.