Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { loadModule } from '@sentry/core/server';

// The default `existingModule` argument must not reference a CJS-only binding: default
// parameters are evaluated before the function body, so a bare `module` would throw
// outside the try/catch that is supposed to make this helper degrade gracefully.
loadModule('node:path');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { afterAll, describe, test } from 'vitest';
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';

afterAll(() => {
cleanupChildProcesses();
});

describe('loadModule', () => {
test('does not throw when called without `existingModule` from ESM', async () => {
await createRunner(__dirname, 'app.mjs').ensureNoErrorOutput().start().completed();
});
});
10 changes: 8 additions & 2 deletions packages/core/src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ function dynamicRequire(mod: any, request: string): any {
* @param existingModule module to use for requiring
* @returns possibly required module
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function loadModule<T>(moduleName: string, existingModule: any = module): T | undefined {
export function loadModule<T>(
moduleName: string,
// Default parameters are evaluated before the body runs, so a bare `module` would throw a
// ReferenceError in ESM before reaching the try/catch below that makes this helper degrade
// gracefully. Guard it so the ESM build resolves to `undefined` instead of crashing.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
existingModule: any = typeof module !== 'undefined' ? module : undefined,
): T | undefined {
let mod: T | undefined;

try {
Expand Down
20 changes: 20 additions & 0 deletions packages/core/test/lib/utils/node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from 'vitest';
import { loadModule } from '../../../src/utils/node';

// vitest's `module` shim has no `require`, so tests hand in an explicit CJS-like module object.
const cjsModule = { require };

describe('loadModule', () => {
it('loads a module via the given `existingModule`', () => {
const path = loadModule<{ join: unknown }>('path', cjsModule);
expect(path?.join).toBeTypeOf('function');
});

it('returns undefined for a module that cannot be resolved', () => {
expect(loadModule('@sentry/this-module-does-not-exist', cjsModule)).toBeUndefined();
});

it('returns undefined instead of throwing when `existingModule` cannot require', () => {
expect(loadModule('path', undefined)).toBeUndefined();
});
});
Loading