Execute function in script from command line (Node JS)

I am writing a web application in Node. If I have a db.js JS file with an init function in it, how can I call this function from the command line?

+135
javascript command-line
Jun 11 '15 at 13:48
source share
10 answers

Don’t comment on why you want to do this, or what might be more standard practice: here is the solution to your question ... Keep in mind that the type of quotes required by your command line may vary.

In your db.js export the init function. There are many ways, but for example:

 module.exports.init = function () { console.log('hi'); }; 

Then name it like this, assuming your db.js is in the same directory as your command line:

 node -e 'require("./db").init()' 

For other readers, the init OP function could be called as you like, it doesn't matter, it's just the specific name used in the question.

+212
Apr 07 '16 at 15:36
source share
β€” -

For other answers, add the following to someFile.js

 module.exports.someFunction = function () { console.log('hi'); }; 

Then you can add the following to package.json

 "scripts": { "myScript": "node -e 'require(\"./someFile\").someFunction()'" } 

From the terminal you can call

 npm run myScript 

I find it much easier to remember commands and use them.

+27
Dec 24 '17 at 17:05
source share

Try make-runnable .

In db.js add require('make-runnable'); to the end.

Now you can do:

 node db.js init 

Any additional arguments are passed to the init method.

+21
Jun 15 '16 at 4:31 on
source share

Install run-func in your project

 npm i -D run-func 

Run any exported function.

 run-func db.js init 

All subsequent arguments will be passed as parameters to the init(param1, param2) function init(param1, param2)

 run-func db.js init param1 param2 

This can also be run from the scripts section in package.json

 "db-init": "run-func db.js init" 

It is important that the function ( init in this example) must be exported to a file containing it

 module.exports = { init }; 

or ES6 export

 export { init }; 
+8
Apr 24 '17 at 21:30
source share

simple way:

let's say you have a db.js file in the helpers directory in the project structure.

now go to the helpers directory and go to the node console

  helpers $ node 

2) db.js file is required

 > var db = require("./db") 

3) call your function (in your case, its init ())

 > db.init() 

hope this helps

+6
May 29 '17 at 18:32
source share

If you turn db.js into a module, you can require it from db_init.js and simply: node db_init.js .

db.js:

 module.exports = { method1: function () { ... }, method2: function () { ... } } 

db_init.js:

 var db = require('./db'); db.method1(); db.method2(); 
+4
Jun 11 '15 at 13:56 on
source share

This one is dirty, but it works :)

I will call the main() function from my script. Previously, I simply put main calls at the end of the script. However, I added some other functions and exported them from the script (to use functions in some other parts of the code) - but I do not want to execute the main () function every time I import other functions into other scripts.

So, I did this, in my script I deleted the call to main (), and instead I put this check at the end of the script:

 if (process.argv.includes('main')) { main(); } 

So when I want to call this function in the CLI: node src/myScript.js main

+3
Jan 31 '19 at 9:48
source share

I am doing IIFE, something like this:

 (() => init())(); 

this code will be executed immediately and will call the initialization function.

+1
Feb 27 '19 at 21:49
source share

If your file just contains your function, for example:

myFile.js:

 function myMethod(someVariable) { console.log(someVariable) } 

Calling from the command line like this, nothing will happen:

 node myFile.js 

But if you change your file:

myFile.js:

 myMethod("Hello World"); function myMethod(someVariable) { console.log(someVariable) } 

Now this will work from the command line:

 node myFile.js 
-3
Dec 16 '17 at 14:28
source share

Simple, in the javascript file testfile.js:

 module.exports.test = function () { console.log('hi'); }; this.test(); 

Runs on the command line:

 node testfile.js 
-3
Dec 26 '17 at 11:25
source share



All Articles