I am confused with using global variables in my express js application which will be running on multiple core machines (it can be done with pm2).
here are my approaches
1)Global variable
global.db = new Sequelize('ICG_dev','root','root')
Access to other files with global.db
2)module.exports
module.exports.db = new Sequelize('ICG_dev','root','root');
Access to other files with
const db = require ('db.js');
db.get()
What are the pros and cons of the above two methods if I am running my application on multiple core machines
The fact you're running your application on multi-core machines with pm2 is completely irrelevant to the question. Each Node.js process will be distinct. pm2 just runs multiple processes. Globals (and loaded modules) will not be shared across those processes.
So the question is really: Is it better to have a global variable, or export something from a module that other modules can import via require
?
It's up to you, but overwhelmingly, I'd say the community has come down on the side of: Use the module.