mirror of
https://github.com/nodejs/node.git
synced 2025-05-07 15:35:41 +00:00

PR-URL: https://github.com/nodejs/node/pull/27627 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
37 lines
897 B
JavaScript
37 lines
897 B
JavaScript
'use strict';
|
|
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const path = require('path');
|
|
|
|
const { createRequire, createRequireFromPath } = require('module');
|
|
|
|
const p = path.resolve(__dirname, '..', 'fixtures', 'fake.js');
|
|
const u = new URL(`file://${p}`);
|
|
|
|
const req = createRequireFromPath(p);
|
|
assert.strictEqual(req('./baz'), 'perhaps I work');
|
|
|
|
const reqToo = createRequire(u);
|
|
assert.deepStrictEqual(reqToo('./experimental'), { ofLife: 42 });
|
|
|
|
assert.throws(() => {
|
|
createRequire('https://github.com/nodejs/node/pull/27405/');
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_VALUE'
|
|
});
|
|
|
|
assert.throws(() => {
|
|
createRequire('../');
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_VALUE'
|
|
});
|
|
|
|
assert.throws(() => {
|
|
createRequire({});
|
|
}, {
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
message: 'The argument \'filename\' must be a file URL object, file URL ' +
|
|
'string, or absolute path string. Received {}'
|
|
});
|