Cannot use import statement outside a module even when targeting CommonJS
Julien-Marcou opened this issue · comments
I'm trying to switch from ts-node
to esbuild-runner
for my package https://github.com/Julien-Marcou/static-html
Here is the problematic file bin/static.ts :
#!/usr/bin/env -S npx ts-node-script
import * as fs from 'fs';
const usage = 'Usage : npx static <init|build|watch|serve|deploy|clean>\n';
// ...
The goal of this file is to expose npx static
commands to developers who install my package.
But when I replace #!/usr/bin/env -S npx ts-node-script
by #!/usr/bin/env -S npx esbuild-runner
, I then get this error when calling my npx static
command :
(node:1888) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/jmarcou/dev/my-project/node_modules/static-html/bin/static.ts:2
import * as fs from 'fs';
^^^^^^
SyntaxError: Cannot use import statement outside a module
As if esbuild
thinks I'm targeting ECMAScript Module
instead of CommonJS
.
I did create a esbuild-runner-config.js
file, and tried to put it inside the root folder of the static-html
package aswell as the bin folder, but nothing changed.
module.exports = {
type: 'bundle',
esbuild: {
target: 'node14',
format: 'cjs',
},
};
Am I doing something wrong ?
Can you try npx esr
instead?
Same result with #!/usr/bin/env -S npx esr
😢
And for completeness, using #!/usr/bin/env -S npx esbuild --format=cjs
indeed works and generates the expected result
Maybe there's a way to directly call esr
with --format=cjs
instead of using a esbuild-runner-config.js
file ?
Running into this as well with lodash-es
- works fine when calling esbuild with --platform=node