Skip to content

bug: CJS require('@optave/codegraph') fails with ERR_PACKAGE_PATH_NOT_EXPORTED #455

@carlos-alm

Description

@carlos-alm

Found during dogfooding v3.1.4

Severity: Medium
Command: Programmatic API via require()

Reproduction

npm init -y && npm install @optave/codegraph@3.1.4
node -e "const cg = require('@optave/codegraph'); console.log(Object.keys(cg))"

Expected behavior

CJS require() should work, returning the module exports.

Actual behavior

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in .../node_modules/@optave/codegraph/package.json

Root cause

The exports field in package.json only defines an import condition:

"exports": {
  ".": {
    "import": "./src/index.js"
  }
}

There is no require or default condition. Since the package is "type": "module", CJS consumers need an explicit export path.

Suggested fix

Either:

  1. Add a CJS wrapper: "require": "./src/index.cjs" (needs a wrapper file), or
  2. Add "default": "./src/index.js" to the exports (Node.js will attempt to load ESM, which works in some contexts), or
  3. Document that the package is ESM-only and CJS consumers must use dynamic import().

Option 3 is the simplest if CJS support isn't a priority.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdogfoodFound during dogfooding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions