grafana / grafana/pyroscope-nodejs

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

Open
#198 4 comments 7 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
51
Forks
36
PR merge metrics
No merged PRs in 30d

Description

## 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. (/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 (4cad0a46a165068137caa0ea53fbe443d2eb2c8e)
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`:
```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)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.