Makefile 🤝 JavaScript

29 May 2020

How many times have you heard this?

"I ran npm install and it failed."

"I installed a new module and the lockfile is totally different."

"The app won't boot."

And how many times has the answer been:

"What version of node are you running?"

I can hear you clacking away at your keyboard: "Use a version manager!"

And to that I say: We do! But you have to either manually activate the version manager (nvm use, fnm use, etc.), or install a script that runs constantly in your shell. Many folks don't want to do the latter, and we often forget to do the former.

Make to the rescue (again)

If you've read the other articles in this series, you know we love using Make as a task runner, similar to how you use npm test or npm run prettier.

Well, when we use Make with JavaScript (and most other languages), you still use npm, except it's wrapped in a Make target like:

test:
	npm test

The special sauce is a few lines of JavaScript we put into a tools/checkversion.js file in each repo:

const fs = require("fs");

const nvmVersion = fs.readFileSync(".nvmrc").toString().trim();
const desired = `v${nvmVersion}`;
const running = process.version;

if (!running.startsWith(desired)) {
  console.error(
    `You are running Node ${running} but version ${desired} is expected. ` +
      `Use nvm or another version manager to install ${desired}, and then activate it.`
  );
  process.exit(1);
}

You may need to modify that script if you want to use .node-version instead of .nvmrc or if you use a different version manager.

Then in the Makefile, we guard all the targets that invoke node and npm to check the active node version. For example, part of the Makefile for our very own marketing site looks like this:

check-version:
	@node tools/checkversion
install: check-version
	yarn install
develop: check-version
	gatsby develop
build: check-version
	gatsby build
serve:
	gatsby serve
run-prod: build serve
fmt: check-version
	yarn run format
	yarn run sort-imports
test: check-version
	yarn test

So if you cd over to a JS project and you forget to activate your version manager, instead of some cryptic error, it looks like this:

$ make test
 You are running Node v12.16.1 but version v12.10 is expected. Use nvm or another version manager to install v12.10, and then activate it.
 make: *** [check-version] Error 1
$ nvm use
 Found '/Users/rob/lithic/lithic-marketing-gatsby/.nvmrc' with version <12.10>
 Now using node v12.10.0 (npm v6.10.3)
$ make test
 yarn run test
 yarn run v1.22.4

WOW, isn't that better! We strongly suggest you take this JS snippet and Makefile with you on your current and future projects. And if this was helpful, you should also check out the rest of this series: how to use wildcards using application presets, and configuring Make targets with dotenv.

Don't miss next time

Next time, we'll wrap up this series by addressing why we use Make as a task runner in more holistic and philosophical terms. To balance the theoretical with the practical, we'll also include a bunch of snippets we have used in our Makefiles over the last year, that we go back to over and over.

Find this post useful, or interested in learning how we can help you make your next project a success? Please get in touch.

Lithic Tech was founded in Portland, Oregon by experienced software engineers with a track record of high-quality software, happy customers, and successful businesses. We believe this success is a result of our unique processes, patterns, and convictions.

We are now offering that expertise to the wider community to build better software and software teams. You can learn more about us, and we’d love if you got in touch.

Email us at hello@lithic.tech or use this form: