Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ import { nextjsUseCacheIntegration } from './useCacheInstrumentation';

export * from '@sentry/node';

// Explicitly re-export so these are statically detectable by turbopack
export { pinoIntegration, vercelAIIntegration } from '@sentry/node';
// Explicit exports are also detectable by Node's CJS lexer when imported from ESM.
export {
addBreadcrumb,
captureException,
captureMessage,
consoleLoggingIntegration,
isEnabled,
isInitialized,
pinoIntegration,
setContext,
setTag,
vercelAIIntegration,
} from '@sentry/node';

export { captureUnderscoreErrorException } from '../common/pages-router-instrumentation/_error';

Expand Down
46 changes: 46 additions & 0 deletions packages/nextjs/test/serverExports.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawnSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { init, parse } from 'cjs-module-lexer';
Expand Down Expand Up @@ -35,3 +36,48 @@ describe('Node-only integrations are statically detectable exports from every ru
},
);
});

// Regression for https://github.com/getsentry/sentry-javascript/issues/22791.
describe('common server APIs imported by plain Node ESM', () => {
const commonExports = [
'addBreadcrumb',
'captureException',
'captureMessage',
'consoleLoggingIntegration',
'isEnabled',
'isInitialized',
'setContext',
'setTag',
];
let staticExports: string[];

beforeAll(async () => {
await init();
staticExports = parse(readFileSync(resolve(__dirname, '../build/cjs/index.server.js'), 'utf8')).exports;
});

it.each(commonExports)('statically exports `%s` from the CJS server build', name => {
expect(staticExports).toContain(name);
});

it('exposes named and namespace imports with the same values as require', () => {
const script = `
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import * as namespace from '@sentry/nextjs';
import { ${commonExports.join(', ')} } from '@sentry/nextjs';
const required = createRequire(import.meta.url)('@sentry/nextjs');
const named = { ${commonExports.join(', ')} };
for (const [name, value] of Object.entries(named)) {
assert.equal(typeof value, 'function', name);
assert.equal(value, required[name], name);
assert.equal(namespace[name], required[name], name);
}
`;
const result = spawnSync(process.execPath, ['--input-type=module', '-e', script], {
cwd: resolve(__dirname, '..'),
encoding: 'utf8',
});
expect(result.status, result.stderr).toBe(0);
});
});
Loading