I want to write a Node.js source file (myCode.js
) in which there is a function say foo(arg1, arg2,..)
, such that I can ..
$ node ./myCode.js arg1 arg2
as well as
// myOtherCode.js
var foo = require('./mycode')
Is it possible? Currently I have a small bunch of files like these and they all are coded like
return (
function foo() {
//...
}
)()
which I call from the node command line like
$ node ./myCode.js arg1 agr2
But now I want to string them together in a single javascript that will be called from commandline with 'node' or 'npm script' (so all functionality from different files will need to be 'require()d'). But I also want to retain the ability to run them individually if it need to be.
I understand that when called from shell prompt, the arg1, arg2 will become argv[2], argv[3] and so on, and when 'require()'d, they would be function arguments.
This probably also means that I need to pass arguments to 'require()'? How can I accomplish this?
[Using Node.js 8.x [LTS] version, so cannot use export/import syntax]
Yes it's possible . you have to use process.argv to retreive the passed parameters
for example if we need to console the string "hello" we have to create a file "fileName.js" that contain this code :
and then execute this in the terminal
It's conventional to have different entry points for command-line and programmatic package use. A usual recipe for parsing CLI arguments in Node.js is
process.argv.slice(2)
, sincenode
and script name are the first 2 arguments.For package-level executable script,
foo
function is exported from index.js entry point:Considering that
foo
expects string arguments, bin/index.js CLI executable script can be as concise as:Both can be specified in package.json:
There is no way to pass them to
require
. If a script needs to accept arguments programmatically, this means that it should be wrapped with factory function that accepts arguments.A quick-and-dirty fix is to pass arguments to CLI executable script through
process.argv
. For instance, a helper function that allows to invoke executable scripts with dynamic arguments:Which can be used like:
Notice that due to how
decache
works, it decaches only the module that was loaded. If submodules make use ofprocess.argv
, this should be handled accordingly.This shouldn't be done in production because the approach is hacky but it's perfectly suitable for testing.
A natural limitation is that arguments that are passed from CLI are strings, so a function should contain boilerplate code to convert them to expected data types (numbers, booleans, etc). For this reason it's preferable to separate programmatic and CLI entry points.
If there's a necessity to call a lot of scripts like that, this may be actual problem, and the solution is to create single CLI executable script that processes arguments with a library like
yargs
and calls functions from other files based on that.