Writing Node Scripts
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: