Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
How to use Superheroes Module in Node.js ?
Next article icon

How to use Node.js REPL ?

Last Updated : 15 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Node.Js REPL or Read-Evaluate-Print Loop is an interactive shell for the Node.js environment which means we can write any valid Javascript code in it. This is used to test, evaluate, experiment, or debug code much easier and accessible way. It basically acts as the Browser's Web dev tools' Console for Javascript.  

To use the REPL, you must have Node.js downloaded for your Operating System. To check if the Node.Js is installed correctly, you can use the following command:

node --version 

If you get a version number, you are good to go else, you need to fix your installation. So, we can now start to use the node.js REPL in your machine.

How to Start the REPL: To start the Node.js REPL is quite easy and straightforward, you simply have to enter the word node into the Terminal/CMD/PowerShell as per your OS.

node

You can use any valid Javascript code in the prompt. We do not need to use console.log to print the value of the variables, simply the name of the variables might be sufficient in most cases. 

As we can see the prompt output is a bit more than plain text, it's nice colored and even has autocompletion built-in. This makes the REPL more convenient and quick to test up some ideas before actually using them in the project.

Exit from the REPL: To exit from the reply, you can press CTRL + D in Windows/Linux and CMD+D in macOS. Optionally, CTRL+C twice will also work to exit. Alternately we can also use the following to exit out of the REPL :

.exit

Using Javascript in REPL: We can use any valid javascript in the REPL. We can use variables, strings, concatenation, arithmetic, and all of the stuff that can be feasible in the REPL. There are limitations to what we can write in the REPL, like a bit longer and functional programs. This issue will be seen in the next section of REPL Commands. 

As we can see we have used a couple of concepts in Javascript like string interpolation, arithmetic, and working with arrays. Any valid and feasible Javascript can be used in the REPL and hence some core features of Javascript can be utilized in it.

REPL Commands: There are a couple of commands and arguments to be used in the Node.Js REPL shell. We'll explore some of them in this section. These commands are to be used in the REPL shell i.e after entering the command node into your terminal/CMD/PowerShell. These commands or characters are reserved in the REPL and hence provide some great features and enhance accessibility. 

Editor command: This command is used to stop the line-by-line evaluation and make an editor-like typing in the shell. It's nothing like an editor but simply writing more longer and meaningful code as a form of a program.



  1.  


  1.  

As we can see we can write more than one line in the shell which makes writing more sophisticated code with a lot of freedom being in the terminal. After writing the required code, we can save and evaluate the code by pressing CTRL + D or we can cancel the evaluation and hence abort the process by pressing CTRL + C. 

Save command: We can use the .save command to save the code of the current REPL session in a file. This might be really handy if you exit the REPL, all the code snippets will be lost and with this command, it becomes much easier to keep a backup at the user's disposal.



  1.  


  1.  

As we can see the code snippet from the REPL is saved into a file.  The file in most cases would be a Js file quite obviously. The .save command is used along with the filename to store the contents of the REPL.

Load  command: The load command as opposed to the .save command loads the variables, functions, and other scopes of a file into the REPL. This is useful to load the existing code from a file for experimenting without re-writing the whole code again.



  1.  


  1.  

As we can see, we loaded the file from the previous example, and it crammed it in a single block of code instead of rendering it line by line. We can extend the code as we want and again save it to the file if we want. This makes experimenting much easier and quicker avoiding repetitive writing of code and loading the Js code from a file makes it super useful as well.

Clear command: The .clear command or .break command is used to break from the existing loop statements or multi-line inputs. 



  1.  


  1.  

We can see from the above example that after entering the .clear or .break command a new prompt appears and breaks out of the current input or statement These commands do not execute the code and return to the main prompt.

Exit command: As said earlier the alternative to CTRL + D or CTRL + C (twice) is the command .exit. It basically exits the REPL. 



  1.  

Help command: The help command as stated in the REPL header gives more information about the options and commands available in the Node.Js REPL. 



  1.  

Underscore Variable: The underscore Variable (_) will give us the result of the last executed command or code. It can be a variable value, function return value, or anything which can return some kind of value, if there is nothing from the evaluation the REPL will default it to undefined.

As we can see in the example, the _ variable gets the result of the last executed command in the shell. It can be undefined if there was just the declaration of a variable else it stores the result or return value of the executed command.

Using Modules: 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. 

If you want to import other modules, you need to follow the following procedure: You need to firstly install the package via the npm package manager for Node.Js.

npm install package_name

After installing the module in the same directory, we can use the "require" command to get the module's core functionalities. 

As we can see the command:

const express = require('express')

Here, express can be other modules as well. We even use the functions in the modules after writing the boilerplate code for the packages in the REPL. Even we can use a template as a file and load that into a REPL. This makes testing some regularly used modules very easy and quick.


Next Article
How to use Superheroes Module in Node.js ?

M

meetgor
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • NodeJS-Questions

Similar Reads

  • How to Send JSON Response using Node.js ?
    NodeJS is the runtime environment, which can execute the javascript code on any platform. It is widely used to create and run web application servers because of its salient features. During production, several times we need to send the resources or some type of information as a response, and javascr
    5 min read
  • How to use Superheroes Module in Node.js ?
    The superheroes module is a fun and simple Node.js package that allows you to generate random superhero names. It's a great example of how to use third-party modules in Node.js and can be useful for testing, generating sample data, or just adding some flair to your applications. In this article, we'
    2 min read
  • How to Setup View Engine in Node.js ?
    View engines are useful for rendering web pages. There are many view engines available in the market like Mustache, Handlebars, EJS, etc but the most popular among them is EJS which simply stands for Embedded JavaScript. It is a simple templating language/engine that lets its user generate HTML with
    2 min read
  • How to Take Input in Node.js ?
    Taking input in a Node.js application is essential for building interactive command-line interfaces, processing user input, and creating dynamic applications. Node.js provides several methods for receiving input from users, including reading from standard input (stdin), command-line arguments, and u
    2 min read
  • What is NPM & How to use it ?
    NPM, short for Node Package Manager, is the default package manager for NodeJS. It is a command-line utility that allows you to install, manage, and share packages or modules of JavaScript code. These packages can range from small utility libraries to large frameworks, and they can be easily integra
    3 min read
  • How to Run Node Server?
    A Node server runs JavaScript outside the browser to handle web requests. It listens for incoming requests, processes them, and sends responses. Unlike traditional servers, it handles multiple requests at once without waiting for each to finish. Some of the key features of the Node Server are: Non-B
    3 min read
  • How to Build a Simple Web Server with Node.js ?
    Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and it’s not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make
    3 min read
  • How to refresh a file in Node.js ?
    Node.js has seen an important growth in past years and is still increasing its value in many organizations and business models. Companies like Walmart or PayPal have already started to adopt it. NPM, the package manager of Node.js has been already installed when you install Node.js and is ready to r
    2 min read
  • Node.js Projects
    Node.js is one of the most popular JavaScript runtime environments widely used in the software industry for projects in different domains like web applications, real-time chat applications, RESTful APIs, microservices, and more due to its high performance, scalability, non-blocking I/O, and many oth
    9 min read
  • How to Install Node.js on Windows
    Installing Node.js on Windows is a straightforward process, but it's essential to follow the right steps to ensure smooth setup and proper functioning of Node Package Manager (NPM), which is crucial for managing dependencies and packages. This guide will walk you through the official site, NVM, Wind
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences