Simple build tools: npm scripts vs Makefile vs runjs

Paweł Gałązka
Oct 3, 2016 · 5 min read
Image for post
Image for post

Command line based build tools gain a lot of attention in the JavaScript world recently. They are straightforward, flexible and quite easy to manage compare to verbose Grunt.js/Gulp.js files and workflows. This goes with the way of being agile and adaptable which is more and more important in the fast changing JavaScript world. But which command line based build tool to choose ? What’s the differences between them ?

Quick sum up of complex build systems

Main disadvantages:

  • Dependence on plugins and their documentation. They are not always up to date, can break later and cause pain with the debugging. Docs are spread out across different projects and finding things which you are looking for can be really hard.

Further reading:

Simple build tools

We have 3 candidates: npm scripts, Makefile and runjs. All of those are based on command line scripting. You don’t need much of knowledge about those tools. They are very easy with their construction and workflow. What is important is how you can effectively manage command line scripts.

Main advantages:

  • shallow learning curve

npm scripts

Built-in feature into npm package manager. Content of each task is held in package.json under “scripts” section. Each task can be called by npm run [task-name] command. [More].

Pros:

  • you don’t need to install additional tools, npm scripts are part of npm, so you are ready to go right away

Cons:

  • when scripts get complex they are hard to read

Obviously you can reduce those cons by extracting commands from scripts section to separate files (bash/node scripts) and call them in npm scripts. In the end it’s not necessary convenient though as you will have your build code spread out across multiple files. For tasks up to 15/20 lines of code which are quite frequent it can feels just too much. Moreover if you want to use ES6/7, ES6 imports in your node.js build scripts you need to compile them. That complicate the process unnecessary.

Example of npm scripts usage:

Example from https://www.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/

Example of calling a task:

npm run watch:test

Makefile

Build automation tool created by Stuart Feldman and initially released in 1977. With the main purpose for C/C++ code compilation. However thanks to its flexibility it started to gain more universal usage. It allows to define tasks in “Makefile” file. Tasks can be called by make command in the terminal. [More].

Pros:

  • implementing more complex logic is not an issue

Cons:

  • you are tied to shell code, you can’t use JavaScript which you already know

Example of Makefile:

Example from https://blog.jcoglan.com/2014/02/05/building-javascript-projects-with-make/

Example of calling a task:

make test

runjs

Written in JavaScript. Minimalistic build tool. Can be installed through npm. Allows to define tasks in “runfile.js” file. Tasks can be called by run (or npx run) command in the terminal. [More].

Pros:

  • you can use JavaScript within tasks (with ES6/7) and import other npm modules. runfile.js is just a JavaScript module. This allows massive flexibility.

Cons:

  • global run command which needs to be installed through npm -g runjs-cli. This can be avoided in node ≥ 8.2, where you can call runjs tasks through npx, like “npx run dev”. More about npx.

Example of runfile.js:

Example of calling a task:

run test
run lint components/App.js --fix
run build:clean

Conclusions

I would say that considering the simple build tooling, all of those 3 are really good. The main difference is within the relation between flexibility and readability. In that matter runjs seems to be the strongest.

For further experimenting I give you a couple of interesting examples which are easy to do in runjs but can be hard to manage in npm scripts or Makefile.

Documenting tasks:

$ run build --help

Sharing tasks to a npm package:

$ run shared

Accepting arguments and conditional checks:

$ run test sidebar/button.js browser

Error handling and fallbacks:

$ run build

Handling async calls:

$ run start
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

Image for post
Image for post

HackerNoon.com

#BlackLivesMatter

Sign up for Get Better Tech Emails via HackerNoon.com

By HackerNoon.com

how hackers start their afternoons. the real shit is on hackernoon.com. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Paweł Gałązka

Written by

Frontend Developer @Envato. Former @Podio. Enthusiast of future interfaces and the nature of creativity.

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Paweł Gałązka

Written by

Frontend Developer @Envato. Former @Podio. Enthusiast of future interfaces and the nature of creativity.

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store