grafana / pyroscope-nodejs

Pyroscope NodeJS integration
Apache License 2.0
31 stars 21 forks source link

NodeJS cannot find module regenerator-runtime/runtime #32

Open BryanHunt opened 1 year ago

BryanHunt commented 1 year ago

We are attempting to add Pyroscope to one of our NodeJS framework modules. Pyroscope fails to load with the following error:

Error: Cannot find module '/Users/bhunt/Development/*****/modules/microservice/node_modules/regenerator-runtime/runtime' imported from /Users/bhunt/Development/*****/modules/microservice/node_modules/@pyroscope/nodejs/dist/esm/index.js
    at finalizeResolution (/Users/bhunt/Development/*****/modules/microservice/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:366:11)
    at moduleResolve (/Users/bhunt/Development/*****/modules/microservice/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:801:10)
    at Object.defaultResolve (/Users/bhunt/Development/*****/modules/microservice/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:912:11)
    at /Users/bhunt/Development/*****/modules/microservice/node_modules/ts-node/src/esm.ts:218:35
    at entrypointFallback (/Users/bhunt/Development/*****/modules/microservice/node_modules/ts-node/src/esm.ts:168:34)
    at /Users/bhunt/Development/*****/modules/microservice/node_modules/ts-node/src/esm.ts:217:14
    at addShortCircuitFlag (/Users/bhunt/Development/*****/modules/microservice/node_modules/ts-node/src/esm.ts:409:21)
    at resolve (/Users/bhunt/Development/*****/modules/microservice/node_modules/ts-node/src/esm.ts:197:12)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36)

We are running NodeJS v16.19.1 with the following mocha configuration:

{
  "mocha": {
    "reporter": "mocha-multi-reporters",
    "reporter-options": [
      "configFile=mocha-reporter-config.json"
    ],
    "loader": "ts-node/esm"
  }
}

Hacking the module and changing the import to:

import 'regenerator-runtime/runtime.js';

seems to fix the problem.

BryanHuntNV commented 1 year ago

Any chance we can get a release with this fix?

Bruno-DaSilva commented 7 months ago

FYI after the big recent refactor, this now requires all relative paths (there are many more now) to end in .js.

KKonstantinov commented 7 months ago

Same here for node v20, ESM, typescript. Any fix available?

abustany commented 3 months ago

If that helps anyone, here's a patch that applies to the built version of version 0.3.11 (can be used with npm/pnpm patch):

diff --git a/dist/esm/express/middleware.js b/dist/esm/express/middleware.js
index 135754317f3810fc0d2b59db6c69ebd3c10e48ad..1886f7d1afb26077d80cd4aca2a8ab366d8a4b17 100644
--- a/dist/esm/express/middleware.js
+++ b/dist/esm/express/middleware.js
@@ -1,5 +1,5 @@
 import debug from 'debug';
-import { getProfiler } from '../utils/pyroscope-profiler';
+import { getProfiler } from '../utils/pyroscope-profiler.js';
 import { encode } from '@datadog/pprof';
 const log = debug('pyroscope');
 async function collectProfile(profiler) {
diff --git a/dist/esm/index.js b/dist/esm/index.js
index b80769c765a85af1b88f3d76bd42bbbd6122719f..0a509e604b2539184fb7f8bde7532b0e74fefbe1 100644
--- a/dist/esm/index.js
+++ b/dist/esm/index.js
@@ -1,13 +1,13 @@
-import 'regenerator-runtime/runtime';
-import expressMiddleware from './express/middleware';
-import { PyroscopeProfiler } from './profilers/pyroscope-profiler';
-import { checkPyroscopeConfig } from './utils/check-pyroscope-config';
-import { getProfiler, setProfiler } from './utils/pyroscope-profiler';
-import { processConfig } from './utils/process-config';
-import { getEnv } from './utils/get-env';
+import 'regenerator-runtime/runtime.js';
+import expressMiddleware from './express/middleware.js';
+import { PyroscopeProfiler } from './profilers/pyroscope-profiler.js';
+import { checkPyroscopeConfig } from './utils/check-pyroscope-config.js';
+import { getProfiler, setProfiler } from './utils/pyroscope-profiler.js';
+import { processConfig } from './utils/process-config.js';
+import { getEnv } from './utils/get-env.js';
 import { setLogger as datadogSetLogger } from '@datadog/pprof';
-import { setLogger as ourSetLogger } from './logger';
-import { SourceMapper } from './sourcemapper';
+import { setLogger as ourSetLogger } from './logger.js';
+import { SourceMapper } from './sourcemapper.js';
 export function init(config = {}) {
     checkPyroscopeConfig(config);
     const processedConfig = processConfig(config, getEnv());
diff --git a/dist/esm/profilers/pyroscope-profiler.js b/dist/esm/profilers/pyroscope-profiler.js
index f6c5720ee650f17fe27373a0eb4edd88644fda76..e5f7f63037a304a3cca896348904669b7ef20f65 100644
--- a/dist/esm/profilers/pyroscope-profiler.js
+++ b/dist/esm/profilers/pyroscope-profiler.js
@@ -1,7 +1,7 @@
-import { PyroscopeApiExporter } from '../pyroscope-api-exporter';
-import { ContinuousProfiler } from './continuous-profiler';
-import { WallProfiler } from './wall-profiler';
-import { HeapProfiler } from './heap-profiler';
+import { PyroscopeApiExporter } from '../pyroscope-api-exporter.js';
+import { ContinuousProfiler } from './continuous-profiler.js';
+import { WallProfiler } from './wall-profiler.js';
+import { HeapProfiler } from './heap-profiler.js';
 const MICROS_PER_SECOND = 1e6;
 const MS_PER_SECOND = 1e3;
 const B_PER_MB = 1024;
diff --git a/dist/esm/pyroscope-api-exporter.js b/dist/esm/pyroscope-api-exporter.js
index 98c411275c9b9b9a67f1505e23b9f9953d298e61..5f83c95a43031356c6c96d7622beda16a116cc73 100644
--- a/dist/esm/pyroscope-api-exporter.js
+++ b/dist/esm/pyroscope-api-exporter.js
@@ -2,8 +2,8 @@ import { URL } from 'node:url';
 import { encode } from '@datadog/pprof';
 import axios from 'axios';
 import FormData from 'form-data';
-import { dateToUnixTimestamp } from './utils/date-to-unix-timestamp';
-import { processProfile } from './utils/process-profile';
+import { dateToUnixTimestamp } from './utils/date-to-unix-timestamp.js';
+import { processProfile } from './utils/process-profile.js';
 import debug from 'debug';
 const log = debug('pyroscope');
 export class PyroscopeApiExporter {
diff --git a/dist/esm/sourcemapper.js b/dist/esm/sourcemapper.js
index 511afb642c04e2dcce220b2e5e29dec48475e9dd..73fa20d3845bb0011cfe2b04bd7cc8bc03d8f8a2 100644
--- a/dist/esm/sourcemapper.js
+++ b/dist/esm/sourcemapper.js
@@ -21,7 +21,7 @@
 import * as fs from 'fs';
 import * as path from 'path';
 import * as sourceMap from 'source-map';
-import { logger } from './logger';
+import { logger } from './logger.js';
 import pLimit from 'p-limit';
 const readFile = fs.promises.readFile;
 const CONCURRENCY = 10;