module-definition

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

MIT 33 个版本
安装
npm install module-definition
yarn add module-definition
pnpm add module-definition
bun add module-definition
README

module-definition

CI npm version npm downloads

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

npm install module-definition

Usage

ESM

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

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.

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

module-definition filename

License

MIT

版本列表
7.0.0 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
2.2.3 2016-06-16
2.2.2 2015-07-04
2.2.1 2015-01-20
2.2.0 2014-12-14
2.1.0 2014-11-12
2.0.1 2014-11-04
2.0.0 2014-10-31
1.2.4 2014-06-09
1.2.3 2014-06-09
1.2.2 2014-06-08
1.2.1 2014-06-08
1.2.0 2014-06-08
1.1.0 2014-06-01
1.0.4 2014-03-28
1.0.3 2014-03-20
1.0.2 2014-03-18
1.0.1 2014-03-18
1.0.0 2014-03-18