DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 6 of NodeJS||REPL Part 2

Hey reader👋Hope you are doing well😊
In the last post we have discussed about REPL in NodeJS. In this post we are going to discuss more about REPL in NodeJS.
So let's get started🔥

Dot Commands in REPL

The REPL has some special commands, all starting with a dot .. They are-:

  • .help-: shows the dot commands help.
    Image description

  • .editor-: enables editor mode, to write multiline JavaScript code with ease. Once you are in this mode, enter ctrl-D to run the code you wrote.
    Image description
    The REPL knows when you are typing a multi-line statement without the need to invoke .editor.
    Image description
    The three dots ... specifies the multiline code.

  • .break-: when inputting a multi-line expression, entering the .break command will abort further input. Same as pressing ctrl-C.

  • .clear -: resets the REPL context to an empty object and clears any multi-line expression currently being input.

  • .load -: loads a JavaScript file, relative to the current working directory.
    This is useful to load the existing code from a file for experimenting without re-writing the whole code again.
    Image description

  • .save -: saves all you entered in the REPL session to a file (specify the filename).
    Image description

  • .exit -: exits the repl (same as pressing ctrl-C two times).
    Image description

Run REPL from JS file

We can even run repl in JS file. Follow the below code-:
Image description
To use import inplace of require save your file with .mjs extension (if you have esm installed as dependency) or add `"type":"module" in package.json.

Using modules in REPL

We can even use Js modules in the Node.Js REPL. There are a couple of modules in the Node.Js REPL by default. You can get the list by pressing the TAB key twice.
Image description

Image description

Why we use REPL?

  • Immediate Feedback: The REPL provides instant feedback by immediately executing the code entered and displaying the result. This helps in quickly testing and debugging code snippets.

  • Experimentation: Developers can experiment with different JavaScript expressions, functions, and modules in an interactive environment without needing to create and run a full script.

  • Learning and Prototyping: For beginners, the REPL is a valuable tool to learn JavaScript and Node.js. It also helps in prototyping small pieces of code before integrating them into a larger application.

  • Debugging: Developers can use the REPL to troubleshoot and debug issues in their code by testing specific functions and modules interactively.

So this was everything about REPL I hope you have understood it well. In the next blog we are going to read about File System in NodeJs. Till then stay connected and don't forget to follow me.
Thankyou 🤍

Top comments (0)