shebang and optional args

2020-09-15

 | 

~1 min read

 | 

197 words

One of the beautiful things about open sourcing work is that you get to learn from the community! I’ve been working on a little CLI to help me manage my notes. I call it nom and I’ll write more about it soon. Today, however, I want to talk about learning something I didn’t know was possible!

Since nom is a CLI, the first line of the app’s entry point is a shebang which in Unix-like platforms is used to specify the interpreter to use for the remainder of the script.

nom is written in Node, which is why the first line of my index.js is:

index.js
#!/usr/bin/env node

The part I learned today is that you can pass optional arguments in this line as well!

For example, let’s suppress warnings from Node:

index.js
#!/usr/bin/env node --no-warnings

Alternatively, we can set it as a NODE_OPTION like so:

index.js
#!/usr/bin/env NODE_OPTIONS=--no-warnings node

While this could also be set from a run script, this capability means that it’s not the sole responsibility of users. Instead authors of programs can assume the responsibility to ensure that the user experience is pleasant. Thanks Alex Lee for showing me this!


Related Posts
  • Yarn: Setting Up Workspaces


  • Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!