Skip to content

Commit eda7a1e

Browse files
authored
fix(cjs): Use module instead of require for CJS check (#15927)
As `require` is supported in ESM as well from [Node 20.19.0](https://nodejs.org/en/blog/release/v20.19.0) onwards, the check does not work anymore. However, `module` is not available in ESM. Also mentioned in this comment: #14202 (comment) [Node: Compatibility with CommonJS](https://nodejs.org/docs/latest-v15.x/api/esm.html#esm_interoperability_with_commonjs) [Bun: Using require](https://bun.sh/docs/runtime/modules#using-require)
1 parent 8af26e5 commit eda7a1e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Diff for: packages/node/src/utils/commonjs.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
/** Detect CommonJS. */
22
export function isCjs(): boolean {
3-
return typeof require !== 'undefined';
3+
try {
4+
return typeof module !== 'undefined' && typeof module.exports !== 'undefined';
5+
} catch {
6+
return false;
7+
}
48
}

0 commit comments

Comments
 (0)