DEV Community

Steve Pepple
Steve Pepple

Posted on

Neat, Easy Way to Run Node.js Functions from the Command Line

Today I learned a quick trick for running and testing Node scripts from the command line.

It’s as simple as this with NPX:

npx run-func ./your-file.js functionName

NPX will prompt you to install run-func. You can also add any optional or required argument after the function name.

What’s particularly nice is that NPX will handle any dependency imports. The same thing can be accomplished with this node script, but it won’t handle tab-complete as nicely:

node -e 'require("./dist/your-file.js").yourFunction()'

Of course you can also use the Node REPL (Read-eval-print loop) interpreter by typing node in terminal and then requiring and running any methods in your script or module. Nice for interactive programming, but I found the output isn’t as friendly.

Top comments (0)