Quantcast
Viewing all articles
Browse latest Browse all 42

Circular Dependency in Node.js ES Modules

In the previous post we had discussed about circular dependency in Node.js modules using CommonJS modules specification.Here we will examine the behaviour of Node.js modules using ES Module specification.

We will use the same sample as used in the previous post with the the two modules modulea and moduleb cross reference each other leading to a circular dependency.

Image may be NSFW.
Clik here to view.
image

Image may be NSFW.
Clik here to view.
image

These files has following lines of code as shown below:

Image may be NSFW.
Clik here to view.
image

Image may be NSFW.
Clik here to view.
image

Image may be NSFW.
Clik here to view.
image

The output of the app.js program is quite different what we saw in case of CommonJS modules.

  • The circular dependency is tracked
  • The value of the counter variable is consistent in both the cases.

Image may be NSFW.
Clik here to view.
image

The main reason behind this is the difference in module loading process in case of ES module. It is done in three phases as shown below where dependency resolution and initialization of the exported variables are handled separately.

Image may be NSFW.
Clik here to view.
image


Viewing all articles
Browse latest Browse all 42

Trending Articles