fix(require): handle ERR_REQUIRE_ESM
error
#8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, tsm didn't handle the case where you could
require
afoo.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 exampleuvu -r tsm tests
– then the TypeScript would be converted into CommonJS, makingrequire()
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 theERR_REQUIRE_ESM
error was thrown. In fact, if any loader (regardless of extension) attempts to execute but throws theERR_REQUIRE_ESM
error, then the file is retried w/ the same options, but forcingformat: cjs
the second time around.Closes #7