Skip to content
Vinod Surendran

Vinod Surendran

Programmer, Photographer, Human

Menu
  • About
Menu
  • About

Build your nodeJS Application in a modular way

Posted on October 26, 2017April 24, 2020 by Vinod Surendran

I think you will spend 176 seconds reading this post

Now a days almost all web services or integrations are done on top of nodeJS run-time. NodeJS is a flexible platform with lot of community support. Even it is possible to create documents like xlsx, docx or pdf directly from nodeJS. All major cloud platforms uses nodeJS as their level 1 language.

Modularity

NodeJS by design achieves the modularity by using the node_modules structure. All the required modules are stored in node_modules directory and we can invoke the modules any where in our code.

But are we using this modularity in our application code. Most of the application I saw contains a lib folder, i which we store all js files . This js files are imported in the required areas using relative paths.

const db = require("../db/") 
const logging = require ("../../logging")

The main problem with this kind of approach is that when we change the path of a service file the path to the db should change. Also the format is not readable. We will be confused with the file’s authenticity.

The Solution

A much better approach is to design our application as modules, such as db , logging, error etc . Lets say your application name is cms, then it is much easier to represent the module using scope

require("@cms/db")

You can develop the modules separately and publish in to any NPM servers ( public / private ) and use it as like any other module.

If your application needs the logging module

npm i --save @cms/logging

If you don’t want to split your application in to bits and pieces, there is another approach.

A more better way

Keep the modules that you want inside a separate folder. Lets say “@cms”. Use separate folder for each module and let the modules have a separate package.json. This way it will become a valid node module.

The package json for the modules will look like this

{
  "name": "@cms/db",
  "version": "1.0.1",
  "description": "db module for CMS Application",
  "main": "index.js",
  "dependencies":{
	"mysql" : "latest"
  }
}

Once the modules are ready now its time to do some scripting. Add the install.js in the “scripts” folder.

let fs = require('fs')

console.log('Creating symlinks ...')
if (fs.existsSync('node_modules/@cms')) {
    console.log('link exists already ')
} else {
    let source = '../@cms'
    console.log(`creating link for ${source}`)
    fs.symlinkSync(source, 'node_modules/@cms', 'junction')
    console.log('done')
}

Add this script to your main package.json.

{
  "name": "CMSApplication",
  "version": "1.0.1",
  "description": "Sample CMS Application",
  "main": "index.js",
  "scripts": {
    "install": "node scripts/install.js",
    "start": "node index.js"
  },
  "dependencies":{
	"express" : "latest"
  }
}

The script will be executed every time you do npm install. So once all other node modules defined in the dependencies are installed, it will create a link from the @cms folder outside to the @cms folder inside node_modules. So any changes you make to the outer @cms folder will be reflected to the folder inside node_modules.

You can see that the symlinks are installed for @cms. This is not a shortcut file rather that hard links created using “ln” in linux.

node_modules folder
@cms folder is along with other npm modules in the node_modules directory

Inside @cms you can see our modules which is defined in the outer @cms folder

The modules inside @cms scope

This way you can achieve modularity. “@cms” folder is part of your source code. You can then import the required modules in the normal way.

This approach helps me in making the application more modular and extensible. Let me know your thoughts on this.

Posted in nodeJSTagged Linux, node, nodeJS, NPM, Tips and Tricks, Windows

Post navigation

JWT – Security with Simplicity
Eureka and AWS ECS

2 thoughts on “Build your nodeJS Application in a modular way”

  1. Bruce says:
    November 1, 2017 at 6:53 am

    Do you Google App Engine allows to to create symlinks?

  2. Vinod Surendran says:
    June 22, 2018 at 4:19 am

    This post is now obsolute. Now npm supports dependencies with file:// protocol, which is automatically linked inside the node_modules.

Leave a Reply Cancel reply

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

Recent Posts

  • Windows Terminal April 24, 2020
  • Eureka and AWS ECS May 2, 2019
  • Build your nodeJS Application in a modular way October 26, 2017
  • JWT – Security with Simplicity March 31, 2017

Recent Comments

  • Vinod Surendran on Build your nodeJS Application in a modular way
  • Bruce on Build your nodeJS Application in a modular way
  • Sujesh on My experiments with IoT – “Meeting with Pi”
  • Hostgator 1 cent coupon on How to get the TableSpace usage in Oracle
  • hostgator vps coupon code on How to get the TableSpace usage in Oracle

Tags

android android applications applications Arduino asianet dataline butteryfly chrome evdo Experiments firefox General google Hello World HTC Htc legend Ideas industry IOT Legend Linux linux ubuntu login microservice Mobile modem movie player Netflix node nodeJS NPM one click Oracle personal photo Raspberry Pi rooting script Scripts story Tips and Tricks tweaks ubuntu unix Update Windows

Archives

Categories

  • Android (4)
  • General (24)
    • Status Updates (8)
  • IoT (1)
  • Linux (4)
  • Microservice (1)
  • Mobile (5)
  • nodeJS (1)
  • Oracle (4)
  • Photography (1)
  • Tips and Tricks (3)
  • Windows (2)

Tags

android android applications applications Arduino asianet dataline butteryfly chrome evdo Experiments firefox General google Hello World HTC Htc legend Ideas industry IOT Legend Linux linux ubuntu login microservice Mobile modem movie player Netflix node nodeJS NPM one click Oracle personal photo Raspberry Pi rooting script Scripts story Tips and Tricks tweaks ubuntu unix Update Windows

History

  • April 2020 (1)
  • May 2019 (1)
  • October 2017 (1)
  • March 2017 (2)
  • February 2017 (2)
  • January 2017 (2)
  • July 2015 (1)
  • July 2014 (3)
  • January 2013 (4)
  • October 2011 (1)
  • September 2011 (1)
  • May 2011 (2)
  • December 2010 (4)
  • August 2010 (2)
  • July 2010 (1)
  • June 2010 (2)
  • May 2010 (2)
  • April 2010 (4)
  • December 2009 (2)
  • July 2009 (1)
  • May 2009 (6)

Categories

  • Android (4)
  • General (24)
    • Status Updates (8)
  • IoT (1)
  • Linux (4)
  • Microservice (1)
  • Mobile (5)
  • nodeJS (1)
  • Oracle (4)
  • Photography (1)
  • Tips and Tricks (3)
  • Windows (2)
Theme Design & Developed By OpenSumo
  • About