Install package-directory
mainInstall the package-directory package using npm to find the root directory of a Node.js project or npm package.
npm install package-directoryrepository·main·Indexed 42 days ago
https://github.com/sindresorhus/package-directoryA utility for finding the root directory of a Node.js project or npm package by locating its package.json file. Version 8.2.0 provides both asynchronous packageDirectory() and synchronous packageDirectorySync() methods, with options to specify the starting directory (cwd) and ignore type-only package.json files.
Install the package-directory package using npm to find the root directory of a Node.js project or npm package.
npm install package-directoryA "type-only" package.json is defined as a file that contains exactly one key, which is "type" with a string value (e.g., { "type": "module" }).
If you set ignoreTypeOnlyPackageJson: true, the search will treat these files as if no package.json exists and continue climbing up the directory tree until it finds a package.json with additional properties or reaches the root directory.
import {packageDirectory} from 'package-directory';
// This will skip package.json files that only define the module type
const directory = await packageDirectory({ignoreTypeOnlyPackageJson: true});Use the asynchronous packageDirectory() function to find the root directory of the current project. It returns a Promise that resolves to the absolute path of the project root or undefined if no package.json is found.
import {packageDirectory} from 'package-directory';
console.log(await packageDirectory());
//=> '/Users/sindresorhus/foo'The library provides both asynchronous and synchronous methods to locate the project root:
packageDirectory(options?): Returns a Promise for the project root path or undefined.packageDirectorySync(options?): Returns the project root path or undefined synchronously.Both packageDirectory() and packageDirectorySync() accept an optional options object to refine the search behavior:
cwd: The directory to start searching from. Defaults to process.cwd().ignoreTypeOnlyPackageJson: A boolean that defaults to false. When set to true, it ignores package.json files that only contain the type field. This is useful if you want to treat {"type":"module"} files as ESM scope markers rather than package roots.// Example of using options
const root = await packageDirectory({
cwd: './some/path',
ignoreTypeOnlyPackageJson: true
});Use packageDirectorySync() to find the directory containing the nearest package.json file synchronously. This is useful in environments where await is not available or preferred.
cwd: The directory to start searching from. Defaults to process.cwd().ignoreTypeOnlyPackageJson: If true, the function will skip package.json files that only contain a "type": "module" (or other string) field and continue searching up the directory tree for a more substantial package.json.import {packageDirectorySync} from 'package-directory';
const directory = packageDirectorySync({cwd: 'path/to/search'});Use packageDirectory() to find the directory containing the nearest package.json file, starting from a specified directory.
By default, it starts from the current working directory (process.cwd()).
cwd: The directory to start searching from. Defaults to process.cwd().ignoreTypeOnlyPackageJson: If true, the function will skip package.json files that only contain a "type": "module" (or other string) field and continue searching up the directory tree for a more substantial package.json.import {packageDirectory} from 'package-directory';
const directory = await packageDirectory({cwd: 'path/to/search'});Synchronously find the root directory of a Node.js project or npm package by searching upwards for a package.json file. Returns the absolute path of the project root or undefined if no package root is found.
Use this for simple scripts or when synchronous execution is required.
import {packageDirectorySync} from 'package-directory';
console.log(packageDirectorySync());
//=> '/Users/sindresorhus/foo'Both packageDirectory and packageDirectorySync accept an optional Options object to customize the search behavior:
cwd: The directory to start searching from. Defaults to process.cwd().ignoreTypeOnlyPackageJson: If set to true, the search will ignore package.json files that only contain the type field (e.g., {"type":"module"}). This allows treating such files as ESM scope markers rather than package roots. Defaults to false.type Options = {
readonly cwd?: string;
readonly ignoreTypeOnlyPackageJson?: boolean;
};