Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Node.JS - Get Started
  • What is Node.js
  • Node.js Process Model
  • Install Node.js
  • Node.js Console/REPL
  • Node.js Basics
  • Node.js Modules
  • Local Modules
  • Export Module
  • Node Package Manager
  • Node.js Web Server
  • Node.js File System
  • Debugging Node.js
  • Node Inspector
  • Node.js EventEmitter
  • Frameworks for Node.js
  • Express.js
  • Express.js Web App
  • Serving Static Resources
  • Node.js Data Access
  • Access SQL Server
  • Access MongoDB
  • Template Engines for Node.js
  • Jade Template Engine
  • Vash Template Engine
  • GruntJS
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Node.js Console/REPL

Node.js comes with virtual environment called REPL (aka Node shell). REPL stands for Read-Eval-Print-Loop. It is a quick and easy way to test simple Node.js/JavaScript code.

To launch the REPL (Node shell), open command prompt (in Windows) or terminal (in Mac or UNIX/Linux) and type node as shown below. It will change the prompt to > in Windows and MAC.

Launch Node.js REPL

You can now test pretty much any Node.js/JavaScript expression in REPL. 10 + 20 will display 30 immediately in new line.

The + operator also concatenates strings as in browser's JavaScript.

Node.js Examples

You can also define variables and perform some operation on them.

Define Variables on REPL

If you need to write multi line JavaScript expression or function then just press Enter whenever you want to write something in the next line as a continuation of your code. The REPL terminal will display three dots (...), it means you can continue on next line. Write .break to get out of continuity mode.

For example, you can define a function and execute it as shown below.

Node.js Example in REPL

You can execute an external JavaScript file by executing the node fileName command. For example, the following runs mynodejs-app.js on the command prompt/terminal and displays the result.

mynodejs-app.js
console.log("Hello World");

Now, you can execute mynodejs-app from command prompt as shown below.

Run External JavaScript file

To exit from the REPL terminal, press Ctrl + C twice or write .exit and press Enter.

Quit from REPL

Thus, you can execute any Node.js/JavaScript code in the node shell (REPL). This will give you a result which is similar to the one you will get in the console of Google Chrome browser.

The following table lists important REPL commands.

REPL CommandDescription
.helpDisplay help on all the commands
tab KeysDisplay the list of all commands.
Up/Down KeysSee previous commands applied in REPL.
.save filenameSave current Node REPL session to a file.
.load filenameLoad the specified file in the current Node REPL session.
ctrl + cTerminate the current command.
ctrl + c (twice)Exit from the REPL.
ctrl + dExit from the REPL.
.breakExit from multiline expression.
.clearExit from multiline expression.
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.