4

The following code is from the Node.js course I am following:

var args = require("minimist")(process.argv.slice(2), { string: "name"});

I understand that a module is being imported, but I don't understand the second set of parentheses after the require() call:

require("minimist")(this part I don't understand)

Specifically, what is the second set of parentheses in terms of syntax?

I know how slice() works, and I understand that string: "name" creates a command line argument to check for, but what method is being called through the require() call, and how?

PS: The course (by Kyle Simpson) indicates that the above syntax will be explained later, but I haven't been able to locate the specific part, and I don't like to proceed without understanding something. I am new to both JS and Node.js.

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • https://stackoverflow.com/questions/22213980/could-someone-explain-what-process-argv-means-in-node-js-please – ptts Oct 20 '18 at 19:09
  • @ptts That link covers argv. I know how argv is passed to the process. My question concerns the syntax as I have explained in detail in my question (at least I thought I made myself clear). – Sabuncu Oct 20 '18 at 19:11

3 Answers3

4

In this case it looks like the module you are requiring is simply returning a function which you are immediately calling with () and passing in two arguments: process.argv.slice(2) and { string: "name"}

So if your module looked like this:

// minimist.js
function test(str){
    console.log(str)
    return "Called with:" + str
}
module.exports = test;  // exports the function

you could use it like:

var arg = require('./minimist.js')("Hello") // calls the function minimist.js exported
Mark
  • 90,562
  • 7
  • 108
  • 148
  • 1
    OK, I understand your example. Will accept as soon as SO lets me. Thank you. Didn't know about module.exports, so thanks for that as well. – Sabuncu Oct 20 '18 at 19:16
1

Try to name each part to figure it out: Let's put it this way:

var args = tmp(v1, v2);

As you can see, that is clearly a function calling. Right?

And now, imagine that:

var tmp=require("minimist");
var v1=process.argv.slice(2);
var v2={ string: "name"};

Now you got it: tmp -namely the value returned by slice()- must be necessarily a function that accepts two arguments.

Little Santi
  • 8,563
  • 2
  • 18
  • 46
1

minimist is a package to parse command line arguments, and it exports a function.

require('minimist') will return a function like function(args, opts={}). Then you pass arguments to this function.

First argument - process.argv.slice(2)

process.argv is an array of parts of your command. For example if you type in your terminal

node app.js --test -x 1 --name '123'

Your process.argv will be an array

['node', 'app.js', '--test', '-x', 1, '--name', '123']

Here ['node', 'app.js'] is obvious you have to call. So to get your options you have to use .slice(2), and it will be

['--test', '-x', 1, '--name', '123']

Now go to the next...

Second argument - { string: "name" }

From minimist documentation you can find the following

opts.string - a string or array of strings argument names to always treat as strings

It means your 'name' argument will be accepted as string. From example above:

name = '123' // not 123

Hope it's clear now for you. Comment if anything is not.

Rashad Ibrahimov
  • 3,279
  • 2
  • 18
  • 39