feat(eval): auto-detect CJS vs ESM in deno eval#32472
Merged
bartlomieju merged 9 commits intomainfrom Mar 17, 2026
Merged
Conversation
Add `-e/--eval` flag to the top-level `deno` command so that `deno -e "require(...)"` evaluates the code as CommonJS. This defaults to `--ext=cjs` unless explicitly overridden. Co-authored-by: bartlomieju <13602871+bartlomieju@users.noreply.github.com>
Co-authored-by: bartlomieju <13602871+bartlomieju@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add -e/--eval flag for CommonJS module evaluation
feat: add Mar 5, 2026
-e/--eval top-level flag for CommonJS evaluation
Instead of defaulting to CJS, use deno_ast's compute_is_script() to detect whether eval code is a script (CJS) or module (ESM). This applies to both `deno -e` and `deno eval`. The --ext flag still overrides auto-detection when explicitly provided. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
-e/--eval top-level flag for CommonJS evaluation-e/--eval top-level flag with CJS/ESM auto-detection
The CJS/ESM auto-detection was running for both `deno -e` and `deno eval`, causing `deno eval` to treat code without import/export as CJS. This broke unit tests that use Deno APIs (e.g. Deno.unrefTimer) because CJS mode makes setTimeout return a Timeout object instead of a number. Now auto-detection only applies to the new `-e/--eval` top-level flag. The existing `deno eval` subcommand retains its ESM default behavior. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove the `-e/--eval` top-level flag (to be added in a separate PR). Apply CJS/ESM auto-detection to `deno eval` itself — when `--ext` is not explicitly provided, the code is parsed with deno_ast to determine if it contains import/export declarations (ESM) or not (CJS). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
-e/--eval top-level flag with CJS/ESM auto-detectionThe previous auto-detection treated all code without import/export as CJS, which broke existing `deno eval` usage (e.g. timer unit tests). Now we only switch to CJS when the code is a script AND contains CJS-specific patterns (require, module.exports, __dirname, etc.). Code without these patterns defaults to ESM as before. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
bartlomieju
reviewed
Mar 17, 2026
Comment on lines
+248
to
+257
| // Only treat as CJS if it parses as a script AND contains CJS patterns. | ||
| // This preserves backward compatibility: code without imports/exports | ||
| // defaults to ESM (the longstanding deno eval behavior). | ||
| let has_cjs_patterns = is_script | ||
| && (source_code.contains("require(") | ||
| || source_code.contains("module.exports") | ||
| || source_code.contains("exports.") | ||
| || source_code.contains("__dirname") | ||
| || source_code.contains("__filename")); | ||
| if has_cjs_patterns { |
Member
There was a problem hiding this comment.
This hack was needed because of the setTimeout global split. If we had a single setTimeout type it wouldn't be needed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Working on Node compat with AI agents it's often frustrating, because they try to reproduce issue by using
deno eval 'const fs = require("fs"); ...'. Also it would be helpful in most situations to be able to use ESM/CJS interchangeably. So this PR adds auto-detect whether code passed todeno eval` is CommonJS or ESM.Uses
deno_ast::parse_program+compute_is_script()to check if the code containsimport/exportdeclarations. If it does, treat as ESM (.mjs); otherwise treat as CJS (.cjs).This means
deno eval "require('fs')"now works without needing--ext=cjs, anddeno eval "import { ok } from 'node:assert'; ..."continues to work as ESM.