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

Register does not transpile node_module dependencies #769

Open
albertogasparin opened this issue Apr 15, 2024 · 4 comments
Open

Register does not transpile node_module dependencies #769

albertogasparin opened this issue Apr 15, 2024 · 4 comments

Comments

@albertogasparin
Copy link

I have a 3rd party dependency that internally relies on https://github.com/google/zx . Now zx does not publish with cjs, only ESM, and when I try to run my-script.ts that includes the dependency via:

 node --loader @swc-node/register/esm my-script.ts

I get an error:

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/zx/build/index.js from node_modules/3rd-party/dist/cjs/whatever.js not supported.
Instead change the require of index.js in node_modules/3rd-party/dist/cjs/whatever.js to a dynamic import() which is available in all CommonJS modules.

Obviously, cannot change the 3rd party dependency import statements. Also this was marked as "resolved" in #634 but seems like it works only on 1st level imports?

Other tools handle this properly (eg node -r esbuild-register) but I'd love to stick with SWC 🙏

@kdy1
Copy link
Member

kdy1 commented Apr 15, 2024

Can you provide a minimal reproduction?

@albertogasparin
Copy link
Author

albertogasparin commented Apr 15, 2024

Install @ling-jun/cli. Write this index.ts:

import {$} from 'zx';
import foo from '@ling-jun/cli';

console.log($);
console.log(foo);

Then run:

 node --loader @swc-node/register/esm index.ts

The first import will work, the second one will fails as swc will pick the main field which contains cjs requires instead of esm.

@yeliex
Copy link
Contributor

yeliex commented Apr 26, 2024

#767 should resolved this

@albertogasparin
Copy link
Author

I tried v1.9.1 which should include this fix but it still fails 🤔
I'll try and see if I can provide more information

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

No branches or pull requests

3 participants