# module-definition

> Determines if a file is using a CommonJS or AMD module definition

Latest version **7.0.0** (published 2026-05-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install module-definition
pnpm add module-definition
yarn add module-definition
bun add module-definition
```

Provides the command `module-definition`.

## Health

**Score 55/100 (C)** — status: active.

Positive: esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 7.0.0 |
| Published | 2026-05-19 |
| First published | 2014-03-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Node | >=20.19.0 \|\| >=22.12.0 |
| Dependencies | 2 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Joel Kemp |
| Maintainers | pahen, mrjoelkemp, xhmikosr |

## Links

- npm: https://www.npmjs.com/package/module-definition
- Repository: https://github.com/dependents/module-definition
- Issues: https://github.com/dependents/module-definition/issues
- npm.io page: https://npm.io/package/module-definition

## Dependencies (2)

- [ast-module-types](https://npm.io/package/ast-module-types.md) ^7.0.0
- [node-source-walk](https://npm.io/package/node-source-walk.md) ^8.0.0

## Recent versions

- 7.0.0 (latest) — 2026-05-19
- 6.0.2 — 2026-04-20
- 6.0.1 — 2025-02-01
- 6.0.0 — 2024-04-10
- 5.0.1 — 2023-05-11
- 5.0.0 — 2023-05-04
- 4.1.0 — 2023-03-19
- 4.0.0 — 2022-03-03
- 3.4.0 — 2022-02-22
- 3.3.1 — 2020-11-01
- 3.3.0 — 2019-11-23
- 3.2.0 — 2019-03-18
- 3.1.0 — 2018-09-25
- 3.0.0 — 2018-08-31
- 2.2.4 — 2016-08-12
- … 18 more at https://npm.io/package/module-definition/versions

## README

# module-definition

[![CI](https://img.shields.io/github/actions/workflow/status/dependents/module-definition/ci.yml?branch=main&label=CI&logo=github)](https://github.com/dependents/module-definition/actions/workflows/ci.yml?query=branch%3Amain)
[![npm version](https://img.shields.io/npm/v/module-definition?logo=npm&logoColor=fff)](https://www.npmjs.com/package/module-definition)
[![npm downloads](https://img.shields.io/npm/dm/module-definition)](https://www.npmjs.com/package/module-definition)

Determines the module definition type (CommonJS, AMD, ES6, or none) for a given JavaScript file by walking through the AST.

```sh
npm install module-definition
```

## Usage

### ESM

```js
import getModuleType from 'module-definition';

// Async
getModuleType('myscript.js', (error, type) => {
  console.log(type);
});

// Sync
let type = getModuleType.sync('myscript.js');
console.log(type);

// From source (string or an AST)
type = getModuleType.fromSource('define({foo: "foo"});');
console.log(type);
```

### CJS

```js
const { default: getModuleType } = require('module-definition');

// Async
getModuleType('myscript.js', (error, type) => {
  console.log(type);
});

// Sync
let type = getModuleType.sync('myscript.js');
console.log(type);

// From source (string or an AST)
type = getModuleType.fromSource('define({foo: "foo"});');
console.log(type);
```

Passes one of the following strings to the given callback or returns the string in sync API:

* amd
* commonjs
* es6
* none

You may also pass an AST to `fromSource` to avoid an internal parsing of the source.

When specifying a filename, using the sync or async API, you can also provide an `options` object with an alternative `fs` implementation used to read the source file with.

```js
const myFs = GetFs();
const options = { fileSystem: myFs };

// Async
getModuleType('myscript.js', (error, type) => {
  console.log(type);
}, options);

// Sync
const type = getModuleType.sync('myscript.js', options);
```

## CLI

*Assumes a global install module-definition with `npm install -g module-definition`*

```sh
module-definition filename
```

### License

[MIT](LICENSE)

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