Skip to content

Commit

Permalink
module: add createRequireFunction method
Browse files Browse the repository at this point in the history
PR-URL: #19360
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Jan Krems <[email protected]>
Reviewed-By: Bradley Farias <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
  • Loading branch information
devsnek authored and targos committed Sep 25, 2018
1 parent adaf530 commit 600c225
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
18 changes: 18 additions & 0 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,12 +888,30 @@ by the [module wrapper][]. To access it, require the `Module` module:
const builtin = require('module').builtinModules;
```

### module.createRequireFromPath(filename)
<!-- YAML
added: REPLACEME
-->

* `filename` {string} Filename to be used to construct the relative require
function.
* Returns: {[`require`][]} Require function

```js
const { createRequireFromPath } = require('module');
const requireUtil = createRequireFromPath('../src/utils');

// require `../src/utils/some-tool`
requireUtil('./some-tool');
```

[`__dirname`]: #modules_dirname
[`__filename`]: #modules_filename
[`Error`]: errors.html#errors_class_error
[`module` object]: #modules_the_module_object
[`path.dirname()`]: path.html#path_path_dirname_path
[GLOBAL_FOLDERS]: #modules_loading_from_the_global_folders
[`require`]: #modules_require
[exports shortcut]: #modules_exports_shortcut
[module resolution]: #modules_all_together
[module wrapper]: #modules_the_module_wrapper
Expand Down
10 changes: 8 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const { NativeModule } = require('internal/bootstrap/loaders');
const util = require('util');
const { pathToFileURL } = require('internal/url');
const vm = require('vm');
const assert = require('assert').ok;
const fs = require('fs');
Expand Down Expand Up @@ -54,7 +55,6 @@ module.exports = Module;
let asyncESM;
let ModuleJob;
let createDynamicModule;
let pathToFileURL;
let decorateErrorStack;

function lazyLoadESM() {
Expand All @@ -63,7 +63,6 @@ function lazyLoadESM() {
createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
decorateErrorStack = require('internal/util').decorateErrorStack;
pathToFileURL = require('internal/url').pathToFileURL;
}

const {
Expand Down Expand Up @@ -745,6 +744,13 @@ Module.runMain = function() {
process._tickCallback();
};

Module.createRequireFromPath = (filename) => {
const m = new Module(filename);
m.filename = filename;
m.paths = Module._nodeModulePaths(path.dirname(filename));
return makeRequireFunction(m);
};

Module._initPaths = function() {
const isWindows = process.platform === 'win32';

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-module-create-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

require('../common');
const assert = require('assert');
const path = require('path');

const { createRequireFromPath } = require('module');

const p = path.resolve(__dirname, '..', 'fixtures', 'fake.js');

const req = createRequireFromPath(p);
assert.strictEqual(req('./baz'), 'perhaps I work');

0 comments on commit 600c225

Please sign in to comment.