deno eval
Evaluate JavaScript or TypeScript code directly from the command line without creating a file.
Basic usage
deno eval "console.log('Hello, world!')"
Syntax
deno eval [OPTIONS] < CODE >
The JavaScript or TypeScript code to evaluate.
Options
Print the result of the evaluated expression.
Permission flags
All standard permission flags are supported. See the permissions guide for details.
Common options
--ext <EXT> - Set content type (ts, tsx, js, jsx)
--no-check - Skip type checking
--import-map <FILE> - Use import map
--env <FILE> - Load environment variables from file
Examples
Simple expression
deno eval "console.log(1 + 2)"
# Output: 3
Print result
deno eval --print "1 + 2"
# Output: 3
Using with permissions
# Read a file
deno eval --allow-read "console.log(Deno.readTextFileSync('README.md'))"
# Fetch from URL
deno eval --allow-net "const res = await fetch('https://example.com'); console.log(await res.text())"
TypeScript code
deno eval "const x: number = 42; console.log(x * 2)"
# Output: 84
Multi-line code
deno eval '
const items = [1, 2, 3, 4, 5];
const sum = items.reduce((a, b) => a + b, 0);
console.log("Sum:", sum);
'
# Output: Sum: 15
JSON processing
# Parse JSON
deno eval --print 'JSON.parse(`{"name":"Deno","version":"2.0"}`).name'
# Output: Deno
# Create JSON
deno eval --print 'JSON.stringify({a: 1, b: 2})'
# Output: {"a":1,"b":2}
Environment variables
deno eval --allow-env 'console.log("User:", Deno.env.get("USER"))'
Using Deno APIs
# Get Deno version
deno eval 'console.log(Deno.version)'
# List directory
deno eval --allow-read '
for await (const entry of Deno.readDir(".")) {
console.log(entry.name);
}
'
# HTTP server (quick test)
deno eval --allow-net 'Deno.serve(() => new Response("Hello!"))'
Working with dates
# Current timestamp
deno eval --print 'Date.now()'
# Formatted date
deno eval --print 'new Date().toISOString()'
Use cases
deno eval --print "Math.sqrt(144)"
deno eval --print "(100 * 1.15).toFixed(2)"
deno eval "const arr = [1,2,3]; console.log(arr.map(x => x * 2))"
deno eval --allow-read --allow-write '
const data = Deno.readTextFileSync("input.txt");
const processed = data.toUpperCase();
Deno.writeTextFileSync("output.txt", processed);
'
echo '{"value": 42}' | deno eval --print 'JSON.parse(await Deno.stdin.readAll()).value'
Tips
Use single quotes to avoid shell interpolation issues
Use --print to automatically print the result without console.log()
TypeScript is supported by default
Top-level await is supported
See also