The NodeJS process object provides several useful properties. Out of this an important property is the NodeJS process.mainModule. In this post, we will learn how to use it in our application.

If you are new to NodeJS, check out this post on getting started with NodeJS.

1 – What is the NodeJS process.mainModule?

The NodeJS process.mainModule is an in-built application programming interface of the process module. We can use this interface to get the main module.

See below example:

const express = require('express');

const app = express();

console.log(process.mainModule);


app.listen(3000)

In a typical ExpressJS application or any NodeJS application, we can access the process object. If we try to print the mainModule property of this object, we get a response similar to below:

Module {
  id: '.',
  exports: {},
  parent: null,
  filename: '/Users/saurabhdashora/NodeProjects/node-express-demo/app.js',
  loaded: false,
  children: [],
  paths:
   [ 
    '/Users/saurabhdashora/NodeProjects/node-express-demo/node_modules',
    '/Users/saurabhdashora/NodeProjects/node_modules',
    '/Users/saurabhdashora/node_modules',
    '/Users/node_modules',
    '/node_modules'
   ]
}

As you can see, it prints the details about the mainModule of the application. Also, if there are child modules, details of those modules are also made available in the children property. The filename property contains the file path of the main module.

2 – Practical Use of NodeJS process.mainmodule

Next, let us consider a real world use of the process.mainmodule. In an earlier post, we tried to serve static HTML files using Express. In that application, we used the NodeJS core module path to build the path to the static files. See below:

router.get('/send-message', (req, res, next) => {
    res.sendFile(path.join(__dirname, '../', 'views', 'send-message.html'));
})

Depending on where the route file is located, we have to manage the path.join() function. This is unnecessary repetition and basically, we can avoid this by using the process.mainModule.filename property.

To do so, we will first create a module to return the root directory of our project. Check out the below code:

const path = require('path');

module.exports = path.dirname(process.mainModule.filename);

In this utility, we basically export the dirname of the main module’s filename.

Now, we can simply use this utility within our ExpressJS route handler. See below:

const path = require('path');

const express = require('express');

const rootDir = require('../util/path');

const router = express.Router();

router.get('/send-message', (req, res, next) => {
    res.sendFile(path.join(rootDir, 'views', 'send-message.html'));
})

module.exports = router;

As you can see, we require the path utility and store the project directory into the constant rootDir. Next, we can simply use the rootDir within the path.join() function.

In other words, we don’t need to figure out the relative path to the project directory from our router module. The utility has made our code more robust.

Conclusion

The NodeJS process.mainModule can have several other use-cases. However, using it to determine the root directory is one of the most common and useful applications of the same.

If you have any queries or comments about this post, please feel free to mention them in the comments section below.

Categories: BlogNodeJS

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *