@@ -135,7 +135,7 @@ const { BuiltinModule } = require('internal/bootstrap/realm');
135135const {
136136 maybeCacheSourceMap,
137137} = require ( 'internal/source_map/source_map_cache' ) ;
138- const { pathToFileURL, fileURLToPath, isURL } = require ( 'internal/url' ) ;
138+ const { pathToFileURL, fileURLToPath, isURL, URL } = require ( 'internal/url' ) ;
139139const {
140140 pendingDeprecate,
141141 emitExperimentalWarning,
@@ -1923,7 +1923,7 @@ Module._extensions['.node'] = function(module, filename) {
19231923 * @param {string } filename The path to the module
19241924 * @returns {any }
19251925 */
1926- function createRequireFromPath ( filename ) {
1926+ function createRequireFromPath ( filename , fileURL ) {
19271927 // Allow a directory to be passed as the filename
19281928 const trailingSlash =
19291929 StringPrototypeEndsWith ( filename , '/' ) ||
@@ -1935,6 +1935,10 @@ function createRequireFromPath(filename) {
19351935
19361936 const m = new Module ( proxyPath ) ;
19371937 m . filename = proxyPath ;
1938+ if ( fileURL !== undefined ) {
1939+ // Save the URL if createRequire() was given a URL, to preserve search params, if any.
1940+ m [ kURL ] = fileURL . href ;
1941+ }
19381942
19391943 m . paths = Module . _nodeModulePaths ( m . path ) ;
19401944 return makeRequireFunction ( m , null ) ;
@@ -1945,28 +1949,32 @@ const createRequireError = 'must be a file URL object, file URL string, or ' +
19451949
19461950/**
19471951 * Creates a new `require` function that can be used to load modules.
1948- * @param {string | URL } filename The path or URL to the module context for this `require`
1952+ * @param {string | URL } filenameOrURL The path or URL to the module context for this `require`
19491953 * @throws {ERR_INVALID_ARG_VALUE } If `filename` is not a string or URL, or if it is a relative path that cannot be
19501954 * resolved to an absolute path.
19511955 * @returns {object }
19521956 */
1953- function createRequire ( filename ) {
1954- let filepath ;
1957+ function createRequire ( filenameOrURL ) {
1958+ let filepath , fileURL ;
19551959
1956- if ( isURL ( filename ) ||
1957- ( typeof filename === 'string' && ! path . isAbsolute ( filename ) ) ) {
1960+ if ( isURL ( filenameOrURL ) ||
1961+ ( typeof filenameOrURL === 'string' && ! path . isAbsolute ( filenameOrURL ) ) ) {
19581962 try {
1959- filepath = fileURLToPath ( filename ) ;
1963+ // It might be an URL, try to convert it.
1964+ // If it's a relative path, it would not parse and would be considered invalid per
1965+ // the documented contract.
1966+ fileURL = new URL ( filenameOrURL ) ;
1967+ filepath = fileURLToPath ( fileURL ) ;
19601968 } catch {
1961- throw new ERR_INVALID_ARG_VALUE ( 'filename' , filename ,
1969+ throw new ERR_INVALID_ARG_VALUE ( 'filename' , filenameOrURL ,
19621970 createRequireError ) ;
19631971 }
1964- } else if ( typeof filename !== 'string' ) {
1965- throw new ERR_INVALID_ARG_VALUE ( 'filename' , filename , createRequireError ) ;
1972+ } else if ( typeof filenameOrURL !== 'string' ) {
1973+ throw new ERR_INVALID_ARG_VALUE ( 'filename' , filenameOrURL , createRequireError ) ;
19661974 } else {
1967- filepath = filename ;
1975+ filepath = filenameOrURL ;
19681976 }
1969- return createRequireFromPath ( filepath ) ;
1977+ return createRequireFromPath ( filepath , fileURL ) ;
19701978}
19711979
19721980/**
0 commit comments