DEV Community

Cover image for Avoid npm build errors by tracking your Node version with nvm
Chris Colborne
Chris Colborne

Posted on • Originally published at chriscolborne.com on

4 2

Avoid npm build errors by tracking your Node version with nvm

You know the feeling. You get a spare moment jump in and do some updates on an old app. But you try to get it running and the dreaded npm errors come up.

Oftentimes, the issue is that you’ve updated Node since you last ran it. Many issues with dependencies and npm can been easily solved by switching Node versions.

Easy, so just go back to the version of node you used when you originally developed it. But how do you know which version to jump to? Just guess and check?

Perhaps you can leave a note in your README saying “This works in Node version X.” And sure, that’s certainly one way of solving it.

But using nvm, we can track the version, as well as automate the switch back, and that’s what we’ll talk about today.

nvm is Node Version Manager. It primarily allows you to switch between isolated Node environments for different versions. If you’ve used rvm/rbenv in Ruby, it’s similar to that.

For our purposes today however, the killer feature is the .nvmrc file. .nvmrc is a file you commit with your project which contains a single line with the version of Node to use for this project.

In a project with an .nvmrc file, you can type nvm use and it will automatically switch back to the version you’ve stated. If you’re really keen, you can (and I’d say you should) also set up your shell to automatically nvm use when it encounters an .nvmrc file.

So start using nvm and .nvmrc in your projects and next time you’re returning to an old project, you’re just an nvm use away from being back up and running quick smart.

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (2)

Collapse
 
liloxjah profile image
Kwadwo Annie
Comment hidden by post author

Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay