module-root-sync
Finds the directory that the module resides in.
import moduleRoot from 'module-root-sync';
const root = moduleRoot(import.meta.filename);
Options
interface RootOptions {
name?: string;
includeSynthetic?: boolean;
}
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.
const root = moduleRoot(import.meta.filename);
const root = moduleRoot(import.meta.filename, { includeSynthetic: true });
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.
const root = moduleRoot(dir, { keyExists: 'name' });
const root = moduleRoot(dir);
Documentation
API Docs