8000 module: add createRequire method · nodejs/node@5bcd770 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 5bcd770

Browse files
MylesBorinstargos
authored andcommitted
module: add createRequire method
This is an abstraction on top of creatRequireFromPath that can accept both paths, URL Strings, and URL Objects. PR-URL: #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>
1 parent 2c3c0d7 commit 5bcd770

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

doc/api/deprecations.md

+14
Original file line numberDiff line numberDiff line change
@@ -2439,6 +2439,19 @@ The `_channel` property of child process objects returned by `spawn()` and
24392439
similar functions is not intended for public use. Use `ChildProcess.channel`
24402440
instead.
24412441
2442+
<a id="DEP0130"></a>
2443+
### DEP00XX: Module.createRequireFromPath()
2444+
<!-- YAML
2445+
changes:
2446+
- version: REPLACEME
2447+
pr-url: https://github.com/nodejs/node/pull/27405
2448+
description: Documentation-only.
2449+
-->
2450+
2451+
Type: Documentation-only
2452+
2453+
Module.createRequireFromPath() is deprecated. Please use [`module.createRequire()`][] instead.
2454+
24422455
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
24432456
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
24442457
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
@@ -2486,6 +2499,7 @@ instead.
24862499
[`http.request()`]: http.html#http_http_request_options_callback
24872500
[`https.get()`]: https.html#https_https_get_options_callback
24882501
[`https.request()`]: https.html#https_https_request_options_callback
2502+
[`module.createRequire()`]: modules.html#modules_module_createrequire_filename
24892503
[`os.networkInterfaces()`]: os.html#os_os_networkinterfaces
24902504
[`os.tmpdir()`]: os.html#os_os_tmpdir
24912505
[`process.env`]: process.html#process_process_env

doc/api/modules.md

+22
Original file line numberDiff line numberDiff line change
@@ -905,15 +905,36 @@ by the [module wrapper][]. To access it, require the `Module` module:
905905
const builtin = require('module').builtinModules;
906906
```
907907

908+
### module.createRequire(filename)
909+
<!-- YAML
910+
added: REPLACEME
911+
-->
912+
913+
* `filename` {string|URL} Filename to be used to construct the require
914+
function. Must be a file URL object, file URL string, or absolute path
915+
string.
916+
* Returns: {require} Require function
917+
918+
```js
919+
const { createRequire } = require('module');
920+
const requireUtil = createRequire(require.resolve('../src/utils/'));
921+
922+
// Require `../src/utils/some-tool`
923+
requireUtil('./some-tool');
924+
```
925+
908926
### module.createRequireFromPath(filename)
909927
<!-- YAML
910928
added: v10.12.0
929+
deprecated: REPLACEME
911930
-->
912931

913932
* `filename` {string} Filename to be used to construct the relative require
914933
function.
915934
* Returns: {require} Require function
916935

936+
> Stability: 0 - Deprecated: Please use [`createRequire()`][] instead.
937+
917938
```js
918939
const { createRequireFromPath } = require('module');
919940
const requireUtil = createRequireFromPath('../src/utils/');
@@ -926,6 +947,7 @@ requireUtil('./some-tool');
926947
[`Error`]: errors.html#errors_class_error
927948
[`__dirname`]: #modules_dirname
928949
[`__filename`]: #modules_filename
950+
[`createRequire()`]: #modules_module_createrequire_filename
929951
[`module` object]: #modules_the_module_object
930952
[`path.dirname()`]: path.html#path_path_dirname_path
931953
[exports shortcut]: #modules_exports_shortcut

lib/internal/modules/cjs/loader.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
const { JSON, Object, Reflect } = primordials;
2525

2626
const { NativeModule } = require('int 9E88 ernal/bootstrap/loaders');
27-
const { pathToFileURL } = require('internal/url');
27+
const { pathToFileURL, fileURLToPath, URL } = require('internal/url');
2828
const { deprecate } = require('internal/util');
2929
const vm = require('vm');
3030
const assert = require('internal/assert');
@@ -824,7 +824,7 @@ Module.runMain = function() {
824824
Module._load(process.argv[1], null, true);
825825
};
826826

827-
Module.createRequireFromPath = (filename) => {
827+
function createRequireFromPath(filename) {
828828
// Allow a directory to be passed as the filename
829829
const trailingSlash =
830830
filename.endsWith(path.sep) || path.sep !== '/' && filename.endsWith('\\');
@@ -838,7 +838,34 @@ Module.createRequireFromPath = (filename) => {
838838

839839
m.paths = Module._nodeModulePaths(m.path);
840840
return makeRequireFunction(m);
841-
};
841+
}
842+
843+
Module.createRequireFromPath = createRequireFromPath;
844+
845+
const createRequireError = 'must be a file URL object, file URL string, or' +
846+
'absolute path string';
847+
848+
function createRequire(filename) {
849+
let filepath;
850+
if (typeof filename === 'object' && !(filename instanceof URL)) {
851+
throw new ERR_INVALID_ARG_VALUE('filename', filename, createRequireError);
852+
} else if (typeof filename === 'object' ||
853+
typeof filename === 'string' && !path.isAbsolute(filename)) {
854+
try {
855+
filepath = fileURLToPath(filename);
856+
} catch {
857+
throw new ERR_INVALID_ARG_VALUE('filename', filename,
858+
createRequireError);
859+
}
860+
} else if (typeof filename !== 'string') {
861+
throw new ERR_INVALID_ARG_VALUE('filename', filename, createRequireError);
862+
} else {
863+
filepath = filename;
864+
}
865+
return createRequireFromPath(filepath);
866+
}
867+
868+
Module.createRequire = createRequire;
842869

843870
Module._initPaths = function() {
844871
var homeDir;

test/parallel/test-module-create-require.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,31 @@ require('../common');
44
const assert = require('assert');
55
const path = require('path');
66

7-
const { createRequireFromPath } = require('module');
7+
const { createRequire, createRequireFromPath } = require('module');
88

99
const p = path.resolve(__dirname, '..', 'fixtures', 'fake.js');
10+
const u = new URL(`file://${p}`);
1011

1112
const req = createRequireFromPath(p);
1213
assert.strictEqual(req('./baz'), 'perhaps I work');
14+
15+
const reqToo = createRequire(u);
16+
assert.deepStrictEqual(reqToo('./experimental'), { ofLife: 42 });
17+
18+
assert.throws(() => {
19+
createRequire('https://github.com/nodejs/node/pull/27405/');
20+
}, {
21+
code: 'ERR_INVALID_ARG_VALUE'
22+
});
23+
24+
assert.throws(() => {
25+
createRequire('../');
26+
}, {
27+
code: 'ERR_INVALID_ARG_VALUE'
28+
});
29+
30+
assert.throws(() => {
31+
createRequire({});
32+
}, {
33+
code: 'ERR_INVALID_ARG_VALUE'
34+
});

0 commit comments

Comments
 (0)
0