• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

NodeJS Best Practice for require()

Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
Im in the process of splitting my nodeJS 'app.js' file into separate modules for different functionality, Passport in a module, mongodb stuff, API stuff, SQLserver interaction etc.

Trouble is some of them require the same modules. So passport needs access to Mongo as well as some of the API stuff. Currently each module does its own require for the things it needs then in my app.js just the modules themselves are required and used for my routes.

The problem is this means im in the situation where I get duplicates of things like mongo required.

So my question what is best practice in this situation? The alternative I can think of is require any things that could be duplicated, like Mongo, in my app.js and pass that around. But im not a fan on having to pass things around to me the point of putting things into modules is to make it completely self contained and independent of the rest of the programme. Relying on dependencies seems to defeat that.
 
Joined
May 5, 2008
Messages
3,318 (0.57/day)
Location
Dallas, Tx
Processor Intel i5-3570K @ 3.4Ghz
Motherboard Asrock LGA1155 Z77 Extreme 4
Cooling Cooler Master Evo 212
Memory 16GB (4X4) G.Skill Ripjaw 2 DDR3-1600
Video Card(s) Nvidia gForce GTX 660ti
Storage 1x Samsung 840 EVO 256GB 6Gb/s, 1x WD 500GB 6Gb/s, 1x WD 80GB 3Gb/s
Display(s) ASUS VH242H Black + 2 HP 2311x 23" LED
Case Fractal Design R4
Audio Device(s) Realtek OnBoard Both
Power Supply Cooler Master 850w
Software Windows 7 Ultimate 64-bit SP1
Sadly, I just started learning Node and Mongo so I don't know enough to help you with this. Have you tried to ask this on Stackoverflow? I only suggest this because you may get a quicker answer by asking it there than here.
 
Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
Sadly, I just started learning Node and Mongo so I don't know enough to help you with this. Have you tried to ask this on Stackoverflow? I only suggest this because you may get a quicker answer by asking it there than here.

Ive referenced Stackoverflow allot when i have problems but i don't have an account on there myself, don't know why but always felt a little intimidated at the thought of asking questions myself :p
 
Joined
May 5, 2008
Messages
3,318 (0.57/day)
Location
Dallas, Tx
Processor Intel i5-3570K @ 3.4Ghz
Motherboard Asrock LGA1155 Z77 Extreme 4
Cooling Cooler Master Evo 212
Memory 16GB (4X4) G.Skill Ripjaw 2 DDR3-1600
Video Card(s) Nvidia gForce GTX 660ti
Storage 1x Samsung 840 EVO 256GB 6Gb/s, 1x WD 500GB 6Gb/s, 1x WD 80GB 3Gb/s
Display(s) ASUS VH242H Black + 2 HP 2311x 23" LED
Case Fractal Design R4
Audio Device(s) Realtek OnBoard Both
Power Supply Cooler Master 850w
Software Windows 7 Ultimate 64-bit SP1
Ive referenced Stackoverflow allot when i have problems but i don't have an account on there myself, don't know why but always felt a little intimidated at the thought of asking questions myself :p

I understand. Stackoverlow can have some big a-holes on there at times.
 
Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
I understand. Stackoverlow can have some big a-holes on there at times.

yup, but to be fair just as many people who go into allot of detail explaining their answers.

Im leaning to the idea of having everything self contained within its own module rather then passing an instance of mongo and monk in all over the place. Even if that means calling:

Code:
var monk = require('monk');
var mongo = require('mongodb');

multiple times in different modules.

Plus it means my modules can be reused in other applications without any external dependencies, well as long as mongo etc is installed on the system.
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
27,037 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
Maybe look at how developers of popular packages do it? I love NodeJS, really awesome what it can do and how well it scales
 
Top