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

@swc-node/register can not handle es modules #634

Closed
cschroeter opened this issue Dec 15, 2021 · 4 comments · Fixed by #643
Closed

@swc-node/register can not handle es modules #634

cschroeter opened this issue Dec 15, 2021 · 4 comments · Fixed by #643

Comments

@cschroeter
Copy link

yarn run v1.22.17
$ node -r @swc-node/register index.ts
/home/christian/Workspaces/swc-modules/index.ts:141
undefined
      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/christian/Workspaces/swc-modules/node_modules/pkg-up/index.js from /home/christian/Workspaces/swc-modules/index.ts not supported.
Instead change the require of index.js in /home/christian/Workspaces/swc-modules/index.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.newLoader [as .js] (/home/christian/Workspaces/swc-modules/node_modules/pirates/lib/index.js:141:7)
    at Object.<anonymous> (/home/christian/Workspaces/swc-modules/index.ts:2:14)
    at Module._compile (/home/christian/Workspaces/swc-modules/node_modules/pirates/lib/index.js:136:24)
    at Object.newLoader [as .ts] (/home/christian/Workspaces/swc-modules/node_modules/pirates/lib/index.js:141:7) {
  code: 'ERR_REQUIRE_ESM'
}

Here is an minimal demo of that isssue https://github.com/cschroeter/bug-swc-esmodules/blob/main/package.json

Here is how ts-node tackles the issue TypeStrong/ts-node#1007

@cschroeter
Copy link
Author

cschroeter commented Dec 15, 2021

Although it works with node itself, you can not use dynamic imports like suggested in the error message above

(async () => {
  const { pkgUpSync } = await import("pkg-up");
  console.log("Hello World", pkgUpSync());
})();

@jameshfisher
Copy link
Contributor

I just hit this issue too. @Brooooooklyn is there any workaround? (I can't change the code to use dynamic imports.)

@danielyogel
Copy link

Same problem here...

@jameshfisher
Copy link
Contributor

Just found this, only to see I had the same problem a year ago ...

FYI: https://github.com/egoist/esbuild-register seems to not have this problem, so I'm switching to that for now.

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

Successfully merging a pull request may close this issue.

3 participants