Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions runtime/reference/cli/eval.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down