module.findPackageJSON(specifier[, base])
¥Stability: 1.1 - Active Development
-
specifier<string> | <URL> 要检索其package.json的模块的说明符。传递裸说明符时,将返回包根目录下的package.json。传递相对说明符或绝对说明符时,将返回最近的父package.json。¥
specifier<string> | <URL> The specifier for the module whosepackage.jsonto retrieve. When passing a bare specifier, thepackage.jsonat the root of the package is returned. When passing a relative specifier or an absolute specifier, the closest parentpackage.jsonis returned. -
base<string> | <URL> 包含模块的绝对位置(file:URL 字符串或 FS 路径)。对于 CJS,使用__filename(而不是__dirname!);对于 ESM,使用import.meta.url。如果specifier是absolute specifier,则无需传递它。¥
base<string> | <URL> The absolute location (file:URL string or FS path) of the containing module. For CJS, use__filename(not__dirname!); for ESM, useimport.meta.url. You do not need to pass it ifspecifieris anabsolute specifier. -
返回:<string> | <undefined> 如果找到
package.json,则为路径。当specifier是包时,包的根package.json;当相对或未解析时,最接近specifier的package.json。¥Returns: <string> | <undefined> A path if the
package.jsonis found. Whenspecifieris a package, the package's rootpackage.json; when a relative or unresolved, the closestpackage.jsonto thespecifier.
警告:请勿使用它来尝试确定模块格式。有很多因素影响该决定;package.json 的
type字段是最不确定的(ex 文件扩展名取代它,加载器钩子取代它)。¥Caveat: Do not use this to try to determine module format. There are many things affecting that determination; the
typefield of package.json is the least definitive (ex file extension supersedes it, and a loader hook supersedes that).
警告:目前,这仅利用了内置的默认解析器;如果
resolve自定义钩子 已注册,它们将不会影响解决方案。这可能会在未来发生变化。¥Caveat: This currently leverages only the built-in default resolver; if
resolvecustomization hooks are registered, they will not affect the resolution. This may change in the future.
/path/to/project
├ packages/
├ bar/
├ bar.js
└ package.json // name = '@foo/bar'
└ qux/
├ node_modules/
└ some-package/
└ package.json // name = 'some-package'
├ qux.js
└ package.json // name = '@foo/qux'
├ main.js
└ package.json // name = '@foo' // /path/to/project/packages/bar/bar.js
import { findPackageJSON } from 'node:module';
findPackageJSON('..', import.meta.url);
// '/path/to/project/package.json'
// Same result when passing an absolute specifier instead:
findPackageJSON(new URL('../', import.meta.url));
findPackageJSON(import.meta.resolve('../'));
findPackageJSON('some-package', import.meta.url);
// '/path/to/project/packages/bar/node_modules/some-package/package.json'
// When passing an absolute specifier, you might get a different result if the
// resolved module is inside a subfolder that has nested `package.json`.
findPackageJSON(import.meta.resolve('some-package'));
// '/path/to/project/packages/bar/node_modules/some-package/some-subfolder/package.json'
findPackageJSON('@foo/qux', import.meta.url);
// '/path/to/project/packages/qux/package.json'// /path/to/project/packages/bar/bar.js
const { findPackageJSON } = require('node:module');
const { pathToFileURL } = require('node:url');
const path = require('node:path');
findPackageJSON('..', __filename);
// '/path/to/project/package.json'
// Same result when passing an absolute specifier instead:
findPackageJSON(pathToFileURL(path.join(__dirname, '..')));
findPackageJSON('some-package', __filename);
// '/path/to/project/packages/bar/node_modules/some-package/package.json'
// When passing an absolute specifier, you might get a different result if the
// resolved module is inside a subfolder that has nested `package.json`.
findPackageJSON(pathToFileURL(require.resolve('some-package')));
// '/path/to/project/packages/bar/node_modules/some-package/some-subfolder/package.json'
findPackageJSON('@foo/qux', __filename);
// '/path/to/project/packages/qux/package.json'