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 · 5 comments
Open

Register does not transpile node_module dependencies #769

albertogasparin opened this issue Apr 15, 2024 · 5 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

@maxpatiiuk
Copy link

I am having the same issue. In my case I am distributing an intranet package as .ts rather than .js and wish to transpile it on the fly, however swc-node ignores files in node_modules.

In particular, it is happening here:

if (url.includes('/node_modules/')) {
debug('skip load: node_modules', url)
return nextLoad(url, context)
}

While I understand that the basic use of swc-node/register doesn't require transpiling in node_modules, could you please modify the above condition to not exclude node_modules if url ends with .ts?

alternatively, please expose some sort of flag/ env variables/option to transpile .ts files in node_modules too

thanks

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

4 participants