Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js 2 require()

I saw some Node.js code as below with 2 require() together in a statement but don't know what does it means, does anyone know that? thx!

const Models = require('mongoose-models')(require('mongoose'));

Thanks, Jack

like image 911
lihomahowareyou Avatar asked Mar 31 '26 07:03

lihomahowareyou


1 Answers

Break up of your query

const Models = require('mongoose-models')(require('mongoose'));

This actually equivalent to

const mongoose = require('mongoose')
const Models = require('mongoose-models')(mongoose);

so you are passing the reference of mongoose to mongoose-models. mongoose-models must require mongoose object to work perfectly. For readablity, you can use two-step initialization.

like image 161
mehta-rohan Avatar answered Apr 02 '26 20:04

mehta-rohan



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!