From b22921f3a0e42250661d6375cd9ddf903a32f94b Mon Sep 17 00:00:00 2001 From: William Swanson Date: Wed, 23 Jun 2021 16:43:26 -0700 Subject: [PATCH] ESM: Fix importing `index.js` files by folder name As pointed out in #4665, running mocha with an argument such as `-r ts-node/register` fails because Node cannot resolve `node_modules/ts-node/register/index.js` as an ESM module. The returned error has a `code` of `'ERR_UNSUPPORTED_DIR_IMPORT'`, which should trigger the `require` fall-back logic. --- lib/esm-utils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/esm-utils.js b/lib/esm-utils.js index d4acf06cb6..1e1230592a 100644 --- a/lib/esm-utils.js +++ b/lib/esm-utils.js @@ -49,7 +49,8 @@ exports.requireOrImport = hasStableEsmImplementation } catch (err) { if ( err.code === 'ERR_MODULE_NOT_FOUND' || - err.code === 'ERR_UNKNOWN_FILE_EXTENSION' + err.code === 'ERR_UNKNOWN_FILE_EXTENSION' || + err.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ) { return require(file); } else {