The REPL & Quick Experiments
The Node.js REPL is an interactive shell for probing APIs, inspecting objects, and validating assumptions before you commit code to a file.
Search across all documentation pages
The Node.js REPL is an interactive shell for probing APIs, inspecting objects, and validating assumptions before you commit code to a file.
Quick-reference recipe card - copy-paste ready.
node # start REPL
node --env-file=.env # preload env vars into REPL context
node -e "console.log(process.version)"
node --input-type=module -e "import { ok } from 'node:assert'; ok(1)"When to reach for this:
fetch response before wiring it into a serviceprocess or import.meta behave# Start REPL with env file (Node 20+)
node --env-file=.env.development> const { readFile } = await import('node:fs/promises')
> const raw = await readFile('package.json', 'utf8')
> const pkg = JSON.parse(raw)
> pkg.engines
{ node: '>=24.18.0 <25' }
> // Inspect built-in fetch headers
> const res = await fetch('https://httpbin.org/headers')
> [...res.headers.entries()].slice(0, 3)
[
[ 'content-type', 'application/json' ],
[ 'content-length', '...' ],
[ 'date', '...' ]
]
> .save repl-session.js
> .exitWhat this demonstrates:
await works in the REPL without wrapping in an async functionimport() loads ESM built-ins inside the REPL session.save writes every evaluated expression to a file for later cleanup--env-file mirrors production config loading during experimentsglobal), and prints expression results._ holds the last evaluated result; _error holds the last thrown error (Node REPL convention)..help, .break, .clear) are meta-commands and not sent to V8.| Command | Action |
|---|---|
.help | List all dot-commands |
.save file | Write session history to disk |
.load file | Execute a file in the current context |
.break | Abort multi-line input |
.editor | Open multi-line editor mode |
# Run TypeScript in REPL-like one-liners with tsx
npx tsx -e "const x: number = 42; console.log(x)"
# Or start tsx REPL for typed experiments
npx tsxFor typed exploration, tsx REPL is preferable to plain node when you need interfaces and generics.
.save output directly - saved sessions include > prompts and may lack imports. Fix: refactor into a proper module and add tests..node_repl_history. Fix: .clear sensitive lines; never .save sessions with secrets.require is not defined in ESM-first projects. Fix: use await import() or start with --input-type=module.console.log(hugeArray) blocks the terminal. Fix: use util.inspect(obj, { depth: 2, maxArrayLength: 10 }).node:test cases.| Alternative | Use When | Don't Use When |
|---|---|---|
node -e / tsx -e | Single-expression checks in CI or scripts | Multi-step exploration |
Scratch .mjs file + --watch | Repeatable experiments you will commit | Throwaway 10-second checks |
node:test + watch mode | Experiments that should become regression tests | Pure object inspection |
Debugger (node inspect) | Stepping through existing code | Prototyping new APIs from scratch |
Yes in Node 24. You can await fetch(...) directly at the prompt without an async wrapper.
Start with node --env-file=.env (Node 20+). Variables appear on process.env immediately.
The built-in REPL is JavaScript. Use npx tsx for a TypeScript-aware REPL.
-p evaluates and prints the expression (implies -e). node -p "2+2" outputs 4.
Press .break or Ctrl+C twice. .break aborts without killing the REPL session.
~/.node_repl_history by default. Set NODE_REPL_HISTORY to change the path.
In a project with "type": "module", use await import('pkg') instead of require.
After a thrown error, _error references it. console.error(_error) prints the stack.
No. Never open REPL on production servers - use read-only logs, metrics, and local reproduction instead.
node --input-type=module
> import.meta.url
'file:///...'Yes via NODE_REPL_HISTORY, custom repl servers, or programmatic repl.start({ prompt: 'api> ' }).
Copy the validated logic into a node:test file and replace manual console.log assertions with assert.equal.
node commandsStack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.
Reviewed by Chris St. John·Last updated Jul 16, 2026