DEV Community

Jason Sultana
Jason Sultana

Posted on • Originally published at jason.sultana.net.au on

Updating all npm packages via command line

G’day guys!

Have you ever come back to an old JavaScript or TypeScript project after a while - or perhaps created a project using a CLI tool whose packages are a bit behind - and needed to update all of the packages in package.json to their latest versions? It turns out there’s a super convenient way to do this on the command line.

Disclaimer

Here is the obligatory disclaimer for what I’m about to say. Updating packages on an existing project that involves changing a major version is likely to introduce some breaking changes in your usage of the package. You of course should not do this on any production environment unless you’re prepared to face errors descending on your project like the Uruk-hai at the end of Fellowship of the Ring. With that out of the way, let’s see the magic!

npx npm-check-updates -u && npm install

Enter fullscreen mode Exit fullscreen mode

For those of you that were just after the quick fix, you can leave now 😁. Or if you’re the type that likes to wait until the end of the credits for that bonus scene, read on!

What’s this npx business? What happened to npm?

If you’re not a big front-end or nodeJS dev, you might be surprised to see this. npx originally started out as an npm package that allowed you to execute another npm package from the command line, even if it weren’t installed globally or on your path. This is typically used for packages installed as devDependencies in your project, like a database migration tool, project build tools, etc. Since 2017 though, npx has been bundled with the npm cli, so there’s no need to install it nowadays. What’s really cool about npx is that if the package doesn’t already exist in your project, it’ll add it to your dev dependencies and install it before executing.

npm-check-updates is an npm package that performs the update of your packages to their latest versions. To quote their docs on npm, npm-check-updates upgrades your package.json dependencies to the latest versions, ignoring specified versions.

What about the double ampersand (&&)?

That’s just a bit of wizardry that tells your shell (yep, even the Windows shell) to execute the first command, wait for it to complete, then execute the second. This is an easy way to put together a rudimentary chain of commands, without using a proper task execution tool like gulp or grunt.

Anyhoo, that’s all from me today. Happy coding and bye for now. Catch ya!

Top comments (0)