How to create your custom Node.js REPL (#tilPost)

Today I came across a quick video which explains Node.js’ REPL functionality (Read-Eval-Print loop).
While I use the built-in REPL from time to time (type node into your terminal to start it) to prototype code, I haven’t used the RE…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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

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

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

// define available methods and state
const state = {
  printSomething() {
    console.log("That's awesome!");
  }
};

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

Object.assign(myRepl.context, state);

If you're like me and like to prototype in a console, the repl package is handy. You could create an application entry script that provides all the initialized objects and functionality. Or you could even build a little shell for everyday tasks.

Screenshot of a terminal: start a new REPL by running "node index.js" and see the available command 'printSomething'

Let me know what REPL's you're building. I'd love to see more examples and real-world use cases. And now, let's "just REPL away"! ?


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2019-08-25T22:00:00+00:00) How to create your custom Node.js REPL (#tilPost). Retrieved from https://www.scien.cx/2019/08/25/how-to-create-your-custom-node-js-repl-tilpost/

MLA
" » How to create your custom Node.js REPL (#tilPost)." Stefan Judis | Sciencx - Sunday August 25, 2019, https://www.scien.cx/2019/08/25/how-to-create-your-custom-node-js-repl-tilpost/
HARVARD
Stefan Judis | Sciencx Sunday August 25, 2019 » How to create your custom Node.js REPL (#tilPost)., viewed ,<https://www.scien.cx/2019/08/25/how-to-create-your-custom-node-js-repl-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » How to create your custom Node.js REPL (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2019/08/25/how-to-create-your-custom-node-js-repl-tilpost/
CHICAGO
" » How to create your custom Node.js REPL (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2019/08/25/how-to-create-your-custom-node-js-repl-tilpost/
IEEE
" » How to create your custom Node.js REPL (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2019/08/25/how-to-create-your-custom-node-js-repl-tilpost/. [Accessed: ]
rf:citation
» How to create your custom Node.js REPL (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2019/08/25/how-to-create-your-custom-node-js-repl-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.