node-essentials
node-essentials copied to clipboard
Best practice for updates
Is this correct?
The recommended workflow for updates is:
- Verify your existing tests pass before starting this update process.
npm audit: to check for vulnerabilities in the current version you are using.npm outdated: to list all the outdated packages. This command provides information in the Wanted, Latest, and Location columns.- The information from
npm auditmay recommend updating to a major version. You should carefully review the breaking changes if any are listed. - Update the version:
npm update <optional package name>: to update the installed packages. If you run this command with a package name specified, the command tries to update only the specified package. If you don't specify a package, the command tries to update all the packages in the package.json file.npm audit fix --force: this command updates the major version of the package. This command can introduce breaking changes. Use this command only if you're aware of the breaking changes and are ready to update your code to accommodate them.
- Verify your tests pass after the update.
Small comments:
-
npm update <optional package name>only updates according to the semantic versioning used: ~ allows patch updates, ^ allows minor version updates. For major version updates, usually the way isnpm install <name>@latest -
npm audit fix --forcedoes not necessarily updates packages, as it tries to fix security issues sometimes it reverts to an older packages version outside of the allowed semantic versioning specified in package.json.
Otherwise, the rest looks fine to me :)
Edited ...
The recommended workflow for updates is:
npm run test: Verify your existing tests pass before starting this update process.npm audit: to check for vulnerabilities in the current version you are using. The information fromnpm auditmay recommend updating to a major version. You should carefully review the breaking changes if any are listed.npm outdated: to list all the outdated packages. This command provides information in the Wanted, Latest, and Location columns.- Update with
npm update:- For smaller projects (a few dependencies in the
package.json: you may trynpm updateto update all dependencies then run your tests. - For larger projects (with many dependencies in the
package.json: update a single package or package family (such as Next.js and React) then run the tests.
- For smaller projects (a few dependencies in the
npm audit: check there are no critical or high vulnerabilities. If vulnerabilities still exist, usenpm updatewith the package name and the major version recommended innpm audit.npm run testagain.- Check in your
package.jsonandpackage-lock.json.
I think the whole content is super good, adding a possible option for people to make it easier to update versions manually: VSCode has an extension called Version Lens, that allows you to see in realtime the current version, the latest version, and the version your package.json will satisfy.
https://marketplace.visualstudio.com/items?itemName=pflannery.vscode-versionlens
If your objective is to go to the latest version in all the dependencies, then just:
- Open
package.json - Manually click the inlay hints to update the text to the major versions (like @sinedied commented, major versions are usually updated by installing only)
- Run
npm updateornpm installto update the versions to the latest ones
This works nicely when you have multiple packages and when you have big projects because you can actually see which are the most impactful dependencies in the system and manually check for major updates one by one.
As a minor note I recall this CLI tool used to be handy: https://www.npmjs.com/package/npm-check-updates
Perhaps it would also be useful to have instructions for other package manager like yarn and pnpm. Also monorepo tools like rush could be good to mention as well.