mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
This is an abstraction on top of creatRequireFromPath that can accept both paths, URL Strings, and URL Objects. PR-URL: https://github.com/nodejs/node/pull/27405 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
35 lines
760 B
JavaScript
35 lines
760 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'
|
|
});
|