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

Function createRequire

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

* Creates a new `require` function that can be used to load modules. * @param {string | URL} filenameOrURL The path or URL to the module context for this `require` * @throws {ERR_INVALID_ARG_VALUE} If `filename` is not a string or URL, or if it is a relative path that cannot be * resolved to an a

(filenameOrURL)

Source from the content-addressed store, hash-verified

2148 * @returns {object}
2149 */
2150function createRequire(filenameOrURL) {
2151 let filepath, fileURL;
2152
2153 if (isURL(filenameOrURL) ||
2154 (typeof filenameOrURL === 'string' && !path.isAbsolute(filenameOrURL))) {
2155 try {
2156 // It might be an URL, try to convert it.
2157 // If it's a relative path, it would not parse and would be considered invalid per
2158 // the documented contract.
2159 fileURL = new URL(filenameOrURL);
2160 filepath = fileURLToPath(fileURL);
2161 } catch {
2162 throw new ERR_INVALID_ARG_VALUE('filename', filenameOrURL,
2163 createRequireError);
2164 }
2165 } else if (typeof filenameOrURL !== 'string') {
2166 throw new ERR_INVALID_ARG_VALUE('filename', filenameOrURL, createRequireError);
2167 } else {
2168 filepath = filenameOrURL;
2169 }
2170 return createRequireFromPath(filepath, fileURL);
2171}
2172
2173/**
2174 * Checks if a path is relative

Calls 3

fileURLToPathFunction · 0.85
createRequireFromPathFunction · 0.85
isURLFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…