Skip to content

Commit

Permalink
[Fix] use file:// URLs for dynamic import()
Browse files Browse the repository at this point in the history
This is required on Windows if the argument passed to `import()` is
an absolute path. Without it `tape test.js` fails if test.js is ESM.

Covered by existing tests: `node test/import.js` fails without this.

See nodejs/node@a084632
  • Loading branch information
vweevers committed Nov 13, 2021
1 parent e41763f commit d487add
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bin/import-or-require.js
@@ -1,14 +1,15 @@
'use strict';

const { extname: extnamePath } = require('path');
const { pathToFileURL } = require('url');
const getPackageType = require('get-package-type');

// eslint-disable-next-line consistent-return
module.exports = function importOrRequire(file) {
const ext = extnamePath(file);

if (ext === '.mjs' || (ext === '.js' && getPackageType.sync(file) === 'module')) {
return import(file);
return import(pathToFileURL(file).href);
}
require(file);
};

0 comments on commit d487add

Please sign in to comment.