New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

module-compat

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

module-compat

Module type detection and loading for CJS and ESM compatibility

latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
1
Created
Source

module-compat

Module type detection and loading for CJS and ESM compatibility.

Installation

npm install module-compat

API

Type Detection

import { moduleType, extToModuleType } from 'module-compat';

// Detect from file path (checks extension + package.json)
moduleType('/path/to/file.js'); // 'module' | 'commonjs'

// Detect from extension only (no filesystem access)
extToModuleType('.mjs'); // 'module'
extToModuleType('.cjs'); // 'commonjs'
extToModuleType('.js');  // undefined (need package.json check)

Capability Detection

import { supportsESM, supportsSyncRequireESM } from 'module-compat';

supportsESM();           // true if Node 12+
supportsSyncRequireESM(); // true if Node 23+

Module Loading

import { loadModule, loadModuleSync } from 'module-compat';

// Callback-based (works on all Node versions)
loadModule('/path/to/module.mjs', (err, mod) => {
  if (err) throw err;
  console.log(mod);
});

// With options
loadModule('/path/to/module.mjs', { interop: 'raw' }, (err, mod) => {
  // mod is the full namespace: { default, namedExport1, ... }
});

// Sync (CJS always works, ESM requires Node 23+)
const mod = loadModuleSync('/path/to/module.cjs');

Interop Modes

Control how ESM default exports are handled:

ModeBehavior
'default'Extract .default if present (default)
'raw'Return module namespace as-is
'typescript'Check __esModule flag, then extract default
// ESM module: export default fn; export function helper() {}

// interop: 'default' (default)
loadModule('module.mjs', (err, mod) => {
  // mod = fn (the default export)
});

// interop: 'raw'
loadModule('module.mjs', { interop: 'raw' }, (err, mod) => {
  // mod = { default: fn, helper: [Function] }
});

Node Version Support

Node VersionCJSESM (async)ESM (sync)
< 12YesNoNo
12-22YesYesNo
23+YesYesYes

Keywords

module

FAQs

Package last updated on 19 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts