Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure your NodeJS application in different modules?

so far i've learned a bit about NodeJS. But now i want to write a huge enterprise app with it and i'm wondering how to setup the structure correctly? Coming from other languages like PHP and Java, i imagine, i would split my project in different NPM modules. For example @mybigproject/customer, @mybigproject/cart and @mybigproject/checkout and so on.

But those submodules would be installed in the node_modules folder of the application skeleton. How would i tell for example Express, that the template files are in the different module directories? Or for example i use TypeORM for data access. So each module would have it's own set of models. How do those models know the database configuration data, as it's only in the main application skeleton, or the other way around, how does the application skeleton should know where to find the models?

like image 330
user7249345 Avatar asked Mar 27 '26 13:03

user7249345


1 Answers

Don't use npm modules for different parts of your project.

This components is integral part of your project and usually depend on your global config / schema / routing / etc

Just put it in different files and require it where you need it.

You can get an idea for folders structure from projects like Sail.JS

Use npm modules if you writing some utility that going to serve you for different apps and you want an easy way to upgrade the utility code once for all your apps (or in case you want to share that utility as open source for all of us)

like image 186
yeya Avatar answered Mar 30 '26 03:03

yeya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!