Loading lessons...
Node.js REPL & Executing Scripts
Using the REPL and Running Scripts
REPL stands for Read-Eval-Print-Loop. It provides an interactive command-line environment for testing JavaScript snippets and Node.js APIs in real time.
Starting the REPL
Launch the REPL by typing node in your terminal:
$ node
Welcome to Node.js v20.10.0.
Type ".help" for more information.
> 5 + 10
15
> const name = "Express";
undefined
> `Hello ${name}`
'Hello Express'
> .exit
Useful REPL Commands
.help: List all dot commands..break/.clear: Reset multiline input context..exit: Exit the REPL session (or press Ctrl+C twice)._(Underscore) : Stores the result of the last evaluated expression.
Running JavaScript Files
Execute scripts by passing the file path to the node command:
$ node server.js --env=production
// server.js
console.log("Command Line Args:", process.argv);
// process.argv[0]: Path to node executable
// process.argv[1]: Path to executed script
// process.argv[2...]: User arguments passed
TL;DR
- REPL = Read-Eval-Print-Loop for interactive JavaScript testing.
- Use
node script.jsto run JS files. process.argvcontains array of command-line arguments.