* 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)
| 2148 | * @returns {object} |
| 2149 | */ |
| 2150 | function 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 |
no test coverage detected
searching dependent graphs…