MCPcopy Index your code
hub / github.com/nodejs/node / createRequireFromPath

Function createRequireFromPath

lib/internal/modules/cjs/loader.js:2119–2138  ·  view source on GitHub ↗

* Creates a `require` function that can be used to load modules from the specified path. * @param {string} filename The path to the module * @returns {any}

(filename, fileURL)

Source from the content-addressed store, hash-verified

2117 * @returns {any}
2118 */
2119function createRequireFromPath(filename, fileURL) {
2120 // Allow a directory to be passed as the filename
2121 const trailingSlash =
2122 StringPrototypeEndsWith(filename, '/') ||
2123 (isWindows && StringPrototypeEndsWith(filename, '\\'));
2124
2125 const proxyPath = trailingSlash ?
2126 path.join(filename, 'noop.js') :
2127 filename;
2128
2129 const m = new Module(proxyPath);
2130 m.filename = proxyPath;
2131 if (fileURL !== undefined) {
2132 // Save the URL if createRequire() was given a URL, to preserve search params, if any.
2133 m[kURL] = fileURL.href;
2134 }
2135
2136 m.paths = Module._nodeModulePaths(m.path);
2137 return makeRequireFunction(m, null);
2138}
2139
2140const createRequireError = 'must be a file URL object, file URL string, or ' +
2141 'absolute path string';

Callers 1

createRequireFunction · 0.85

Calls 2

makeRequireFunctionFunction · 0.85
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…