When writting this "bin" script where local package is "module":
#!/usr/bin/env node
// code
I get this stacktrace:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for C:/myproject/bin/script
at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
at Loader.getFormat (internal/modules/esm/loader.js:113:42)
at Loader.getModuleJob (internal/modules/esm/loader.js:244:31)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Loader.import (internal/modules/esm/loader.js:178:17) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
The code after // code to run.
npm init -ytype = "module" in package.json.bin/script file in the project and add it to package.json.
I already tried these hacks sometime ago. Tried again and these don't work.
https://stackoverflow.com/questions/48179714/how-can-an-es6-module-be-run-as-a-script-in-node
I believe extensionless files do not currently have a way to be native ESM code.
if you want to kick off an ESM file you are going to want to use dynamic import not require. require is undefined in an ESM file and you cannot require an ESM file (only import).
Oh, actually { bin: "bin/cli.mjs" } is working. Because of tsc from TypeScript I used to thought you always had to use an extensionless file.