Jack Garay Jack Garay - 5 months ago 16x
Node.js Question

NodeJS Two Require Parameters

This isn't really a code-heavy question since it's more of a concept-type.

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

How does the code above work? I understand that I am including the minimist library in from the NPM but I don't quite understand why there's (process.argv.slice(2)). There are two open close parentheses over them.

I don't know how this process is called in Javascript. Is there any name for this form of usage ('minimist')(process.argv.slice....)?


Your code is equivalent to:

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

This means, the second parenthesis of your code is actually calling minimist (or rather the function exported by the minimist module) with two arguments:

  1. process.argv.slice(2): all the arguments from the command line
  2. {string: "name"}: The options object

I'm not aware of any official name.