npm.io
2.0.5 • Published 2 weeks ago

module-root-sync

Licence
MIT
Version
2.0.5
Deps
0
Size
24 kB
Vulns
0
Weekly
0

module-root-sync

Finds the directory that the module resides in.

npm install module-root-sync
import moduleRoot from 'module-root-sync';

const root = moduleRoot(import.meta.filename);

Pass a module file path, directory, or file:// URL. The synchronous call returns the nearest matching directory and throws when no marker is found.

Options

interface RootOptions {
  name?: string;              // Custom marker filename (default: 'package.json')
  includeSynthetic?: boolean; // Include synthetic package.json files (default: false)
}

Synthetic package.json Detection

By default, moduleRoot skips "synthetic" package.json files that only exist to specify the module system (e.g., { "type": "module" }). These are commonly found in dist/esm or dist/cjs directories.

A package.json is considered synthetic if it has no name field.

// Default: skips synthetic package.json, finds real one
const root = moduleRoot(import.meta.filename);

// Include synthetic package.json files
const root = moduleRoot(import.meta.filename, { includeSynthetic: true });

// Custom marker file (synthetic detection only applies to package.json)
const root = moduleRoot(import.meta.filename, { name: 'tsconfig.json' });
Migration from v1.x

The keyExists option has been removed. If you were using keyExists: 'name' to skip synthetic packages, this is now the default behavior.

// v1.x
const root = moduleRoot(dir, { keyExists: 'name' });

// v2.x (same behavior, now default)
const root = moduleRoot(dir);
Documentation

API Docs

Keywords