The Node.js REPL (Read-Eval-Print Loop) is an indispensable scratchpad for testing JavaScript syntax, inspecting core APIs, and experimenting with asynchronous code. In addition, mastering Node's native command-line interface (CLI) flags empowers you to run, watch, and debug applications like an industry professional.
To enter the REPL, simply type node in your terminal with no arguments:
$ node
Welcome to Node.js v22.11.0.
Type ".help" for more information.
> 2 + 5 * 10
52
> const os = require('os');
undefined
> os.totalmem() / (1024 * 1024 * 1024);
15.892765045166016 // Gigabytes of RAM on this machine
>
| Command / Shortcut | Function Description |
|---|---|
_ (Underscore) |
Stores the result of the previously evaluated expression (e.g., > Math.sqrt(16) followed by > _ * 2 produces 8). |
Tab (Twice) |
Auto-completes variable names or displays all methods on an object (e.g., fs. + Tab Tab). |
.help |
Lists all available dot-commands inside the REPL session. |
.break / .clear |
Exits multi-line expression entry mode. |
.editor |
Enters a multi-line code editor mode. Press Ctrl + D to execute the block. |
.exit (or Ctrl + C twice) |
Terminates the REPL session and returns to the operating system shell. |
Modern versions of Node.js include powerful built-in flags that eliminate the need for third-party utilities:
--watch (Native File Watcher)Replaces nodemon. Node automatically restarts the process whenever source files are modified:
node --watch server.js
--env-file=.env (Native Environment Variables)Replaces the dotenv package. Automatically loads key-value pairs from your .env file directly into process.env:
node --env-file=.env index.js
--inspect (Chrome DevTools Debugger)Opens a debugging WebSocket port (default 127.0.0.1:9229). Open Google Chrome and visit chrome://inspect to set visual breakpoints, step through code line-by-line, and inspect the memory heap:
node --inspect server.js
# Or break at the very first line before running:
node --inspect-brk server.js
--trace-warnings (Debugging Deprecations)Prints full stack traces for runtime warnings and deprecation notices, making it easy to identify the exact file and line causing an issue:
node --trace-warnings index.js
Boost your server-side JavaScript proficiency by studying these closely connected core modules:
Build basic scripts and HTTP listeners.
Integrate CLI scripts with npm run.
Use --inspect and memory profiling flags in production.