Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of REPL in node js

  1. What is the use of REPL in nodejs?
  2. What are the use-cases/scenario for using REPL?
  3. When should I use the REPL node module in nodejs?

I have understood the API Doc, how to include REPL module and how to start the REPL instance using the prompt and eval.

But can anyone help me to understood above question? So that I can understand how exactly the REPL module can be used?

like image 867
Soorya Prakash Avatar asked Jan 20 '18 11:01

Soorya Prakash


People also ask

What is the purpose of REPL?

A Read-Eval-Print Loop, or REPL, is a computer environment where user inputs are read and evaluated, and then the results are returned to the user. REPLs provide an interactive environment to explore tools available in specific environments or programming languages.

How do you execute a REPL?

Starting the REPL is simple - just run node on the command line without a filename. It then drops you into a simple prompt ('>') where you can type any JavaScript command you wish. As in most shells, you can press the up and down arrow keys to scroll through your command history and modify previous commands.

Which command is used to start REPL session?

REPL stands for "Read Eval Print Loop". 3) Which of the following command is used to start a REPL session? Answer: A is the correct option. We can start REPL simply by running node on shell/console without any argument.


1 Answers

What are the use-cases/scenario for using REPL?

I've often seen it (or Chrome's console which is a JavaScript REPL as well) used in tech talks as a quick way to demonstrate what different expressions evaluate to. Let's image you're holding a talk about Equality in JavaScript and want to show that NaN strangely is not equal to itself.

You could demonstrate that by:

  • running node without arguments in your terminal (this starts the REPL)
  • entering NaN == NaN and pressing Enter (the REPL will evaluate the expression)
  • pretending to be surprised that the output is false

When should I use the REPL node module in nodejs?

When you want to implement a Node.js REPL as part of your application, for example by exposing it through a "remote terminal" in a web browser (not recommending that because of security reasons).

Examples

Replicating the REPL that is shown by calling node

const repl = require('repl')
const server = repl.start()

Using the REPLServer's stream API

fs-repl.js file:

const repl = require('repl')
const fs = require('fs')
const { Readable } = require('stream')

const input = new fs.createReadStream('./input.js')

const server = repl.start({
  input,
  output: process.stdout
})

input.js file:

40 + 2
NaN
["hello", "world"].join(" ")

You can now run node fs-repl and you will get the following output:

> 40 + 2
42
> NaN
NaN
> ["hello", "world"].join(" ")
'hello world'

This output can obviously be passed into a Writable stream other than process.stdout by changing the output option.

like image 95
Niklas Higi Avatar answered Oct 01 '22 00:10

Niklas Higi