diff --git a/runtime/reference/cli/eval.md b/runtime/reference/cli/eval.md index 8fc21a087..df2a55d04 100644 --- a/runtime/reference/cli/eval.md +++ b/runtime/reference/cli/eval.md @@ -24,14 +24,25 @@ TypeScript works out of the box: deno eval "const greeting: string = 'Hello'; console.log(greeting)" ``` -## CommonJS support +## CommonJS and ESM auto-detection -CommonJS modules are automatically recognized and supported: +Starting in Deno 2.8, `deno eval` inspects the snippet for `import` / +`export` declarations and treats it as ESM if it finds any. Otherwise the +snippet runs as CommonJS — meaning `require`, `module.exports`, and friends +work without any flag. ```sh +# Detected as CommonJS — no --ext=cjs needed deno eval "const path = require('path'); console.log(path.join('a', 'b'))" + +# Detected as ESM because of the static import +deno eval "import { ok } from 'node:assert'; ok(true); console.log('ok')" ``` +If you need to override the heuristic — for example when a snippet has no +imports but you still want it parsed as ESM — pass `--ext=mjs` (or +`--ext=cjs` to force CommonJS). + ## Printing expression results Use `--print` (or `-p`) to evaluate an expression and print its result, similar