# module-root-sync

> Finds the directory that the modules resides in

Latest version **2.0.5** (published 2026-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install module-root-sync
pnpm add module-root-sync
yarn add module-root-sync
bun add module-root-sync
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2026-09-12 |
| First published | 2024-12-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=0.8 |
| Dependencies | 0 |
| Unpacked size | 23.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Kevin Malakoff |
| Maintainers | kmalakoff |
| Keywords | package, root, directory |

## Links

- npm: https://www.npmjs.com/package/module-root-sync
- Repository: https://github.com/kmalakoff/module-root-sync
- Homepage: https://github.com/kmalakoff/module-root-sync#README.md
- Issues: https://github.com/kmalakoff/module-root-sync/issues
- npm.io page: https://npm.io/package/module-root-sync

## Recent versions

- 2.0.5 (latest) — 2026-09-12
- 2.0.4 — 2026-08-31
- 2.0.3 — 2026-05-26
- 2.0.2 — 2025-12-12
- 2.0.1 — 2025-12-12
- 2.0.0 — 2025-12-10
- 1.2.8 — 2025-12-09
- 1.2.7 — 2025-12-09
- 1.2.6 — 2025-12-04
- 1.2.5 — 2025-12-04
- 1.2.4 — 2025-12-02
- 1.2.3 — 2025-11-06
- 1.2.2 — 2025-10-21
- 1.2.1 — 2025-10-21
- 1.2.0 — 2025-06-28
- … 22 more at https://npm.io/package/module-root-sync/versions

## README

# module-root-sync

Finds the directory that the module resides in.

```bash
npm install module-root-sync
```

```typescript
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

```typescript
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.

```typescript
// 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.

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

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

### Documentation

[API Docs](https://kmalakoff.github.io/module-root-sync/)

---
_Source: https://npm.io/package/module-root-sync · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
