How To Update Node.js Dependencies

Node.js

01/08/2022


Recently, I decided to upgrade the Node.js environment of my blog. This meant updating npm packages that might have new breaking changes. Here's how I did it. 👇

Updating npm packages

By running npm outdated, you get a list of all your existing packages with information on the currently used version, the recommended version given your Node.js version and the latest publicly available package version.

Output of npm update

Output of npm outdated

Those that require an update will be marked in red, as was the case with @fortawesome/react-fontawesome. To update them, simply run npm update.

Upgrading Node.js

Next up, upgrade your Node environment! The actual procedure itself depends on how you have it installed. If you use a version manager such as nvm, it's as simple as nvm install 16 and nvm use 16.

After doing this, list in package.json the new Node and npm versions under engines.

BASH
$ node -v && npm -v
v16.14.2
8.5.0
JSON
"engines": {
"node": ">=16.14.2",
"npm": ">=8.5.0"
}

Upgrading npm packages

By upgrading your Node.js environment, there will inevitably be packages that don't support it yet. It is up to you to identify these packages, e.g. by building the project.

It's highly recommended to focus on one package at a time and to commit locally regularly. Things can get messy real quick. 🙈

Here's an example of mine. When running my project again, I encountered several of these errors below.

node sass build error

Build error caused by an incompatible package version

In this case, node-sass was giving me troubles. What I did next was:

  • Identify which version of the package was compatible with which version of Node. This turned out quite easy, 😅 but it won't always be.
  • Install the correct version with npm install node-sass@6.
  • Afterwards, I deleted the node_modules folder and package-lock.json file, and attempted to reinstall everything. This makes it easier to spot if other packages are using an older version of node-sass, which I then I had to update as well.
  • Rinse and repeat.

Overall, there's no one-size-fits-all procedure. It comes down to trying things out and hoping for the best. 😬 If things get out of hand, you can always revert to an older commit.


WRITTEN BY

Code and stuff