package-directory

repository·main·Indexed 42 days ago

https://github.com/sindresorhus/package-directory

A 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.

Tokens
1.3K
Snippets
8
Records
9
Agent score
49%

What's inside package-directory

  1. Skip type-only package.json files using ignoreTypeOnlyPackageJson

    main

    A "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});
  2. Find the project root directory with packageDirectory()

    main

    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'
  3. Use packageDirectory() and packageDirectorySync()

    main

    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.
  4. Configure packageDirectory options

    main

    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
    });
  5. Use packageDirectorySync() to find the nearest package directory synchronously

    main

    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.

    Options

    • 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'});
  6. Use packageDirectory() to find the nearest package directory asynchronously

    main

    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()).

    Options

    • 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'});
  7. Find the project root directory synchronously with packageDirectorySync()

    main

    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'
  8. Configure package-directory search options

    main

    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;
    };