Running Node Modules from the Command Line

Being able to run node.js modules from the command line is a really useful thing, especially if you’re building an app that runs on a server, and might occasionally need to have pieces of it invoked manually.

For example, let’s pretend you had an app that creates some kind of report, say, analytics for your site. You might have created a node module called report.js that runs the reports, given a type of report you pass into one of the methods.

Let’s assume that script looks something like this:

var reporter = {};
reporter.createReport = function(reportType){
    //your logic to create a report

module.exports = reporter;

This very simple module exports a method called createReport, which can be included in your application and called, like so:

var reporter = require("./reporter");
reporter.createReport("site analytics");

But, what if you want to also be able to manually kick this off from the command line, in case someone asks you to run a report on the fly? Well, we can easily refactor our module to permit us to do just that.

Here’s our refactored module:

#!/usr/bin/env node

    var reporter = {};
    exports.createReport = reporter.createReport = function(reportType){
        //your logic to create a report

    if (!module.parent) {

Let’s break down what we’ve done.

  1. First, we’ve slightly modified how things are constructed on the object, choosing to assign our method to both the exports variable and to make it a method on reporter.
  2. Then, we’ve added logic that checks to see if this module has been required in by another script, and if not, runs our method with arguments from the command line as the arguments for the method. The module.parent call is a Node call that returns true if this module has been required by another file. By checking to see if the value is false, we’re telling our script that this wasn’t required by another file, and must have been executed as a stand alone file (such as from the command line).
  3. Finally, we wrap all of this in a self-invoking anonymous function, so our module logic runs immediately once the file is called.

That’s it! Now, you can call this file as we did above where it was required into another file, or you can call it from the command line like this: ./reporter "site analytics".

Note that if you don’t include the shebang in the file, you’ll need to prepend your CLI call with node.

BOOM! Now your node modules can be included and invoked from another file, or simply run ala carte from the command line. I’ve found a lot of uses for this, and hope you do as well!

Related Posts

How to Onboard a Product Designer

If you're bringing a product designer or UX designer in to help you design your product, there's a bad way to do it, and a good way to do it. Here's how to make sure you're doing it right.

Review: Slicing Pie

Slicing Pie is a new way to think about company equity splits, and it blows away the old methods you've probably used.

When Troubleshooting, Follow the Process!

When you're trying to troubleshoot something - a car that won't start, or a business that isn't working - follow the right process.

The Art of Finding a Way

Being resourceful and relentless is one of the keys to being successful (and a great shipper). When in doubt, find a way.

Why I Have a Cocktail at 3:30 Every Day

Every day at 3:30, I stop what I'm doing and have a cocktail. It's become an incredibly important part of my daily routine.

Everyone In a Startup Should Have This Skill

You know you need technical skills to build the product, and sales skills to sell it, but does your team have this critical skill?

Doing Things Makes You Feel Better

If you're feeling down, depressed or just in a blah mood, do something. Anything. Make a thing, clean a room. Action makes you feel better.

Review: Auth0

Auth0 is a service that provides identity as a service, so you never have to build authentication again. Here, I lay out the good and the bad.

Why I Unfollowed Everyone on Twitter

I unfollowed everyone (~4,500 people) on Twitter. Here's why I'd do such an insane thing.

How to Turn Off Facebook Live Notifications

Facebook Live is cool, but the constant notifications about new videos aren't. Here's how to turn them off and get some peace.