REPL stands for Read-Evaluate-Print-Loop, and it's a great way to explore the Node features in a quick way.
The node command is the one we use to run our Node.js scripts:

node script.jscode-box
If we omit the filename, we use it in REPL mode:
If you try it now in your terminal, this is what happens:
❯ node

the command stays in idle mode and waits for us to enter something.
Tip: if you are unsure how to open your terminal, google "How to open terminal on ".alert-info
The REPL is waiting for us to enter some JavaScript code, to be more precise
Start simple and enter
> console.log('test')

        The first value, test , is the output we told the console to print, then we get undefined which is the return value of running console.log(). We can now enter a new line of JavaScript.

Use the tab to autocomplete

    The cool thing about the REPL is that it's interactive. As you write your code, if you press the tab key the REPL will try to autocomplete what you wrote to match a variable you already defined or a predefined one.

Exploring JavaScript objects

        Try entering the name of a JavaScript class, like Number , add a dot and press tab . The REPL will print all the properties and methods you can access on that class:

Explore global objects

        You can inspect the globals you have access to by typing global. and pressing tab :

The _ special variable 

If after some code you type _ , that is going to print the result of the last operation.

Dot commands 

The REPL has some special commands, all starting with a dot . . They are 
  • .help : shows the dot commands help
  • .editor : enables editor more, to write multiline JavaScript code with ease. Once you are in this mode, enter ctrl-D to run the code you wrote.
  • .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 
  • .save : saves all you entered in the REPL session to a file (specify the filename)
  • .exit : exists the repl (same as pressing ctrl-C two times) 
The REPL knows when you are typing a multi-line statement without the need to invoke .editor
For example if you start typing an iteration like this: 
[1, 2, 3].forEach(num => {code-box
and you press enter , the REPL will go to a new line that starts with 3 dots, indicating you can now continue to work on that block.
... console.log(num)
... })code-box

If you type .break t the end of a line, the multiline mode will stop and the statement will not be executed.

Pass arguments from the command line

How to accept arguments in a Node.js program passed from the command line 

You can pass any number of arguments when invoking a Node.js application using:
node app.jscode-box
Arguments can be standalone or have a key and a value. For example :
node app.js akucodecode-box
ornode app.js name=akucodecode-box
        This changes how you will retrieve this value in the Node code. The way you retrieve it is using the process object built into Node. It exposes an argv property, which is an array that contains all the command line invocation arguments. The first argument is the full path of the node command. The second element is the full path of the file being executed. All the additional arguments are present from the third position going forward. You can iterate over all the arguments (including the node path and the file path) using a loop:
process.argv.forEach((val, index) => {

        You can get only the additional arguments by creating a new array that excludes the first 2 params:
const args = process.argv.slice(2)code-box
        If you have one argument without an index name, like this:
node app.js akucodecode-box
        You can access it using :
const args = process.argv.slice(2)
In this case:
node app.js name=akucodecode-box
     args[0]is name=akucode , and you need to parse it. The best way to do so is by using the minimist library, which helps to deal with arguments:
const args = require('minimist')(process.argv.slice(2))
args['name'] //akucodecode-box


This is the first post Next Post

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel