Itβs not called the "best way." If you need to use the same object among different modules, you must wrap it in a module. Something like that:
//db.js var postgres = require (...) var connection; module.exports = { getConnection: function (){ return connection; }, createConnection: function (){ connection = createConnection (postgress); } }; //app.js - main file require ("./db").createConnection (); //a.js var db = require("./db") db.getConnection() //b.js var db = require("./db") db.getConnection()
source share