Skip to content

PackageEsmType

const PackageEsmType: FilePolicyDefinition<PackageEsmTypeConfig | undefined> & object

Defined in: policies/PackageEsmType.ts:371

A policy that ensures the type field in package.json is correctly set.

This policy helps maintain consistency in how packages declare their module format, which is essential for proper module resolution in Node.js and bundlers.

requiresConfig: true

The policy can operate in two modes:

  1. Required Type: Set requiredType to enforce a specific type for all packages
  2. Detection: Set detectFromExports: true to detect the expected type from the package’s exports or main field

When detectFromExports is enabled, the policy looks for:

  • .mjs extensions or "import" conditions → expects "module"
  • .cjs extensions or "require" conditions → expects "commonjs"
import { makePolicy } from "repopo";
import { PackageEsmType } from "repopo/policies";
// Require all packages to be ESM
makePolicy(PackageEsmType, { config: { requiredType: "module" } })
// Or detect based on exports
makePolicy(PackageEsmType, { config: { detectFromExports: true } })