Dynamic imports not working with CommonJS Register API
Closed this issue · 3 comments
Acknowledgements
- I read the documentation and searched existing issues to avoid duplicates
- I understand this is a bug tracker and anything other than a proven bug will be closed
- I understand this is a free project and relies on community contributions
- I read and understood the Contribution guide
Minimal reproduction URL
https://github.com/lpgera/tsx-dynamic-import/actions/runs/11345278234/job/31551792675
Problem & expected behavior (under 200 words)
I have a project which must use the CommonJS Register API instead of the CLI, but I'm having issues with dynamic imports. I noticed that using the tsx
CLI command they are working as I'd expect, but with the CommonJS Register API I'm getting ERR_MODULE_NOT_FOUND errors.
I've set up a minimal reproduction repo here: https://github.com/lpgera/tsx-dynamic-import
Here's the output with the tsx
CLI on GitHub Actions: https://github.com/lpgera/tsx-dynamic-import/actions/runs/11345278234/job/31551792675#step:5:7
I'd expect the same output with the Register API, but I'm getting an error instead. See here: https://github.com/lpgera/tsx-dynamic-import/actions/runs/11345278234/job/31551792675#step:6:7
Bugs are expected to be fixed by those affected by it
- I'm interested in working on this issue
Compensating engineering work will speed up resolution and support the project
- I'm willing to offer $10 for financial support
I appreciate the minimal reproduction and sponsorship!
This caveat is documented in the warning in the require()
API:
https://tsx.is/dev-api/tsx-require
So basically, this is a limitation with ESM. imports()
is ESM world so the CJS API can't intercept it. If you also register the ESM API (tsx does both), it should work expectedly.
If you also register the ESM API (tsx does both), it should work expectedly.
Oh, I didn't realize this. I was afraid that registering both CJS and ESM APIs programmatically may cause issues, but I'm glad this is supported. I'll try this suggestion in my project soon.
Greatly appreciate this project by the way, it's a huge time saver in this CJS/ESM hell we live in nowadays. 🙂
After adding your suggestion, things are working as I'd expect: https://github.com/lpgera/tsx-dynamic-import/actions/runs/11346208623/job/31554771329#step:6:9
No bug here, this was just a misunderstanding on my end.