DEV Community

Stefan Judis
Stefan Judis

Posted on • Originally published at stefanjudis.com on

1 2

TIL: How to create your custom Node.js REPL

Today I came across a quick video which explains functionality about Node.js and its REPL (Read-Eval-Print loop).

While I use the built-in REPL from time to time (type node into your terminal to start it) I haven't used the REPL module before. It turns out that you can create your custom REPL with just a few lines of JavaScript:

// index.js
const repl = require('repl');

const state = {
  printSomething() {
    console.log("That's awesome!");
  }
};

const myRepl = repl.start("stefan's repl > ");

Object.assign(myRepl.context, state);
Enter fullscreen mode Exit fullscreen mode

If you're like me and like to prototype in a console, this can become very handy. You could create an entry script for your application that provides all the initialized objects and functionality.

Screenshot of a terminal: start a new REPL by running

By providing your own REPL that includes all the needed state you can "just REPL away" without starting a debugger and attaching breakpoints. 🎉

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Oldest comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay