Skip to content

Commit d487add

Browse files
committed
[Fix] use file:// URLs for dynamic import()
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
1 parent e41763f commit d487add

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

bin/import-or-require.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
'use strict';
22

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

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

1011
if (ext === '.mjs' || (ext === '.js' && getPackageType.sync(file) === 'module')) {
11-
return import(file);
12+
return import(pathToFileURL(file).href);
1213
}
1314
require(file);
1415
};

0 commit comments

Comments
 (0)