Skip to content

Instantly share code, notes, and snippets.

@johnloy
Last active September 8, 2021 14:14
Show Gist options
  • Save johnloy/1c1d91e295c137d5b9c9065b0b245b66 to your computer and use it in GitHub Desktop.
Save johnloy/1c1d91e295c137d5b9c9065b0b245b66 to your computer and use it in GitHub Desktop.

From nodejs/node#32316 (comment)
Related: https://gist.github.com/WebReflection/8840ec29d296f2fa98d8be0102f08590#

Here’s how to create your own node-esm:

Create a file node-esm somewhere in your PATH, e.g. /usr/local/bin/node-esm:

#!/usr/bin/env sh
input_file=$1
shift
exec node --input-type=module - $@ <$input_file

Make it executable:

chmod +x /usr/local/bin/node-esm

Now you can save the following as a file like ./my-script:

#!/usr/bin/env node-esm
import { version } from 'process';
console.log(version);
And once it’s executable, it should work as you want:
$ chmod +x ./my-script
$ ./my-script
v13.12.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment