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

fix(require): handle ERR_REQUIRE_ESM error #8

Merged
merged 5 commits into from Oct 9, 2021
Merged

Conversation

lukeed
Copy link
Owner

@lukeed lukeed commented Oct 9, 2021

Previously, tsm didn't handle the case where you could require a foo.js file that contained ESM syntax. Even though this combination is/should be invalid, tsm has to be able to rewrite the file(s) as necessary in the event there's a series or combination of tools that produce this scenario.

For example, you could be writing tests in TypeScript & those tests import third-party code that's written in ESM within .js files. When executing those tests with a require hook – for example uvu -r tsm tests – then the TypeScript would be converted into CommonJS, making require() calls to the third-party ".js" file, which still contains ESM. This would fail.

This is implemented in a way such that node -r tsm does nothing to .js files by default. It will only retry a .js file if the ERR_REQUIRE_ESM error was thrown. In fact, if any loader (regardless of extension) attempts to execute but throws the ERR_REQUIRE_ESM error, then the file is retried w/ the same options, but forcing format: cjs the second time around.

Note: If you add custom configuration for .js files, then tsm will respect that and follow your directions anyway. Your config will always execute, not just when the ERR_REQUIRE_ESM error is thrown.


Closes #7

@lukeed lukeed merged commit 1368d98 into master Oct 9, 2021
@lukeed lukeed deleted the fix/require-esm branch October 9, 2021 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support native ESM packages
1 participant