Check out Node module caching warnings for cases where a “single error” of modules will break.
If you always reference your single module with file paths (starting with ./, ../or /) in one package, you are safe.
If your service is wrapped in a package that will be used by other modules, you may encounter multiple instances of your singleton.
Let's say we publish this sweet library of services:
service-lib/
⌞ package.json
⌞ service.js
service.js:
var singleton = {};
module.exports = singleton;
In this application, server.jsthey other.jswill receive different instances of our service:
app/
⌞ server.js
⌞ node_modules/
⌞ service-lib/
⌞ service.js
⌞ other-package/
⌞ other.js
⌞ node_modules/
⌞ service-lib/
⌞ service.js
For now, this application will share with the instance:
app/
⌞ server.js
⌞ node_modules/
⌞ service-lib/
⌞ service.js
⌞ other-package/
⌞ other.js
npm install app . Node doc .