ESM: Fix importing index.js
files by folder name
#4666
Closed
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.
Description of the Change
This is a fix for #4665, where running with
-r ts-node/register
or similar flags doesn't work.The reason this fails is because Node cannot resolve
node_modules/ts-node/register/index.js
as an ESM module. The returned error has acode
of'ERR_UNSUPPORTED_DIR_IMPORT'
, which should trigger therequire
fall-back logic, but it doesn't because that error code is not on the list.Alternate Designs
Mocha v8 used a much simpler
try
/catch
approach that worked reliably. Mocha v9 tries to be more picky, which is good, but we need to include all the edge cases for this to work smoothly.Why should this be in core?
It's a bug-fix for a pretty serious v9 regression.
Benefits
mocha -r ts-node/register
works again! Ormocha -r sucrase/register
, if you prefer that one.Possible Drawbacks
I can't think of any - it's a pretty obvious fix.
Applicable issues
#4665
This is a minor patch fix for a pretty common issue, so it would be good to release as v9.0.1 -> v9.0.2.