Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cjs): Use module instead of require for CJS check #15927

Merged
merged 3 commits into from
Mar 31, 2025
Merged

Conversation

s1gr1d
Copy link
Member

@s1gr1d s1gr1d commented Mar 31, 2025

As require is supported in ESM as well from Node 20.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

Bun: Using require

@@ -1,4 +1,4 @@
/** Detect CommonJS. */
export function isCjs(): boolean {
return typeof require !== 'undefined';
return typeof module !== 'undefined' && typeof module.exports !== 'undefined';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we also checking for module.exports?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some additional safety as there is maybe a variable named module. But it's more unlikely that there is one that is an object with the key exports.

Copy link
Contributor

github-actions bot commented Mar 31, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 23.27 KB - -
@sentry/browser - with treeshaking flags 23.1 KB - -
@sentry/browser (incl. Tracing) 36.68 KB - -
@sentry/browser (incl. Tracing, Replay) 73.85 KB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 67.22 KB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 78.51 KB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 91.07 KB - -
@sentry/browser (incl. Feedback) 40.41 KB - -
@sentry/browser (incl. sendFeedback) 27.9 KB - -
@sentry/browser (incl. FeedbackAsync) 32.7 KB - -
@sentry/react 25.08 KB - -
@sentry/react (incl. Tracing) 38.6 KB - -
@sentry/vue 27.49 KB - -
@sentry/vue (incl. Tracing) 38.38 KB - -
@sentry/svelte 23.3 KB - -
CDN Bundle 24.51 KB - -
CDN Bundle (incl. Tracing) 36.69 KB - -
CDN Bundle (incl. Tracing, Replay) 71.7 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 76.88 KB - -
CDN Bundle - uncompressed 71.44 KB - -
CDN Bundle (incl. Tracing) - uncompressed 108.62 KB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 219.91 KB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 232.48 KB - -
@sentry/nextjs (client) 39.89 KB - -
@sentry/sveltekit (client) 37.11 KB - -
@sentry/node 142.95 KB +0.02% +22 B 🔺
@sentry/node - without tracing 96.15 KB +0.03% +23 B 🔺
@sentry/aws-serverless 120.5 KB +0.02% +19 B 🔺

View base workflow run

@@ -1,4 +1,8 @@
/** Detect CommonJS. */
export function isCjs(): boolean {
return typeof require !== 'undefined';
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO if we do the try catch, we can omit the && module check xD as we should be safe already!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suuper nit xD

@s1gr1d s1gr1d mentioned this pull request Mar 31, 2025
3 tasks
Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@s1gr1d s1gr1d merged commit eda7a1e into develop Mar 31, 2025
103 of 104 checks passed
@s1gr1d s1gr1d deleted the sig/cjs-check branch March 31, 2025 08:43
onurtemizkan pushed a commit that referenced this pull request Apr 3, 2025
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants