Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  15] [ 5]  / answers: 1 / hits: 110071  / 12 Years ago, thu, november 1, 2012, 12:00:00

I recently came across this article on how to write a singleton in Node.js. I know the documentation of require states that:




Modules are cached after the first time they are loaded. Multiple calls to require('foo') may not cause the module code to be executed multiple times.




So it seems that every required module can be easily used as a singleton without the singleton boilerplate-code.



Question:



Does the above article provide a round about solution to creating a singleton?


More From » node.js

 Answers
9

This has basically to do with nodejs caching. Plain and simple.



https://nodejs.org/api/modules.html#modules_caching



(v 6.3.1)




Caching



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, if it would resolve to the same
file.



Multiple calls to require('foo') may not cause the module code to be
executed multiple times. This is an important feature. With it,
partially done objects can be returned, thus allowing transitive
dependencies to be loaded even when they would cause cycles.



If you want to have a module execute code multiple times, then export
a function, and call that function.



Module Caching Caveats



Modules are cached based on their resolved filename. Since modules may
resolve to a different filename based on the location of the calling
module (loading from node_modules folders), it is not a guarantee that
require('foo') will always return the exact same object, if it would
resolve to different files.



Additionally, on case-insensitive file systems or operating systems,
different resolved filenames can point to the same file, but the cache
will still treat them as different modules and will reload the file
multiple times. For example, require('./foo') and require('./FOO')
return two different objects, irrespective of whether or not ./foo and
./FOO are the same file.




So in simple terms.



If you want a Singleton; export an object.



If you do not want a Singleton; export a function (and do stuff/return stuff/whatever in that function).



To be VERY clear, if you do this properly it should work, look at https://stackoverflow.com/a/33746703/1137669 (Allen Luce's answer). It explains in code what happens when caching fails due to differently resolved filenames. But if you ALWAYS resolve to the same filename it should work.



Update 2016



creating a true singleton in node.js with es6 symbols
Another solution: in this link



Update 2020



This answer refers to CommonJS (Node.js's own way to import/export modules). Node.js will most likely be switching over to ECMAScript Modules: https://nodejs.org/api/esm.html (ECMAScript is the real name of JavaScript if you didn't know)



When migrating to ECMAScript read the following for now: https://nodejs.org/api/esm.html#esm_writing_dual_packages_while_avoiding_or_minimizing_hazards


[#82249] Tuesday, October 30, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
xochitl

Total Points: 559
Total Questions: 95
Total Answers: 117

Location: Antigua and Barbuda
Member since Sat, Apr 24, 2021
3 Years ago
;