diff --git a/src/cache.js b/src/cache.js index 7d953f3b..23ac0f85 100644 --- a/src/cache.js +++ b/src/cache.js @@ -7,6 +7,7 @@ * @see https://github.com/babel/babel-loader/issues/34 * @see https://github.com/babel/babel-loader/pull/41 */ +const nodeModule = require("node:module"); const os = require("os"); const path = require("path"); const zlib = require("zlib"); @@ -21,6 +22,15 @@ let defaultCacheDirectory = null; const gunzip = promisify(zlib.gunzip); const gzip = promisify(zlib.gzip); +const findRootPackageJSON = () => { + if (nodeModule.findPackageJSON) { + return nodeModule.findPackageJSON("..", __filename); + } else { + // todo: remove this fallback when dropping support for Node.js < 22.14 + return findUpSync("package.json"); + } +}; + /** * Read the contents from the compressed file. * @@ -136,7 +146,7 @@ const handleCache = async function (directory, params) { `discarded cache file '${file}' due to changes in external dependencies`, ); } catch { - // conitnue if cache can't be read + // continue if cache can't be read logger.debug(`discarded cache as it can not be read`); } @@ -219,7 +229,7 @@ function findCacheDir(name) { if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) { return path.join(env.CACHE_DIR, name); } - const rootPkgJSONPath = path.dirname(findUpSync("package.json")); + const rootPkgJSONPath = path.dirname(findRootPackageJSON()); if (rootPkgJSONPath) { return path.join(rootPkgJSONPath, "node_modules", ".cache", name); }