3
onkarj
6y

Hello, a question about Node project -
How to import node libraries only once and use them all over the project.

I have asked it on StackOverflow, link to it

https://stackoverflow.com/questions...

If you guys have any idea for it, please, share!

Comments
  • 5
    Already -1 points. Sounds about right for SOF 🤣
  • 5
    You’re worried about overhead, but chose nodeJS 🤔
    That’s not how that works.
  • 1
    @C0D4 I don't have knowledge about anything other than node. Just need to implement it in my project!
  • 1
  • 1
    For you guys to understand my need more properly -
    I got this idea from Angular's ngModules where you can import everything in single ngModule decorated class and then you can access all that in all the other files under that module!
  • 4
    @onkarj The imports are needed because you code in a modular fashion.
    Every module is a box for itself and within the module it does not know what usable stuff is outside (except for some global stuff).

    What you are asking is possible to do, but will lead to seriously bad code!

    Btw: When you add an import at the top of your module, it will add this import's code only ONCE in the whole project.
    The import is just a way to tell the module you need access to this other module, but br other module is ONLY ONCE in your transpiled code.
    It's similar to the #include directive (with include guard in the .h file of course) in C/C++.
  • 1
  • 2
    Modules are only imported once and then cached. These are the kind of things that cause so little overhead, you may as well ignore it... If you are looking into performance tuning you are looking at the wrong end. Even when a "module" is not executed in a node context, build tools like webpack cache your modules anyway...
  • 3
    @onkarj In your stackoverflow post you mention that you want to basically import all modules in a single file and then import that file with all exports in your modules, Instead of having separate imports for each module you want...

    Sorry if I am blunt but that just dumb. Why would you do that? Thats just not how modules work. Importing modules is too hard for you? Come on seriously? Every editor worth its salt will give you a option to import modules automatically so there is literally 0 work. How is this even a thing?
  • 2
    You don’t want this, only import the one that you needed
  • 2
    You. Just. Don't.

    I am suspecting an XY problem. Your attempted solution won't help you at all. Full stop.
  • 2
    As long as you only install module once (check out package.json's "resolutions" field), it will get imported only once, and all 'require' calls (or bundlers) will return a reference to the same object.
  • 0
    The whole idea was just to know, how exactly Angular does that, when you import things in NgModule, you don't need to import them anywhere in the other files under that module. How does this work?
    Just wanted to know this. But, people here seem to just throw some basic import export knowledge that I already know.
  • 0
    @onkarj Where did you get Angular from? You were asking about Node the whole time... So let's get this straight: you actually want to know how Angular does it so that you can import components and services in @Module, and use them across your components without specifically importing them each time?
  • 0
    @hitko Exactly, that's what I wanted to know. But its long back ago now😌
Add Comment