15

During my work with the Node.js environment, I faced the issue of version maintenance of Node.js modules. I would like to be sure that all internal Node.js modules are updated.

Many of existing manuals focus just on how to update Node.js modules, but not how to automate such routine.

The question:
How to update all Node.js modules automatically to the latest version?
Ideally, it should be some script, job, or task.

Mike
  • 12,690
  • 24
  • 87
  • 142
  • Declaring versions in `package.json` i believe is enough , you pretty much configure everything in there. Your question seems more similar to "maintaining an npm registry".. – Gntem Dec 10 '15 at 13:33
  • Hey, it sounds like you answered your own question here – Halfpint Dec 11 '15 at 01:05
  • 1
    Possible duplicate of [How do I update each dependency in package.json to the latest version?](https://stackoverflow.com/questions/16073603/how-do-i-update-each-dependency-in-package-json-to-the-latest-version) – Suyash Mittal Feb 20 '19 at 10:56

3 Answers3

32

To update all Node.js modules manually:

  1. Open console with administrative permissions
  2. Go to Node.js installation folder: cd C:\Program Files\nodejs
  3. Update npm: npm i npm@latest
  4. Go to modules folder: cd C:\Program Files\nodejs\node_modules\npm
  5. Install all desired modules: npm i %MODULE_NAME%@latest
  6. Install update manager: npm i npm-check@latest -g
  7. Available updates for locally installed modules: npm-check -u
  8. Available updates for globally installed modules: npm-check -u -g
  9. Recursive update of all locally installed modules: npm update --depth 9999 --dev
  10. Recursive update of all globally installed modules: npm update --depth 9999 --dev -g
  11. Clear the cache: npm cache clear --force

To update all Node.js modules automatically:

  1. Create a package.json:
{
    "_cmd-update-all-modules": "npm run update-all-modules",
    "scripts": {
        "create-global-node-modules-folder": "if not exist \"%appdata%\\npm\\node_modules\" mkdir %appdata%\\npm\\node_modules",
        "npm-i-g": "npm i npm@latest -g",
        "npm-check-i-g": "npm i npm-check@latest -g",
        "npm-check-u-l": "npm-check \"C:\\Program Files\\nodejs\\node_modules\\npm\" -y",
        "npm-check-u-g": "npm-check \"C:\\Program Files\\nodejs\\node_modules\\npm\" -y -g",
        "npm-deep-update-l": "npm update --depth 9999 --dev",
        "npm-deep-update-g": "npm update --depth 9999 --dev -g",
        "npm-cache-clear": "npm cache clear --force",
        "update-all-modules": "npm run create-global-node-modules-folder && npm run npm-i-g && npm run npm-check-i-g && npm run npm-check-u-l && npm run npm-check-u-g && npm run npm-deep-update-l && npm run npm-deep-update-g && npm run npm-cache-clear"
    }
}
  1. Specify all desired modules to be installed in the scripts section
  2. Make sure the folder with Node.js, e.g. C:\Program Files\nodejs, is added to the PATH through the Environment Variables
  3. Copy package.json to the folder with Node.js from the step #3
  4. Open console with the administrative permissions
  5. In the console, go to the folder with package.json from the step #3
  6. Execute npm run update-all-modules

Both of these approaches allow you keeping all Node.js modules updated to the latest version, wherever it is installed locally or globally.

To run this package.json, call npm run update-all-modules, stored as a hint inside of the _cmd-update-all-modules property.

Mike
  • 12,690
  • 24
  • 87
  • 142
  • Could you clarify a little bit more what you mean by, "To run package.json use the value of _cmd-update-all-modules property"... Use it where? – Atticus29 May 09 '20 at 04:08
  • 2
    @Atticus29, I've elaborated the text, hope, now it is clear. – Mike May 09 '20 at 07:36
5

Just need to follow below commands

  1. npm install -g npm-check-updates

  2. ncu -u

  3. npm update

  4. npm install

Explanation:

  1. To update all packages to a new major version, install the npm-check-updates package globally.

  2. This will upgrade all the version hints in the package.json file, to dependencies and devDependencies, so npm can install the new major version.

  3. You are now ready to run the update.

  4. Now install updated packages. Flag --force sometimes required if already exists some conflicting packages.

Reference link: https://nodejs.dev/learn/update-all-the-nodejs-dependencies-to-their-latest-version

Benno Kress
  • 2,487
  • 25
  • 36
Daniyal
  • 510
  • 5
  • 9
0

I went to https://nodejs.org/en/download/ downloaded the installer and repaired the installation, all warnings and errors disapeared. hope helps someone :P

Setar
  • 144
  • 10