Skip to content

Commit ce068e5

Browse files
committed
fix(node): Make sure modulesIntegration does not crash esm apps
1 parent 6729214 commit ce068e5

File tree

3 files changed

+37
-0
lines changed
  • dev-packages/node-integration-tests/suites/esm/modules-integration
  • packages/node/src/integrations

3 files changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
2+
import * as Sentry from '@sentry/node';
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
autoSessionTracking: false,
8+
integrations: [Sentry.modulesIntegration()],
9+
transport: loggingTransport,
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
3+
afterAll(() => {
4+
cleanupChildProcesses();
5+
});
6+
7+
describe('modulesIntegration', () => {
8+
test('does not crash ESM setups', done => {
9+
createRunner(__dirname, 'app.mjs').ensureNoErrorOutput().start(done);
10+
});
11+
});

packages/node/src/integrations/modules.ts

+16
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@ import { existsSync, readFileSync } from 'node:fs';
22
import { dirname, join } from 'node:path';
33
import { defineIntegration } from '@sentry/core';
44
import type { IntegrationFn } from '@sentry/types';
5+
import { logger } from '@sentry/utils';
6+
import { DEBUG_BUILD } from '../debug-build';
7+
import { isCjs } from '../utils/commonjs';
58

69
let moduleCache: { [key: string]: string };
710

811
const INTEGRATION_NAME = 'Modules';
912

1013
const _modulesIntegration = (() => {
14+
// This integration only works in CJS contexts
15+
if (!isCjs()) {
16+
DEBUG_BUILD &&
17+
logger.warn(
18+
'modulesIntegration only works in CommonJS (CJS) environments. Remove this integration if you are using ESM.',
19+
);
20+
return {
21+
name: INTEGRATION_NAME,
22+
};
23+
}
24+
1125
return {
1226
name: INTEGRATION_NAME,
1327
processEvent(event) {
@@ -23,6 +37,8 @@ const _modulesIntegration = (() => {
2337

2438
/**
2539
* Add node modules / packages to the event.
40+
*
41+
* Only works in CommonJS (CJS) environments.
2642
*/
2743
export const modulesIntegration = defineIntegration(_modulesIntegration);
2844

0 commit comments

Comments
 (0)