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

Shitty Sales Have Made Product Development Harder

Shitty, one-sided sales processes have made product development much more difficult for early stage startups.

Why I'm Cold Emailing You

You might have gotten a cold email from me. Tasteless? Some people think so. Here's why I'm doing it.

How I Found Your Email

I've been cold emailing a lot of people, and many folks are surprised that I found their email. Here's where I dug it up.

Announcement: The Most Exciting Thing I've Done

Today, I'm announcing the most exciting project I've been involved with. It's called CrowdSync, and it helps to automate paperwork, communication and logistics when dealing with groups of people. Read on for why this is so big.

How to Post to Private Slack Channels from Zapier

If you automate posting to Slack via Zapier, you might need to post to a private channel. It's not entirely obvious how to do it, but actually pretty damn easy.

Sharing is the Currency of the Web

You consume free content all day on the web. The best way to pay back the people creating it? Share it.

The Magic of Low Fidelity

High-fidelity documentation is great, but it has a hidden dark side. Learn to embrace low-fidelity documentation, and you'll be amazed at the benefits.

Why You Should Blog More (Data)

When you stop blogging, people stop coming. Simple enough. Here's the proof.

Using a Linter Will Make You a Better Dev

Using a linter while you write code won't only make your code better formatted, it'll make you a better programmer.

Everyone Is Self Employed

The idea that you're not self-employed if you work fulltime somewhere is wrong. EVERYONE is self-employed.