Node help! Why this require works inside arrow function but no outside of it?

General Discussion
  • I'm geting DB is not a constructor for the following code. If I move the const DB = require("./db"); inside the mysql() arrow function then it works. But I can't understand why...

    const winston = require("winston");
    const DB      = require("./db");
    let logger  = winston.createLogger(
        format: winston.format.json(),
        transports: [
          new winston.transports.File({
              filename: 'logs.txt'}),
          new winston.transports.Console({
            format: winston.format.colorize()})
    logger.mysql = (level, msg, filename, stack)=>
        //Create the database object.
        const db = new DB();
        //Open the connection.>
            db.conn.query(`INSERT INTO t_logs (m_level, m_msg, m_filename, m_stack) 
            VALUES (?, ?, ?, ?);`, [level, msg, filename, stack], (err)=>
                //Something went wrong.
                if (err)
                //Close the connection.
    module.exports = logger;
  • FWIW, I should mention that this is a forum for community discussion for NodeBB, the forum software, not for Node.js 😄

  • @babaliaris who is calling the logger.mysql () function... probably a browser.. but the CONST DB was done in the main process I would guess..

Suggested Topics