# ESM and CJS

Canonical URL: https://developers.iagenify.com/sdk/esm-and-cjs/
Language: en

The package is ESM. A CommonJS build exists for require, and it is a bundle, not a transpiled copy — which is why one habit matters after changing the source.

Status: type: module · dual entry point · no runtime dependencies

## The entry points {#map}

```javascript
{
  "type": "module",
  "main": "./dist/index.cjs",
  "exports": {
    ".": {
      "import": "./src/index.js",     // ESM gets the source
      "require": "./dist/index.cjs"   // CJS gets the bundle
    }
  },
  "engines": { "node": ">=18" }
}
```

`import` reads the published source, so stack traces point at real files inside the package. `require` reads a single pre-bundled file.

## Node builtins stay external {#builtins}

Neither entry point inlines `node:fs`, `node:os`, `node:path`, `node:crypto` or `node:child_process`. They are imported at runtime, behind a check, which is what lets the same file load where they do not exist: the import simply fails and the SDK uses the fallback for that runtime.

The practical consequence is in your bundler config, not in your code — see [Install](/sdk/install#bundlers).

## Mixing the two {#interop}

In a CommonJS file, the named exports come off the object. In an ESM file, named imports work directly. Both give the same classes, so `instanceof` holds — as long as one process loads one of the two, not both.

```javascript
// CommonJS
const { IaGenify, NotEnoughCredits } = require("iagenify");

// CommonJS reaching for the ESM entry point, when you need it
const { IaGenify } = await import("iagenify");
```

> **Do not load both in one process**
>
> A value thrown by the ESM copy is not an instanceof the CJS copy's class. The error still carries name, status and code, so test those if you cannot avoid the mix.

## TypeScript {#typescript}

The package ships no type declarations. Under `moduleResolution: "bundler"` or `"node16"` the import resolves; the shapes are untyped.

```javascript
// iagenify.d.ts — enough to stop the compiler complaining
declare module "iagenify" {
  export class IaGenify {
    constructor(options: {
      apiKey?: string; baseUrl?: string; storage?: unknown;
      sessionPath?: string; appName?: string; timeout?: number;
    });
    [key: string]: any;
  }
  export class IaGenifyError extends Error { status: number; code: string; details?: unknown }
  export class Unauthorized extends IaGenifyError {}
  export class NotEnoughCredits extends IaGenifyError { required: number; available: number }
}
```

```python
# Python has no ESM/CommonJS split: one package, one import path.
# Your own pyproject.toml only declares the dependency:
#
# [project]
# name = "my-app"
# requires-python = ">=3.10"
# dependencies = ["iagenify>=0.1.0"]
from iagenify import IaGenify
```

```python
# Python has one import statement for every kind of script
from iagenify import IaGenify, NotEnoughCredits

# Importing lazily, when you need it
import importlib
iagenify = importlib.import_module("iagenify")
```

```python
# Type checkers read the package's Python signatures directly:
# no separate declaration file is needed.
from iagenify import IaGenify, IaGenifyError, Unauthorized, NotEnoughCredits

api: IaGenify = IaGenify(api_key=api_key, base_url=None, session_path=None, app_name=None, timeout=60)

try:
    api.get_user()
except NotEnoughCredits as error:      # error.required, error.available
    ...
except IaGenifyError as error:         # error.status, error.code, error.details
    ...
```
