codex icon indicating copy to clipboard operation
codex copied to clipboard

TypeScript SDK CommonJS import type-checks but fails at runtime

Open dovvnloading opened this issue 3 weeks ago • 1 comments

Summary

In a CommonJS TypeScript project using module: NodeNext, an import from the published SDK type-checks successfully but fails at runtime with ERR_PACKAGE_PATH_NOT_EXPORTED. The package exposes declarations through a top-level types export condition but has no require export.

Reproduction

Tested on Windows with Node 24.11.1, TypeScript 5.9.2, and @openai/[email protected]. In a fresh directory:

npm init -y
npm install --ignore-scripts @openai/[email protected]
npm install --ignore-scripts --save-dev [email protected] @types/node@20 @modelcontextprotocol/[email protected]

The MCP SDK is installed here to isolate this issue from #61009. Save the following as repro.cts:

import { Codex } from "@openai/codex-sdk";
console.log(typeof Codex);
npx tsc --strict --module NodeNext --moduleResolution NodeNext --target ES2022 --outDir out repro.cts
node out/repro.cjs

The compiler exits successfully; Node then reports:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in .../@openai/codex-sdk/package.json

As a control, the same source saved as repro.mts compiled and ran successfully as ESM. No model call is needed.

Expected / possible fix

If CommonJS is supported, provide an appropriate require entry. If the SDK is ESM-only, scope the declaration condition to the import export so NodeNext rejects the CommonJS import at compile time, and document ESM usage. In a temporary package-metadata control, nesting types under import made the .cts compile fail and preserved the successful .mts compile.

The package export map has a top-level types condition alongside import. Add ESM and CommonJS consumer compilation/runtime checks to packaging tests.

Workaround: use an ESM consumer (for example, an .mts entry point).

dovvnloading avatar Nov 12 '26 04:11 dovvnloading

Additional AI-assisted verification using Node.js 22.16.0 and TypeScript 5.8.3 confirms the export-condition mismatch.

With reduced package fixtures matching the upstream export map, both ESM and CommonJS consumers type-check, but CommonJS fails at runtime with ERR_PACKAGE_PATH_NOT_EXPORTED.

Nesting the types and default conditions under import preserves ESM compilation and execution while correctly rejecting CommonJS consumers at compile time.

Reference implementation:

https://github.com/sanskarIN/codex/commit/9981d8fde0318b7c688694b035b2bc50218592b6

This verifies the package-resolution behavior independently of the full SDK build.

sanskarIN avatar Nov 28 '26 14:11 sanskarIN