Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
how to pass correctly a module in node

app.js --------------

const mongoose = require("mongoose");
const utils = require("./config/utils");
const app = express();

utils.initializeDb(mongoose);
app.listen(3000);

config/utils.js

module.exports = (mongoose) => {
  initializeDb: async () => {
    await mongoose.connect(
      process.env.MONGO_URI,
      {
        useNewUrlParser: true,
      },
      () => {
        console.log("Mongoose connection successfuly started");
      }
    );
  };
};

The error in the terminal/console is: TypeError: utils.initializeDb is not a function.

I'm digging into optimization, encapsulation, to clean more my code passing modules through functions and etc. and I tried this thing but it give me this error... I would like to know the error that is happening in the code and also some tips on how to optimize this code. Thank you :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

try the below method :

app.js

    const mongoose = require("mongoose");
    const initializeDb = require("./config/utils");
    const app = express();

    initializeDb(mongoose);
    app.listen(3000);

config/utils.js

module.exports = {
  initializeDb: async (mongoose) => {
    await mongoose.connect(
      process.env.MONGO_URI,
      {
        useNewUrlParser: true,
      },
      () => {
        console.log("Mongoose connection successfuly started");
      }
    );
  };
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!