Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Module Systems

Node.js module.findPackageJSON() method

Reference for module.findPackageJSON() in Node.js: find the package.json for a file, a relative path or an installed package, with base argument and edge cases.

module.findPackageJSON(specifier, base) returns the absolute path of the package.json that governs a module. For a relative path or a file URL it is the nearest package.json found by walking up the folder tree. For a bare package name it is that installed package's own package.json.

It does the file lookup that Node itself uses to read "type", "exports" and "main", so tools no longer need to walk the directories by hand. It returns the path only; reading and parsing the file is up to you.

NODE
import { findPackageJSON } from 'node:module';

const file = findPackageJSON('..', import.meta.url);

Try it

Node.js From a file, a folder and a package name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js When nothing is found

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
import { findPackageJSON } from 'node:module';

findPackageJSON(specifier: string | URL, base?: string | URL): string | undefined
Name Description
specifier What to look up: a relative path ('./x.js', '..'), a file: URL or a package name ('helper', '@scope/pkg').
base The file or URL the lookup starts from. Required for relative paths and bare names; use import.meta.url. It may be omitted when specifier is an absolute file: URL or a path.

Returns: the absolute path of the package.json as a string, or undefined if none applies.

Errors: ERR_MODULE_NOT_FOUND when a bare package name cannot be resolved or a relative path points at nothing that exists, and ERR_INVALID_URL_SCHEME for a URL that is not file: (so node:fs is rejected: built-in modules have no package.json).

Usage notes

  • Two different questions. A relative specifier asks "which package.json owns this file?", a bare name asks "where is that dependency's package.json?". The second works even if the package hides it behind "exports", which require.resolve('pkg/package.json') cannot do.
  • The result is the package scope. The nearest file wins, even if it is an empty {}. That matches how Node decides "type".
  • Returns a path, not data. Read it with fs, or load JSON with an import attribute: import pkg from './package.json' with { type: 'json' } when the location is fixed.
  • The target must exist. A relative specifier is resolved like an import, so a missing file throws instead of returning undefined.
  • Not for the current directory. The lookup starts from base, not from process.cwd(). For the app's own manifest, pass the entry file's URL.
  • Availability. Added in Node 23.2 and 22.14; older versions need a manual walk up the directory tree.
  • Do not cache results across installs. Package locations change after npm install, so look them up when you need them.
  • Stability. The API is newer than most of node:module; check your Node version's documentation for its current status.