The REPL
The REPL is the interactive mode of Node.js. The name stands for Read, Eval, Print, Loop: read a line, evaluate it, print the result and wait for the next one. It is used to quickly try an expression or a function without creating a file.
Starting it
Type node without a file name:
nodeA > prompt appears. Type expressions — the result is printed right away:
> 2 + 3
5
> 'blog'.toUpperCase()
'BLOG'
> [1, 2, 3].map((n) => n * 2)
[ 2, 4, 6 ]There is no need for console.log(): the REPL shows the value of each expression itself.
Variables and the last result
Variables live until you leave the REPL. The special variable _ holds the result of the previous expression:
> const posts = ['First post', 'Second post']
undefined
> posts.length
2
> _ * 10
20undefined after a declaration is normal: the declaration itself returns nothing.
Built-in modules
Built-in modules are already loaded in the REPL — use them by name without import:
> path.extname('post.json')
'.json'
> os.platform()
'win32'Top-level await works too: type await fs.promises.readdir('.') to see the files in the current folder.
REPL commands
Commands start with a dot:
| Command | What it does |
|---|---|
.help | Lists the commands |
.editor | Multi-line input, finished with Ctrl+D |
.load app.js | Runs a file in the current session |
.exit | Exits |
You can also exit by pressing Ctrl+C twice. The Tab key completes names: type posts. and press Tab twice to see all array methods.
Common mistake
node app.js must be typed in the ordinary terminal, not inside the REPL. Typed after the > prompt, it is treated as JavaScript:
> node app.js
Uncaught ReferenceError: node is not definedLeave the REPL with .exit and run the file again.