Skip to content

ERR_REQUIRE_ESM: Cannot require() p-limit in CommonJS builds #198

@jacekplonka

Description

@jacekplonka

Description

The @pyroscope/nodejs package fails with ERR_REQUIRE_ESM when using the CommonJS build (dist/cjs) due to p-limit@7.x being an ESM-only module.

Error

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/@pyroscope/nodejs/node_modules/p-limit/index.js from /app/node_modules/@pyroscope/nodejs/dist/cjs/sourcemapper.js not supported.

Instead change the require of index.js in /app/node_modules/@pyroscope/nodejs/dist/cjs/sourcemapper.js to a dynamic import() which is available in all CommonJS modules.

at Object.<anonymous> (/app/node_modules/@pyroscope/nodejs/dist/cjs/sourcemapper.js:64:35)

Root Cause

  1. p-limit was upgraded to ^7.2.0 in package.json in v0.4.8 (4cad0a4)
  2. p-limit@7.x is ESM-only and cannot be require()'d
  3. src/sourcemapper.ts line 27 has: import pLimit from 'p-limit';
  4. TypeScript compiles this to require('p-limit') in the CommonJS build
  5. Runtime fails because ESM modules cannot be required from CommonJS

Reproduction

Environment:

  • @pyroscope/nodejs: 0.4.8+
  • Node.js: 20.x or 22.x
  • Module system: CommonJS

Steps:

  1. Install @pyroscope/nodejs in a CommonJS project
  2. Import and initialize the profiler
  3. Error occurs when the module loads, regardless of configuration

Workaround

Downgrade or add to package.json:

"resolutions": {
  "p-limit": "3.1.0"
}

Impact

  • Severity: High - breaks CommonJS environments
  • Affected versions: 0.4.8+ (when p-limit was upgraded to 7.x)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions