subprogram

Turn individual ES modules into subprograms that can be run with node or improted without running.

Usage no npm install needed!

<script type="module">
  import subprogram from 'https://cdn.skypack.dev/subprogram';
</script>

README

subprogram

Utility to turn individual ES modules into programs that can be run.

Usage

You can turn ES module into runnable node program. This is an ES alternative to if (require.main === module) { ... } pattern.

import { script } from "subprogram"

export const main () => {
  console.log("Hello world")
}

script({ ...module.meta, main })

When above program is run as node script.js it will run a main function of the module, if module is imported no action will take place.