Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use import statement outside a module even when targeting CommonJS #31

Open
Julien-Marcou opened this issue Jul 8, 2021 · 3 comments

Comments

@Julien-Marcou
Copy link

Julien-Marcou commented Jul 8, 2021

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 ?

@folke
Copy link
Owner

folke commented Jul 8, 2021

Can you try npx esr instead?

@Julien-Marcou
Copy link
Author

Julien-Marcou commented Jul 8, 2021

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 ?

@thdxr
Copy link

thdxr commented Sep 16, 2021

Running into this as well with lodash-es - works fine when calling esbuild with --platform=node

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants