Writing Node Scripts

Published See discussion on Twitter

When I find myself repeatedly making edits to a large number of files, or if I need to collect some metadata about a collection of files I tend to reach for a quick and dirty Node script.

Usually I can get by with a script.js file written somewhere and then run it via node script.js, however sometimes I want to turn a script into a small node package that I can install and reference elsewhere.

To accomplish that I usually reach for adding a package.json with a bin script entry pointing to the node script.

  "name": "my-cool-package",
  "version": "1.0.0",
  "bin": {
    "name": "./index.js"

However, before the traditional node script can be run via name, we need to do two other things to the index.js file that the bin command is referencing.

First we need to add a line at the top of the file:

#!/usr/bin/env node

In addition to adding that line to the top of the file, we also need to change the mode of the file, we can do that via:

chmod u+x index.js

For more information, this article on node shell scripting is a great one to reference: https://2ality.com/2011/12/nodejs-shell-scripting.html