swc-project / swc-project/swc-node

Register does not transpile node_module dependencies

Open
#769 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
2k
Forks
95
Avg merge
14d 11h
Merged PRs (30d)
1

Description

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 https://github.com/swc-project/swc-node/issues/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 🙏

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the @swc-node/register/esm loader entry point and reproduce the command using my-script.ts. Trace how node_modules/3rd-party/dist/cjs/whatever.js loads node_modules/zx/build/index.js, then verify that the transitive ESM dependency works without changing the third-party package.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.