|
3 | 3 | // Last checked on: Apr 29, 2023. |
4 | 4 |
|
5 | 5 | /** |
| 6 | + * @typedef {import('node:fs').Stats} Stats |
6 | 7 | * @typedef {import('./errors.js').ErrnoException} ErrnoException |
7 | 8 | * @typedef {import('./package-json-reader.js').PackageConfig} PackageConfig |
8 | 9 | */ |
9 | 10 |
|
10 | 11 | import assert from 'node:assert' |
11 | | -import {Stats, statSync, realpathSync} from 'node:fs' |
| 12 | +import {statSync, realpathSync} from 'node:fs' |
12 | 13 | import process from 'node:process' |
13 | 14 | import {URL, fileURLToPath, pathToFileURL} from 'node:url' |
14 | 15 | import path from 'node:path' |
@@ -129,14 +130,17 @@ function emitLegacyIndexDeprecation(url, packageJsonUrl, base, main) { |
129 | 130 |
|
130 | 131 | /** |
131 | 132 | * @param {string} path |
132 | | - * @returns {Stats} |
| 133 | + * @returns {Stats | undefined} |
133 | 134 | */ |
134 | 135 | function tryStatSync(path) { |
135 | 136 | // Note: from Node 15 onwards we can use `throwIfNoEntry: false` instead. |
136 | 137 | try { |
137 | 138 | return statSync(path) |
138 | 139 | } catch { |
139 | | - return new Stats() |
| 140 | + // Note: in Node code this returns `new Stats`, |
| 141 | + // but in Node 22 that’s marked as a deprecated internal API. |
| 142 | + // Which, well, we kinda are, but still to prevent that warning, |
| 143 | + // just yield `undefined`. |
140 | 144 | } |
141 | 145 | } |
142 | 146 |
|
@@ -251,14 +255,14 @@ function finalizeResolution(resolved, base, preserveSymlinks) { |
251 | 255 | filePath.endsWith('/') ? filePath.slice(-1) : filePath |
252 | 256 | ) |
253 | 257 |
|
254 | | - if (stats.isDirectory()) { |
| 258 | + if (stats && stats.isDirectory()) { |
255 | 259 | const error = new ERR_UNSUPPORTED_DIR_IMPORT(filePath, fileURLToPath(base)) |
256 | 260 | // @ts-expect-error Add this for `import.meta.resolve`. |
257 | 261 | error.url = String(resolved) |
258 | 262 | throw error |
259 | 263 | } |
260 | 264 |
|
261 | | - if (!stats.isFile()) { |
| 265 | + if (!stats || !stats.isFile()) { |
262 | 266 | const error = new ERR_MODULE_NOT_FOUND( |
263 | 267 | filePath || resolved.pathname, |
264 | 268 | base && fileURLToPath(base), |
@@ -996,7 +1000,7 @@ function packageResolve(specifier, base, conditions) { |
996 | 1000 | let lastPath |
997 | 1001 | do { |
998 | 1002 | const stat = tryStatSync(packageJsonPath.slice(0, -13)) |
999 | | - if (!stat.isDirectory()) { |
| 1003 | + if (!stat || !stat.isDirectory()) { |
1000 | 1004 | lastPath = packageJsonPath |
1001 | 1005 | packageJsonUrl = new URL( |
1002 | 1006 | (isScoped ? '../../../../node_modules/' : '../../../node_modules/') + |
|
0 commit comments