Skip to content

Commit dd5c170

Browse files
authored
feat(node): Warn if ESM mode is detected (#11914)
This prints a console.warn out if we detect that we are running in ESM mode.
1 parent b61bb18 commit dd5c170

File tree

5 files changed

+95
-7
lines changed

5 files changed

+95
-7
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { loggingTransport } = require('@sentry-internal/node-integration-tests');
2+
const Sentry = require('@sentry/node');
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
transport: loggingTransport,
8+
});
9+
10+
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests');
11+
const express = require('express');
12+
13+
const app = express();
14+
15+
app.get('/test/success', (req, res) => {
16+
res.send({ response: 'response 3' });
17+
});
18+
19+
Sentry.setupExpressErrorHandler(app);
20+
21+
startExpressServerAndSendPortToRunner(app);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
2+
import * as Sentry from '@sentry/node';
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
transport: loggingTransport,
8+
});
9+
10+
import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
11+
import express from 'express';
12+
13+
const app = express();
14+
15+
app.get('/test/success', (req, res) => {
16+
res.send({ response: 'response 3' });
17+
});
18+
19+
Sentry.setupExpressErrorHandler(app);
20+
21+
startExpressServerAndSendPortToRunner(app);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
3+
afterAll(() => {
4+
cleanupChildProcesses();
5+
});
6+
7+
const esmWarning =
8+
'[Sentry] You are using the Sentry SDK with an ESM build. This version of the SDK is not compatible with ESM. Please either build your application with CommonJS, or use v7 of the SDK.';
9+
10+
test('warns if using ESM', async () => {
11+
const runner = createRunner(__dirname, 'server.mjs').ignore('session', 'sessions', 'event').start();
12+
13+
await runner.makeRequest('get', '/test/success');
14+
15+
expect(runner.getLogs()).toContain(esmWarning);
16+
});
17+
18+
test('does not warn if using CJS', async () => {
19+
const runner = createRunner(__dirname, 'server.js').ignore('session', 'sessions', 'event').start();
20+
21+
await runner.makeRequest('get', '/test/success');
22+
23+
expect(runner.getLogs()).not.toContain(esmWarning);
24+
});

dev-packages/node-integration-tests/utils/runner.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export function createRunner(...paths: string[]) {
159159
let dockerOptions: DockerOptions | undefined;
160160
let ensureNoErrorOutput = false;
161161
let expectError = false;
162+
const logs: string[] = [];
162163

163164
if (testPath.endsWith('.ts')) {
164165
flags.push('-r', 'ts-node/register');
@@ -335,12 +336,14 @@ export function createRunner(...paths: string[]) {
335336
child?.kill();
336337
});
337338

338-
if (ensureNoErrorOutput) {
339-
child.stderr?.on('data', (data: Buffer) => {
340-
const output = data.toString();
339+
child.stderr?.on('data', (data: Buffer) => {
340+
const output = data.toString();
341+
logs.push(output.trim());
342+
343+
if (ensureNoErrorOutput) {
341344
complete(new Error(`Expected no error output but got: '${output}'`));
342-
});
343-
}
345+
}
346+
});
344347

345348
child.on('close', () => {
346349
hasExited = true;
@@ -389,6 +392,8 @@ export function createRunner(...paths: string[]) {
389392
let splitIndex = -1;
390393
while ((splitIndex = buffer.indexOf(0xa)) >= 0) {
391394
const line = buffer.subarray(0, splitIndex).toString();
395+
logs.push(line.trim());
396+
392397
buffer = Buffer.from(buffer.subarray(splitIndex + 1));
393398
// eslint-disable-next-line no-console
394399
if (process.env.DEBUG) console.log('line', line);
@@ -402,6 +407,9 @@ export function createRunner(...paths: string[]) {
402407
childHasExited: function (): boolean {
403408
return hasExited;
404409
},
410+
getLogs(): string[] {
411+
return logs;
412+
},
405413
makeRequest: async function <T>(
406414
method: 'get' | 'post',
407415
path: string,

packages/node/src/sdk/init.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ import { defaultStackParser, getSentryRelease } from './api';
3939
import { NodeClient } from './client';
4040
import { initOpenTelemetry } from './initOtel';
4141

42-
function getCjsOnlyIntegrations(isCjs = typeof require !== 'undefined'): Integration[] {
43-
return isCjs ? [modulesIntegration()] : [];
42+
function isCjs(): boolean {
43+
return typeof require !== 'undefined';
44+
}
45+
46+
function getCjsOnlyIntegrations(): Integration[] {
47+
return isCjs() ? [modulesIntegration()] : [];
4448
}
4549

4650
/** Get the default integrations for the Node Experimental SDK. */
@@ -85,6 +89,16 @@ export function init(options: NodeOptions | undefined = {}): void {
8589
}
8690
}
8791

92+
if (!isCjs()) {
93+
// We want to make sure users see this warning
94+
consoleSandbox(() => {
95+
// eslint-disable-next-line no-console
96+
console.warn(
97+
'[Sentry] You are using the Sentry SDK with an ESM build. This version of the SDK is not compatible with ESM. Please either build your application with CommonJS, or use v7 of the SDK.',
98+
);
99+
});
100+
}
101+
88102
setOpenTelemetryContextAsyncContextStrategy();
89103

90104
const scope = getCurrentScope();

0 commit comments

Comments
 (0)