Skip to content

Commit 9b8f5f0

Browse files
committed
conditional node imports?
1 parent aac34e0 commit 9b8f5f0

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

packages/cloudflare/src/integrations/modules.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { existsSync, readFileSync } from 'node:fs';
2-
import { dirname, join } from 'node:path';
1+
import type * as NodeFs from 'node:fs';
2+
import type * as NodePath from 'node:path';
33
import type { IntegrationFn } from '@sentry/core';
44
import { isCjs } from '../utils/commonjs';
55

@@ -11,13 +11,27 @@ const INTEGRATION_NAME = 'Modules';
1111

1212
declare const __SENTRY_SERVER_MODULES__: Record<string, string>;
1313

14+
// Node utils are not available in the worker runtime, so we need to import them dynamically
15+
// So this may or may not be available at runtime
16+
let nodeUtils:
17+
| undefined
18+
| {
19+
dirname: typeof NodePath.dirname;
20+
join: typeof NodePath.join;
21+
existsSync: typeof NodeFs.existsSync;
22+
readFileSync: typeof NodeFs.readFileSync;
23+
};
24+
1425
/**
1526
* `__SENTRY_SERVER_MODULES__` can be replaced at build time with the modules loaded by the server.
1627
* Right now, we leverage this in Next.js to circumvent the problem that we do not get access to these things at runtime.
1728
*/
1829
const SERVER_MODULES = typeof __SENTRY_SERVER_MODULES__ === 'undefined' ? {} : __SENTRY_SERVER_MODULES__;
1930

2031
const _modulesIntegration = (() => {
32+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
33+
getNodeUtils();
34+
2135
return {
2236
name: INTEGRATION_NAME,
2337
processEvent(event) {
@@ -58,6 +72,22 @@ function collectModules(): ModuleInfo {
5872
};
5973
}
6074

75+
async function getNodeUtils(): Promise<void> {
76+
try {
77+
const { existsSync, readFileSync } = await import('node:fs');
78+
// eslint-disable-next-line @typescript-eslint/unbound-method
79+
const { dirname, join } = await import('node:path');
80+
81+
nodeUtils = {
82+
dirname,
83+
join,
84+
existsSync,
85+
readFileSync,
86+
};
87+
} catch {
88+
// no-empty
89+
}
90+
}
6191
/** Extract information about package.json modules from require.cache */
6292
function collectRequireModules(): ModuleInfo {
6393
const mainPaths = require.main?.paths || [];
@@ -68,6 +98,12 @@ function collectRequireModules(): ModuleInfo {
6898
const infos: ModuleInfo = {};
6999
const seen = new Set<string>();
70100

101+
if (!nodeUtils) {
102+
return infos;
103+
}
104+
105+
const { dirname, join, existsSync, readFileSync } = nodeUtils;
106+
71107
paths.forEach(path => {
72108
let dir = path;
73109

@@ -121,6 +157,12 @@ interface PackageJson {
121157
}
122158

123159
function getPackageJson(): PackageJson {
160+
if (!nodeUtils) {
161+
return {};
162+
}
163+
164+
const { join, readFileSync } = nodeUtils;
165+
124166
try {
125167
const filePath = join(process.cwd(), 'package.json');
126168
const packageJson = JSON.parse(readFileSync(filePath, 'utf8')) as PackageJson;

0 commit comments

Comments
 (0)