How to Create and Use the Deno REPL: A Complete Developer Guide
Launch the Deno REPL by running the deno command without arguments, which starts an interactive TypeScript/JavaScript shell with full permissions enabled, allowing immediate execution of code, module imports, and access to the Deno namespace.
The Deno REPL (Read-Eval-Print Loop) provides an interactive environment for experimenting with TypeScript and JavaScript without creating files. According to the denoland/deno source code, understanding how to create and use the Deno REPL involves tracing the CLI argument parsing through to the worker initialization and evaluation loop.
Starting the Deno REPL
The simplest way to create a Deno REPL session is to invoke the binary without specifying a script file.
deno
When executed without arguments, the Deno CLI detects the absence of a target script and routes execution to the REPL implementation. This behavior is defined in cli/args/flags.rs, which handles the argument parsing and identifies when to enter interactive mode.
By default, the REPL starts with full permissions (--allow-all is automatically applied), removing the need to grant individual permissions for network, file system, or environment access during interactive sessions.
REPL Architecture and Source Code
Understanding the internal structure of how to create and use the Deno REPL requires examining the worker factory and evaluation loop.
CLI Entry Point and Flag Parsing
The REPL entry point begins in cli/args/flags.rs, where the flag parser defines REPL-specific options including:
--eval: Execute code before entering the REPL--allow-all: Automatically enabled for REPL sessions- Environment variable handling for
DENO_REPL_HISTORY
Worker Creation and Permissions
The main REPL logic resides in cli/tools/repl/mod.rs. This module creates a CliMainWorkerFactory that spins up a main worker with full permissions enabled. Unlike standard script execution, the REPL worker bypasses individual permission prompts to ensure a smooth interactive experience.
The Evaluation Loop
Inside the worker, the JavaScript side runs the built-in Deno.core.evalContext loop. This loop performs the following sequence:
- Reads a line from standard input
- Evaluates the code in the global context
- Prints the result or error to stdout
- Prompts for the next line
This cycle continues until the user exits the session.
Command History and Persistence
The Deno REPL maintains command history across sessions, enabling you to recall previous commands using arrow keys.
Default History Location
History persistence is managed by the DenoDir implementation in libs/resolver/cache/deno_dir.rs. By default, the history file is stored at:
$DENO_DIR/repl_history.txt
Where $DENO_DIR defaults to $HOME/.cache/deno on Linux, $HOME/Library/Caches/deno on macOS, or %LOCALAPPDATA%/deno on Windows.
Customizing History with DENO_REPL_HISTORY
You can override the default history location or disable history entirely using the DENO_REPL_HISTORY environment variable:
# Use a custom history file
export DENO_REPL_HISTORY="$HOME/.my_deno_history"
deno
# Disable history completely
export DENO_REPL_HISTORY=""
deno
Advanced REPL Usage
Beyond basic interaction, the Deno REPL supports several advanced features for power users.
Evaluating Code Snippets with --eval
You can execute code immediately upon starting the REPL without entering interactive mode first using the --eval flag:
deno --eval "console.log('Hello from REPL')"
This prints the result and then drops you into the REPL prompt, allowing you to continue interacting with the variables and functions defined in the eval snippet.
Importing Modules in the REPL
The REPL supports dynamic imports, enabling you to load external modules during your session:
> import { serve } from "https://deno.land/std@0.203.0/http/server.ts";
> serve(() => new Response("Hello"));
This creates a server directly from the REPL without creating a local file, demonstrating the full power of Deno's permissionless module loading in interactive mode.
Exiting the Session
To exit the Deno REPL, use one of the following methods:
- Press Ctrl+D (EOF signal)
- Type
.exit(if the exit command is enabled in your version) - Press Ctrl+C twice (interrupt signal)
IDE Integration via LSP
When using the Deno REPL through an integrated development environment like VS Code, the Language Server Protocol (LSP) provides enhanced functionality. The LSP server loads cli/lsp/repl.rs, which forwards LSP-specific diagnostics, completions, and type information to the same REPL worker used in CLI mode.
This integration ensures that editor features like autocomplete and error highlighting work seamlessly within REPL sessions launched from your IDE.
Summary
- Launch the REPL by running
denowithout arguments; the CLI routes tocli/tools/repl/mod.rswhen no script is specified. - Full permissions are automatically granted in REPL mode via
CliMainWorkerFactoryto enable unrestricted experimentation. - History persistence defaults to
$DENO_DIR/repl_history.txtand can be customized via theDENO_REPL_HISTORYenvironment variable. - Advanced features include
--evalfor startup code execution and dynamicimport()support for loading remote modules. - LSP integration in
cli/lsp/repl.rsprovides IDE support for interactive sessions.
Frequently Asked Questions
How do I start the Deno REPL with specific permissions?
You do not need to specify permissions when starting the Deno REPL. According to the source code in cli/tools/repl/mod.rs, the REPL automatically runs with --allow-all permissions enabled through the CliMainWorkerFactory. This design choice removes friction during interactive development by granting full access to the file system, network, and environment variables.
Where is the Deno REPL history stored?
By default, the REPL history is stored at $DENO_DIR/repl_history.txt, as implemented in libs/resolver/cache/deno_dir.rs. The $DENO_DIR variable resolves to platform-specific cache directories such as ~/.cache/deno on Linux or ~/Library/Caches/deno on macOS. You can override this location or disable history entirely by setting the DENO_REPL_HISTORY environment variable.
Can I import external modules in the Deno REPL?
Yes, the Deno REPL supports dynamic module imports using standard ES module syntax. You can use import statements directly at the prompt to load local files or remote URLs, such as import { serve } from "https://deno.land/std@0.203.0/http/server.ts";. This functionality works because the REPL worker runs with full permissions and has access to Deno's module resolution system.
How does the Deno REPL handle TypeScript?
The Deno REPL executes TypeScript code directly without requiring separate compilation steps. When you enter TypeScript syntax at the REPL prompt, the Deno.core.evalContext function (running inside the worker created by CliMainWorkerFactory) compiles and executes the code immediately. This provides a seamless TypeScript development experience in the interactive shell, identical to running .ts files directly with Deno.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →