diff --git a/dev-packages/node-integration-tests/suites/no-code/app.js b/dev-packages/node-integration-tests/suites/no-code/app.js new file mode 100644 index 000000000000..cb1937007297 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/no-code/app.js @@ -0,0 +1,3 @@ +setTimeout(() => { + throw new Error('Test error'); +}, 1000); diff --git a/dev-packages/node-integration-tests/suites/no-code/app.mjs b/dev-packages/node-integration-tests/suites/no-code/app.mjs new file mode 100644 index 000000000000..cb1937007297 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/no-code/app.mjs @@ -0,0 +1,3 @@ +setTimeout(() => { + throw new Error('Test error'); +}, 1000); diff --git a/dev-packages/node-integration-tests/suites/no-code/test.ts b/dev-packages/node-integration-tests/suites/no-code/test.ts new file mode 100644 index 000000000000..dfaae9de7cdc --- /dev/null +++ b/dev-packages/node-integration-tests/suites/no-code/test.ts @@ -0,0 +1,37 @@ +import { conditionalTest } from '../../utils'; +import { cleanupChildProcesses, createRunner } from '../../utils/runner'; + +const EVENT = { + exception: { + values: [ + { + type: 'Error', + value: 'Test error', + }, + ], + }, +}; + +describe('no-code init', () => { + afterAll(() => { + cleanupChildProcesses(); + }); + + test('CJS', done => { + createRunner(__dirname, 'app.js') + .withFlags('--require=@sentry/node/init') + .withMockSentryServer() + .expect({ event: EVENT }) + .start(done); + }); + + conditionalTest({ min: 18 })('--import', () => { + test('ESM', done => { + createRunner(__dirname, 'app.mjs') + .withFlags('--import=@sentry/node/init') + .withMockSentryServer() + .expect({ event: EVENT }) + .start(done); + }); + }); +}); diff --git a/packages/node/package.json b/packages/node/package.json index 4f03e9b86b98..374845ef13c8 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -41,6 +41,14 @@ "import": { "default": "./build/loader-hook.mjs" } + }, + "./init":{ + "import": { + "default": "./build/esm/init.js" + }, + "require": { + "default": "./build/cjs/init.js" + } } }, "typesVersions": { diff --git a/packages/node/rollup.npm.config.mjs b/packages/node/rollup.npm.config.mjs index f9adb7d91ca5..8e18333836ef 100644 --- a/packages/node/rollup.npm.config.mjs +++ b/packages/node/rollup.npm.config.mjs @@ -19,6 +19,7 @@ export default [ localVariablesWorkerConfig, ...makeNPMConfigVariants( makeBaseNPMConfig({ + entrypoints: ['src/index.ts', 'src/init.ts'], packageSpecificConfig: { output: { // set exports to 'named' or 'auto' so that rollup doesn't warn diff --git a/packages/node/src/init.ts b/packages/node/src/init.ts new file mode 100644 index 000000000000..245ae8573afa --- /dev/null +++ b/packages/node/src/init.ts @@ -0,0 +1,9 @@ +import { init } from './sdk/init'; + +/** + * The @sentry/node/init export can be used with the node --import and --require args to initialize the SDK entirely via + * environment variables. + * + * > SENTRY_DSN=https://examplePublicKey@o0.ingest.sentry.io/0 SENTRY_TRACES_SAMPLE_RATE=1.0 node --import=@sentry/node/init app.mjs + */ +init();