diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 60fa7e79d19693..55f28398a921d5 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -84,6 +84,7 @@ const { kEmptyObject, filterOwnProperties, setOwnProperty, + getCwdSafe, getLazy, } = require('internal/util'); const { internalCompileFunction } = require('internal/vm'); @@ -1025,7 +1026,7 @@ Module._resolveFilename = function(request, parent, isMain, options) { } if (request[0] === '#' && (parent?.filename || parent?.id === '')) { - const parentPath = parent?.filename ?? process.cwd() + path.sep; + const parentPath = parent?.filename ?? getCwdSafe(); const pkg = readPackageScope(parentPath) || {}; if (pkg.data?.imports != null) { try { diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 927b118f8ede2b..d4d7ee94dd707a 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -37,6 +37,7 @@ const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); const typeFlag = getOptionValue('--input-type'); const { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url'); +const { getCwdSafe } = require('internal/util'); const { canParse: URLCanParse } = internalBinding('url'); const { ERR_INPUT_TYPE_NOT_ALLOWED, @@ -1018,7 +1019,7 @@ function defaultResolve(specifier, context = {}) { const isMain = parentURL === undefined; if (isMain) { - parentURL = pathToFileURL(`${process.cwd()}/`).href; + parentURL = pathToFileURL(getCwdSafe()).href; // This is the initial entry point to the program, and --input-type has // been passed as an option; but --input-type can only be used with