May 11, 2019

How to create a Node.js executable script

How to create a Node.js executable script

I am probably not the only one who has trouble remembering the bash syntax when it comes to write some scripts for automatization for instance. Fortunately we can use Javascript to do the same things, when using Node.js to execute our scripts.

To do so we simply add the location of our node installation to the shebang on unix based systems.

#!/usr/bin/env node

On windows systems we can make use of the bin field in our package.json, this will create a .cmd wrapper to execute our script on Windows machines.

{ "bin" : { "myapp" : "./cli.js" } }