BLOG

Notes from the KAYAC hyper-casual team

[Goodbye gulp] Building a Simple Front-End Development Environment with npm-scripts

Takumi Fukasawa

This past summer, I was using gulp as usual when one day, after a gulp version upgrade or something along those lines, my homemade gulp development environment stopped working properly and I got badly stuck. It was a truly dire situation...

At that moment it suddenly struck me: "Wait, why am I using gulp again..." So I went back to basics and experimented to see what would happen if I got rid of gulp. My development environment became amazingly clean, and on top of that, building an environment from scratch through trial and error turned out to be fun.


In this article, I'd like to build a modern front-end development environment centered on npm-scripts, without task runners like gulp or grunt. Goodbye, task runners.

Development environments built on npm-scripts have been trendy for a year or two now, but while I'm at it, I'd like to show an environment that uses modern modules as well.

I've already made a template for such a development environment, which I'll explain at the end.

Steps


Note: this assumes node.js is already installed.

These are the basic steps, in order. Facebook's new package manager, yarn, came out recently, but apart from the commands, there's no real difference between yarn and npm.

1. $ yarn init ($ npm init)
2. $ yarn add module-name ($ npm install module-name)
3. Edit scripts in package.json
4. Repeat 2 and 3

As you can see, the workflow is very simple, and the init and install parts should be familiar. Now, let's finally start using npm-scripts.

What are npm scripts?


npm lets you register shell scripts and alias commands in a field called "scripts" in package.json.

Writing them

First, create a scripts field in package.json. Then just keep adding pairs of "script名": "実行コマンド" ("script name": "command to run"). To start, I've written three scripts.

↓↓ The explanations below use these scripts as examples.

...
"author": "takumifukasawa",
"license": "MIT",
"scripts":  {
 "start": "npm run watchify & npm run serve",
 "serve": "browser-sync start --server ./public/",
 "watchify": "watchify ./src/js/app.js -o ./public/js/bundle.js -v wait;"
},
...
Running them

With npm-scripts you can run a script with $ npm run [script name]. To run the watchify script we registered, use $ npm run watchify.

Reserved words

One thing to note is that npm-scripts has four reserved words. These can be run without run.

$ npm start
$ npm restart
$ npm stop
$ npm test

So the three npm-scripts we registered earlier can each be run with these commands.

$ npm start
$ npm run serve
$ npm run watchify
Execution order

You can control the order in which scripts run using & and &&. The start script above joins its scripts with &.

  • Joining with a single & runs each script in parallel
  • Joining with && runs them in series. && is handy when the scripts need to run in order.

The start in the example was written as npm run watchify & npm run serve, so it tells npm to run watchify and serve at the same time.


* For more detail on using npm-scripts itself, this article, which I used as a reference, is very easy to follow. You can also set environment variables. If you're interested, do give it a read. How to Use npm as a Build Tool

Pros and cons compared with gulp

Pros

  1. No task runner in between, so you can call the wealth of node_modules directly
  2. No need to learn a task runner such as gulp or grunt itself
  3. You only need the bare minimum of node_modules, so you can keep the project size down

With a task runner, things can stop working when the node_modules written for it are no longer developed or the task runner itself gets a version bump. I suspect many of you have gotten stuck that way.

That's why I think the big advantage of running things directly from npm-scripts is number 1. In most cases, going straight to the npm documentation is enough. For example, for the watchify from earlier, reading this documentation is all you need.

watchify

Cons

  1. package.json gets bloated
  2. Less flexibility and extensibility (for example, the variables you can use are limited)
  3. Readability is worse than with a task runner

Since all the tasks end up crammed into package.json, it's not well suited to anything complex. When you have many tasks to run at the same time, or need strict control over the order in which tasks run, it's easier to manage with a task runner such as gulp.

The sample development environment


Finally, I'll wrap up by explaining the npm-scripts development environment I mentioned at the start. I hope it's useful when you build your own development environment, or as a companion to this article.

It combines modern modules. It's a very minimal setup that assumes a single page, so when I need multiple html, js and css files, I modify it as needed.

What's in it

html: pug (formerly jade)

Lets you write html in an indentation-based format. You don't have to write closing tags, which saves effort.

css: postcss

With postcss, you install and use only the modules you want, such as autoprefixer and postcss-assets. You can mix and match modules depending on the environment, so it scales freely with the size of the project.

javascript: watchify

Gives you incremental builds for browserify. If you use browserify as is, it compiles from scratch on every update, but incremental builds dramatically speed up compilation.

serve: browser-sync

A development server. The autoreload feature, which reloads automatically when a file is updated, is handy.

Tomorrow


Next up is an amazing article by @beinto, who joined us straight out of school this year and is doing a terrific job!

[References]

How to Use npm as a Build Tool

Centralizing both dependencies and tasks with npm

The story of a middle-aged guy worn out by Grunt/Gulp