How to update all Node.js modules automatically?

て烟熏妆下的殇ゞ 提交于 2019-12-30 00:42:48

问题


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.


回答1:


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",
        "eslint-i-g": "npm i eslint@latest -g",
        "stylelint-i-g": "npm i stylelint@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 eslint-i-g && npm run stylelint-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. Assure that 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 step #3
  4. Open console with administrative permissions
  5. In the console, go to the folder with package.json from 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.

Notes:

  1. To run package.json use the value of _cmd-update-all-modules property
  2. An installation of ESLint & Stylelint in the script is for the reference only


来源:https://stackoverflow.com/questions/34202617/how-to-update-all-node-js-modules-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!