Scarass
Scarass

Reputation: 944

JavaScript modules hierarchy?

I can't believe I haven't found an answer to this question anywhere.

So, is there any kind of module hierarchy in JavaScript (EcmaScript 6 also)? So that a module can contain another modules. Or that modules are contained inside packages, and packages can contain another packages?

Upvotes: 1

Views: 612

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138287

Is it possible that modules are contained inside packages?

Well partly. Theres no package as it isnt necessary. This can be done easily with folders and nested requires.

E.g. you have an auth folder containing User.js and Admin.js , then you can bundle it with a index.js :

module.exports={
  Admin:require("Admin.js"),
  User:require("User.js")
};

Now you could load the "package" like this:

var auth=require("auth");
auth.User.login("sth");

So there is no package in JS but it can be easily made with nested requires. ( the upper code uses NodeJS style, you may use import instead of require).

So is there a module hirarchy?

Yes based on how they require each other.

Upvotes: 1

Related Questions