Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
module: allow passing a directory to createRequireFromPath
Fixes: #23710
  • Loading branch information
gillesdemey authored and MylesBorins committed Apr 30, 2019
commit a73469663de0c7360b0ba0e01126e899c82376aa
12 changes: 9 additions & 3 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,9 +825,15 @@ Module.runMain = function() {
};

Module.createRequireFromPath = (filename) => {
const m = new Module(filename);
m.filename = filename;
m.paths = Module._nodeModulePaths(path.dirname(filename));
// Allow a directory to be passed as the filename
const proxyPath = filename.endsWith('/') ?
path.join(filename, 'noop.js') :
Comment thread
guybedford marked this conversation as resolved.
Outdated
filename;

const m = new Module(proxyPath);
m.filename = proxyPath;

m.paths = Module._nodeModulePaths(path.dirname(proxyPath));
Comment thread
MylesBorins marked this conversation as resolved.
Outdated
return makeRequireFunction(m);
};

Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-module-create-require-from-directory.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.join(path.resolve(__dirname, '..', 'fixtures'), '/');
Comment thread
guybedford marked this conversation as resolved.
Outdated

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