Babel Npm Scripts
If, like me, you want to…
…then this post is for you. Let’s Babelify our npm scripts.
If you haven’t used npm scripts for your build/development process yet, this just stands on the shoulders of giants like @linclark’s articles at the npm blog, @keithamus’s amazing article, and numerous examples from the repos of masters like tj. I’m still a noob at the commandline stuff, so if you have suggestions for improvement, please let me know!
- Get a
package.jsonif you don’t already have one:
- Install Babel to your project:
npm -i --save-dev babel.
- Create scripts in a
- Wire it up to npm by editing your
At the top of your script file, include the shebang
#! /usr/bin/env babel-node. Now you can write your
script in ESnext syntax. If you aren’t passing flags (
-- style arguments), everything will just work from here.
Here’s an example that reads an
.env file, spins up redis, then spins up a server.
I can invoke this with
npm start or
npm start <environment>. The argument is passed through.
If you need more control over the execution, you can try using commanderjs.
I created a simple script to create a jwt token for my server based
.env file specified as well as a user.
After adding this to your
package.json scripts, you have to pass flags using
However, node will intercept its flags first, so don’t repeat any flags listed in
node --help (including
npm run jwt -- --help will output node’s help. But
npm run jwt -- -h will output commanderjs’s
help for our script. That’s the one flag you can’t control because it’s assumed by commanderjs. Here, I’ve
-E to avoid conflict with node’s
-e --eval. Creating a token is as simple as
npm run jwt -- -u <username>.
Lastly, I created a script to execute my tests and coverage. While it’s a bash one-liner, it’s pretty long and
I didn’t want to maintain it in that form. It was hard to break up because of how I
.env file (it was a bash script that
exported each variable). It also didn’t manage redis.
Was replaced with:
The only take-away here is that because of the execution context, you can’t just use the
npm reference of a
file, you have to use the full path. Because this script can run on the CI server, and it already has redis,
I made the redis commands conditional.