Thank you very much for taking the time and giving me feedback.
close
Visitors are also reading...
← Previous

Organize your NodeJS code without learning curve or new technology

02 May 2017

No refactoring time required for this easy organization. Your boss wants to move fast.. That does not mean you need to compromise code quality.

Easily breaking node project to modules - Overview

The easiest way is to isolate code to a folder.
Identify code that is already relatively encapsulated and that will work if you simply move it.

Here is a summary of the method:

Lets see some examples

Welcome to project mograblog. It is a huge project with thousand of files. It’s becoming hard to maintain.
Moreover, I have a folder named utils which is used all over. So a lot of files have a line that looks like ../../../utils.

 + mograblog-project
   + src
     - server.js
     - package.json
     + some-code
     + utils
       - string-utils.js
       - math-utils.js
       - posts-utils.js
       - index.js
     + some-more-code
     ...

I notice that all the code in utils is pretty isolated.
There are some dependencies to external libraries such as lodash and pad but no internal dependencies outside utils.

I move the files under packages and add file package.json

+ mograblog-project
  + src
    - server.js
    - package.json
    + some-code
    + packages
      + mograblog-utils
        - package.json
        - string-utils.js
        - math-utils.js
        - posts-utils.js
        - index.js
    + some-more-code
    ...

I deliberately add a prefix mograblog to utils so if a library exists by the name utils (and one does exist) - it won’t collide.

I modify package.json files

// mograblog-project/package.json
{
  "scripts": {
    "postinstall": "ls -ll utils", // this is now redundant.. I will move this soon.
  },
 // ...
  "dependencies" : {
    ...
    "lodash": "4.0.3"
    "mograblog-utils": "./packages/mograblog-utils",
   // ...
  }
}

// mograblog-project/packages/mograblog-utils/package.json
{
  "name": "mograblog-utils",
  "version": "0.0.0",
  "dependencies": {
    "lodash": "4.0.3",
    "pad": "^0.0.0"
  }
}

I now notice 2 things:

// mograblog-project/packages/mograblog-utils/package.json
{
  "name": "mograblog-utils",
  "version": "0.0.0",
  "scripts": {
    "postinstall": "ls -ll ."
  },
  "dependencies": {
    "pad": "^0.0.0"
  },
  "peerDependencies": {
    "lodash": "4.0.3"
  }
}

I also go over all dependencies that look like require('../../../utils') and change them to simply require('mograblog-utils')

What about the tests

You could move the tests under your new package and make them work there.
This would require some build changes as well.

Downsides

The main downside is that this still does not make your code reusable outside the mograblog-project so for example if you have a monorepo with multiple projects inside this code will only be available to this specific project.

 + monorepo
   + mograblog-project
     + packages
       + mograblog-utils // only available for mograblog-project
   - other-project

The technique will not work well with folders outside mograblog-project especially if you are using Docker to build mograblog-project.

Upside

What’s next?

This step will be the first in a series of steps.
The following steps might include

Which I will explore in future posts.

Summary

NodeJS is an amazingly flexible environment which allows you to get fast results especially when it comes to organizing the code.
Teams that need to move fast and are unable to put effort in the near future in learning curves and build modifications, do not need to compromise on code organization.

← Previous