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 { diff --git a/test/integration/esm.spec.js b/test/integration/esm.spec.js index f1de91dfb5..7cf1c39071 100644 --- a/test/integration/esm.spec.js +++ b/test/integration/esm.spec.js @@ -1,4 +1,5 @@ 'use strict'; +var path = require('path'); var helpers = require('./helpers'); var run = helpers.runMochaJSON; var runMochaAsync = helpers.runMochaAsync; @@ -65,4 +66,19 @@ describe('esm', function() { done(); }); }); + + it('should enable requiring/loading a cjs module with "dir" as filename', async function() { + var fixture = 'esm/test-that-uses-dir-cjs-require.fixture.js'; + const result = await runMochaAsync( + fixture, + [ + ...args, + '--require', + path.resolve(__dirname, './fixtures/esm/dir-cjs-require') + ], + {stdio: 'pipe'} + ); + + expect(result, 'to have passed test count', 1); + }); }); diff --git a/test/integration/fixtures/esm/dir-cjs-require/index.js b/test/integration/fixtures/esm/dir-cjs-require/index.js new file mode 100644 index 0000000000..4b8359abe1 --- /dev/null +++ b/test/integration/fixtures/esm/dir-cjs-require/index.js @@ -0,0 +1 @@ +global.testPassesIfThisVariableIsDefined = true diff --git a/test/integration/fixtures/esm/test-that-uses-dir-cjs-require.fixture.js/index.js b/test/integration/fixtures/esm/test-that-uses-dir-cjs-require.fixture.js/index.js new file mode 100644 index 0000000000..35d336d4ff --- /dev/null +++ b/test/integration/fixtures/esm/test-that-uses-dir-cjs-require.fixture.js/index.js @@ -0,0 +1,4 @@ +// See https://github.com/mochajs/mocha/issues/4665 for an explanation of this test +it('should require a dir import', () => { + expect(global.testPassesIfThisVariableIsDefined, 'to be', true) +})