1

In Node.js, considering a main.js file:

const TOOLS = require('myTools.js');
const LIB1 = require('lib1.js');
const LIB2 = require('lib2.js');

I'll use TOOLS functions from the main module (main.js), from LIB1 and from LIB2.

TOOLS will stablish connections to the database and I'm not sure if it is a good idea doing const TOOLS = require('myTools.js'); multiple times on different modules.

How do I "share" TOOLS so it is visible from everywhere?

Azevedo
  • 2,059
  • 6
  • 34
  • 52
  • 2
    Node.js caches requires, so you can require the same module in however many places you want, without it really having any negative effect. – adeneo Nov 02 '16 at 20:46
  • 1
    *docs* - [Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned](https://nodejs.org/api/modules.html#modules_caching) – adeneo Nov 02 '16 at 20:50
  • Oh no, I don't mess with Community, and I do think it's been answered before, and is a duplicate, I just don't think global variables is the correct answer. – adeneo Nov 02 '16 at 20:55

0 Answers0