Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

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:

node

A > 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
20

undefined 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:

CommandWhat it does
.helpLists the commands
.editorMulti-line input, finished with Ctrl+D
.load app.jsRuns a file in the current session
.exitExits

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 defined

Leave the REPL with .exit and run the file again.

Thanks for reading!

Donate to the site — we work hard for you.