I'm writing a web app in Node. If I've got some JS file db.js
with a function init
in it how could I call that function from the command line?

- 7,845
- 5
- 28
- 39
-
use npm run-func https://stackoverflow.com/a/43598047/696535 – Pawel Nov 17 '17 at 16:16
-
5@Pawel I prefer the accepted answer because it doesn't require installing a third party dependency which may either lose support or contain vulnerabilities. It's a risk you run with any dependency, so limiting dependencies to well vetted and maintained ones is always a good idea. – winhowes Nov 24 '17 at 03:57
-
You could also use the package https://www.npmjs.com/package/invoke-script – Stefan van de Vooren Jan 05 '20 at 18:55
16 Answers
No comment on why you want to do this, or what might be a more standard practice: here is a 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 call it like this, assuming your db.js
is in the same directory as your command prompt:
node -e 'require("./db").init()'
If your db.js
were a module db.mjs
, use a dynamic import to load the module:
node -e 'import("./db.mjs").then( loadedModule => loadedModule.init() )'
To other readers, the OP's init
function could have been called anything, it is not important, it is just the specific name used in the question.

- 10,680
- 4
- 46
- 63
-
123This was a useful tip for testing some javascript that was running from AWS lambda - thanks – Alex Hinton Jun 03 '16 at 22:37
-
19
-
34In case anyone else is trying to do this in their package.json as a npm script, I tried it with the single quotes, and double quotes inside, but it didn't work until I switched them: "start": "node -e \"require('./server')()\"", – Sako73 Apr 27 '17 at 16:52
-
1
-
-
@winhowes Yes, init is just part of the the example posted - could be called anything, just an entry point to the script. – Lee Goddard Apr 30 '17 at 07:51
-
1@LeeGee yep - was more just helping @C.Lee debug - as they might not realize that `init` was a function in the script file – winhowes May 01 '17 at 08:08
-
2Thanks @winhowes for your reply, I just used your example `module.exports.init = function () { console.log('hi'); };` And `node -e 'require("./db").init()'` didn't work for me somehow. I am not sure what I did wrong, but by following your idea, I used `module.exports = myFunction, `, and then `node -e 'require("./myFunction")()'` worked for me. – C.Lee May 02 '17 at 05:15
-
3as an augmentation to @AlexHinton's comment, I now use the following to mimick an event and the callback: `node -e 'require("./index").handler(require("./fixtures/sample_event_01.json"), {}, console.log)'`. The middle `{}` would be the context, feel free to adjust. Also console.log is a bit primitive but a nice start. Of course you can also write a dedicate CLI.js that in turn require()'s the index.js/handler as stated in other comments. – Adrian Föder Jun 30 '17 at 12:00
-
1@LeeGee Here is a perfect example: I have a script I want to run. For my own reasons I decided I'd like to write it in javascript. Now I want to run it. I happen to have a particular application in mind which is what led me here. – abalter Jul 04 '17 at 20:01
-
@ToKra — Add to you `package.json`'s `scripts` node the following: `"example": "node -e 'require(\"./db\").init()' "` — YMMV with the escaped quotes! – Lee Goddard Feb 01 '19 at 11:59
-
node -p instead of node -e prints the result: https://nodejs.org/api/cli.html#cli_p_print_script – Damien Golding Mar 19 '19 at 06:40
-
-
-
I've used sth like this ``"node -e 'require(\"./handler\").findMostSuitableLinkStation()'"`` for testing before uploading to AWS lambda, but it didn't work!!!! – Chandara Chea Dec 07 '19 at 13:41
-
1@AugustinRiedinger, A bit late to the party, but if it's a promise it would go something like `node -e 'require("./db").init().then(x=>{console.log(x)})'` then you'll have to ctrl+c out of the script or do `node -e 'require("./db").init().then(x=>{console.log(x);process.exit()})'` – invertedSpear Dec 26 '19 at 21:56
-
-
1In windows, I can only make it by `node -e require('./db').init()` or `node -e "require('./db').init()"`. Pay attention to single quote and double quote. – newman Feb 12 '20 at 22:37
-
@newman In my windows, I failed with `node -e require('./db').init()` but `node -e "require('./db').init()"` works. And additionally `node -e 'require(\"./db\").init()'` also works. – doctorgu Sep 21 '20 at 08:53
-
``npx run-func /path/to/folder/file.js myfunction `` This worked for me. thanks. – Jay Teli Oct 13 '22 at 07:28
Update 2020 - CLI
As @mix3d pointed out you can just run a command where file.js
is your file and someFunction
is your function optionally followed by parameters separated with spaces
npx run-func file.js someFunction "just some parameter"
That's it.
file.js
called in the example above
const someFunction = (param) => console.log('Welcome, your param is', param)
// exporting is crucial
module.exports = { someFunction }
More detailed description
Run directly from CLI (global)
Install
npm i -g run-func
Usage i.e. run function "init", it must be exported, see the bottom
run-func db.js init
or
Run from package.json script (local)
Install
npm i -S run-func
Setup
"scripts": {
"init": "run-func db.js init"
}
Usage
npm run init
Params
Any following arguments will be passed as function parameters init(param1, param2)
run-func db.js init param1 param2
Important
the function (in this example init
) must be exported in the file containing it
module.exports = { init };
or ES6 export
export { init };

- 16,093
- 5
- 70
- 73
-
1I was thinking to use eye_mew' suggestion to use `make-runnable`, but this is a lot better than that, me thinks. Thanks. – luis.espinal Oct 16 '17 at 13:45
-
@luis.espinal I'm glad you find this useful. There's less magic in the background and no need to modify files. Unless a function is not exported then it has to be, but that makes logical sense just like regular ES6 modules and their import/export. – Pawel Oct 17 '17 at 09:50
-
1this doesn't work for me; $ run-func db.js init bash: run-func: command not found – Patlatus Jun 03 '19 at 12:07
-
@Patlatus to use directly from CLI without a script in package.json install it globally `npm i -g run-func` – Pawel Jun 03 '19 at 12:49
-
@Patlatus actually there was a bug that was only allowing this to run inside package.json scripts. I uploaded a new version that fixes it and it works globally too – Pawel Oct 30 '19 at 18:11
-
4Thanks to `npx`, we can do `npx run-func file.js functionName` without having to install run-func globally. #winning! – mix3d Aug 20 '20 at 18:31
-
@mix3d wow I haven't build this functionality intentionally but it works like that too ! – Pawel Sep 19 '20 at 14:40
-
4You should mention, that you are one of the contributors of package [`run-func`](https://github.com/DVLP/run-func)... – A_blop Dec 27 '20 at 12:52
-
-
-
-
@JerryGreen It seems like your exported files are in ES6 modules which must be in .mjs to work as modules or you can change the output target to ES5 in typescript and then it will work with .js files – Pawel Mar 09 '21 at 19:03
-
@Pawel nah, it just doesn't work with typescript. I tried to use ts-node, but it doesn't help – Jerry Green Mar 09 '21 at 19:04
-
@JerryGreen so you're trying to run a typescipt file in Node directly without transpiling? It's not going to work like that – Pawel Mar 09 '21 at 19:07
-
@Pawel I have webpack for that. But I don't have js sources before I start my application, so I have to either transpile it by running `tsc` compiler (which is meh), or I can use `ts-node`, but I can't get it working either. Whatever. I'll figure some way – Jerry Green Mar 09 '21 at 19:45
-
@Pawel do you have idea how to run it if function is ES6 module and is importing something from another module for example `import { mockApi } from "@/api/instances";` ? – Verthon Jun 28 '21 at 11:31
-
-
1``npx run-func /path/to/folder/file.js myfunction `` This worked for me. thanks. – Jay Teli Oct 13 '22 at 07:27
As per the other answers, add the following to someFile.js
module.exports.someFunction = function () {
console.log('hi');
};
You can then add the following to package.json
"scripts": {
"myScript": "node -e 'require(\"./someFile\").someFunction()'"
}
From the terminal, you can then call
npm run myScript
I find this a much easier way to remember the commands and use them

- 15,167
- 5
- 62
- 99
-
1On my Win10 machine, this syntax is simply echoing the script (in either a PowerShell or Command Prompt terminal). Running it directly instead of via 'npm run' throws 'Unexpected token' pointing to the start of the require parameter. I haven't figured out how to make it work yet. – CalvinDale Oct 14 '18 at 15:42
-
@CalvinDale same here except I can run the script itself in powershell just fine. – ferr Nov 06 '18 at 20:49
-
12On my machine (Windows 10) i had to switch the double- and single-quotes, like this: "myScript": "node -e \"require('./someFile').someFunction()\"" Otherwise Node would just print out the command inside the single-quotes but not evaluate it. Maybe this solves the issues of @CalvinDale and ferr. – Christoph Jan 15 '19 at 03:15
-
7
Try make-runnable.
In db.js, add require('make-runnable');
to the end.
Now you can do:
node db.js init
Any further args would get passed to the init
method, in the form of a list or key-value pairs.

- 8,855
- 7
- 30
- 50
Sometimes you want to run a function via CLI, sometimes you want to require
it from another module. Here's how to do both.
// file to run
const runMe = () => {}
if (require.main === module) {
runMe()
}
module.exports = runMe

- 4,813
- 4
- 22
- 35
This one is dirty but works :)
I will be calling main()
function from my script. Previously I just put calls to main at the end of script. However I did add some other functions and exported them from script (to use functions in some other parts of code) - but I dont want to execute main() function every time I import other functions in other scripts.
So I did this, in my script i removed call to main(), and instead at the end of script I put this check:
if (process.argv.includes('main')) {
main();
}
So when I want to call that function in CLI: node src/myScript.js main

- 3,344
- 3
- 38
- 45
-
1For me is enough, and does not force double quotes escapes in `package.json` scripts – Marecky Feb 18 '21 at 07:29
-
2I don't think this is dirty at all. It's simple and explicit and it works. :-) – Holf Oct 22 '22 at 19:09
-
1You have no idea how much you helped me with this idea! Thank you so much!! – m4design Feb 16 '23 at 09:56
simple way:
let's say you have db.js file in a helpers directory in project structure.
now go inside helpers directory and go to node console
helpers $ node
2) require db.js file
> var db = require("./db")
3) call your function (in your case its init())
> db.init()
hope this helps

- 4,414
- 2
- 31
- 31
If you turn db.js
into a module you can require it from db_init.js
and just: node db_init.js
.
db.js:
module.exports = {
method1: function () { ... },
method2: function () { ... }
}
db_init.js:
var db = require('./db');
db.method1();
db.method2();

- 503
- 3
- 14
Updated for 2022 - If you've switched to ES Modules, you can't use the require tricks, you'd need to use dynamic imports:
node -e 'import("./db.js").then(dbMod => dbMod.init());'
or with the --experimental-specifier-resolution=node flag:
node --experimental-specifier-resolution=node -e 'import("./db").then(dbMod => dbMod.init());'

- 11,488
- 9
- 84
- 97
I do a IIFE, something like that:
(() => init())();
this code will be executed immediately and invoke the init function.

- 556
- 8
- 12
-
2But if you run: `node init.js` and the file contains an IIFE it will work. I think that I didn't fully understand your question. Sorry. – Natan Deitch Mar 02 '19 at 01:50
-
Totally get that, but if the code contains other functions they may or may not be called – winhowes Mar 03 '19 at 02:05
-
1
You can also run TypeScript with ts-node
similar to @LeeGoddard answer.
In my case, I wanted to use app
and init
separately for testing purposes.
// app.ts
export const app = express();
export async function init(): Promise<void> {
// app init logic...
}
// commonjs
npx ts-node -e 'require("./src/app").init();'
// esmodule
npx ts-node -e 'import("./src/app").then(a => a.init());'

- 1,252
- 2
- 15
- 22
-
Not necessary to use npx, can just do `ts-node -e 'require("./index.ts").run();'` – Dave Apr 04 '23 at 06:46
-
Npx will provide consistent reproducibility for `ts-node` unless a global binary of it has been installed. – Filip Seman Apr 04 '23 at 09:34
maybe this method is not what you mean, but who knows it can help
index.js
const arg = process.argv.splice(2);
function printToCli(text){
console.log(text)
}
switch(arg[0]){
case "--run":
printToCli("how are you")
break;
default: console.log("use --run flag");
}
and run command node . --run
command line
probuss-MacBook-Air:fb_v8 probus$ node . --run
how are you
probuss-MacBook-Air:fb_v8 probus$
and you can add more arg[0] , arg[1], arg[2] ... and more
for node . --run -myarg1 -myarg2

- 1,747
- 15
- 9
If you want to include environment variables from your .env files, you can use env-cmd
:
npx env-cmd node -e 'require("./db").init()'
If you want run a specific function in the file too, use run-func
:
npx env-cmd npx run-func db.js init someArg
Or, to provide an argument for the accepted answer you'd have to do something like:
npx env-cmd node -e 'require("./db").init(someArg)'
Writing/updating an expression here is less explicit (so easier to miss when you're checking back, for example) than providing different arguments to the commands, so I recommend using env-cmd
with run-func
.
Note: I also usually add --experimental-modules
on the end when necessary.

- 10,179
- 4
- 50
- 46
Inspired by https://github.com/DVLP/run-func/blob/master/index.js
I create https://github.com/JiangWeixian/esrua
if file index.ts
export const welcome = (msg: string) => {
console.log(`hello ${msg}`)
}
just run
esrua ./index.ts welcome -p world
will output hello world

- 19
- 1
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30409373) – Jmaurier Nov 23 '21 at 14:19
-
This is good for running functions in `ts` files from cmd. It seems that ts-node is behind the scenes working. – Timo Mar 04 '22 at 09:36
Following on from the other answers here, if you wanted to make this reusable you could create a script as follows:
// rn-fn.js
const [, , filePath, fn, ...args] = process.argv;
import(filePath).then((mod) => mod[fn](...args));
Then as an example:
// hello.js
export function hello(str) {
console.log(`hello ${str}`)
}
Running:
node ./rn-fn.js ./hello.js hello world
at the command line should then return
hello world

- 155
- 1
- 5
If your file just contains your function, for example:
myFile.js:
function myMethod(someVariable) {
console.log(someVariable)
}
Calling it 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

- 75,855
- 30
- 208
- 233
-
3Sure, that's how to run a JS file. The question was more aimed at whether I could run a specific function (out of many possible functions) where the only change was to the command line input rather than JS file itself per function call – winhowes Dec 18 '17 at 16:01
-
-
@jobmo it is, they want to run a method from the cmd line, this does that. ( i got here by googling the question myself, so someone else might appreciate the answer), don't worry there are a diverse set of answers you are allowed to have a choice – Blundell Feb 08 '19 at 08:55
-
Exactly, the question is about running a method from the cmd line. In this answer, myFile.js is executed.That's it. It is no executing any function. It happens then that the file has a function and the function is called inside the file. That was my point. – jobmo Feb 10 '19 at 14:53
-
You just explained that the answer executions the function from the command line :+1: – Blundell Feb 10 '19 at 15:08