Starting in Node v8.5.0, support for ES6 style modules
import x from 'x'
has been available by running node using the option --experimental-modules, as below:
node --experimental-modules test.mjs
Using the bin key in package.json you can easily create cli tools npm cli, by running npm link.
Unfortunately, when running in this manner, node is called without the optional --experimental-modules flag.
How can you use bin modules with --experimental-modules?
Here is an example
bin/healthcheck.mjs
import { connect } from 'amqplib'
let open = connect(process.env.RABBITMQ_URL);
const exit = ({healthy = true}) => {
return healthy ? process.exit(0) : process.exit(1)
}
open.then(() => {
exit({healthy: true})
}).catch((e) => {
exit({healthy: false})
})
package.json
{
"name": "my-cli",
"bin": {
"healthcheck": "./bin/healthcheck.mjs"
}
}
running...
> npm link
> healthcheck
/usr/local/bin/healthcheck: line 1: import: command not found
/usr/local/bin/healthcheck: line 3: syntax error near unexpected token `('
/usr/local/bin/healthcheck: line 3: `let open = connect(process.env.RABBITMQ_URL);'