You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm wondering if I use for e.g. module.exports = new A(); in the file A.js and that A.js will not be used by any other file with require(...) it will be cached to Node.js (if it's started with node A.js)? Because as I tested if I require that A.js in files like B.js and C.js it will be executed only 1 time (in the file who firstly required it), so it means after exporting it, it was executed and cached?
If A.js will be executed with node A.js and no one required it, it will be cached to Node.js?
If A.js will be used in B.js and C.js with require(...) it will be executed in the file who firstly required it and it will be cached? I mean no matter how much files require A.js it will be executed only 1 time in for e.g. B.js who was first?
A.js
const chalk = require('chalk');
class A {
constructor() {
console.log(chalk.red('Colored msg.'));
}
}
module.exports = new A();
Very roughly speaking modules implement the (per process) singleton pattern, with the caveat @bnoordhuis mentioned, and that if no one requires it, it will be run 0 times.
Reacted by Benas Svipas, Alexander Mills, M.K., paul dariye, Tonderai Lucian Davies Khatai, Mathéo, Andreas Rotter, mojtaba hashemi and Samuel Okoroafor
Reacted by Samuel Okoroafor
Reacted by Benas Svipas, Alexander Mills, M.K., Tonderai Lucian Davies Khatai, Baptiste Mathus, Kunal, Andreas Rotter and Saikat Guha
@bnoordhuis@refack What do you think guys, which is the best way to define private class which will not be required and only will be used by node (it will be the main file which will be executed firstly). I like ES6 syntax (I'm from Java and C#) 😄 and code reads more easier if I use classes.
So I have these options:
1.
new class A {
...
}
class A {
...
}
new A();
class A {
...
}
const a = new A();
Was with module.exports which I loved, but Node.js caches it... 😢
P.S. I know this question is a little bit off topic 😄.
P.S.S. Or it's better to avoid classes in such cases? When you use Node.js do you prefer classes?
You want to be very careful assuming modules are singletons. It is true, but module in this case means "fully-resolved path on disk", it doesn't mean the "A" in require('A'). In particular, npm/yarn are free to lay modules out on disk any way they want as long as the end result is that module can require its direct dependencies. Their is absolutely no requirement for them to be de-duplicated, so no requirement that they be global singletons. In other words, when module A requires X, and module B requires X, they might get different instances of the X module. When various files in A require X, they will always get the same X, but that assumption breaks down quickly.
You might not notice this until your module tree gets more complicated, or until you include more modules in your app.
Reacted by Benas Svipas, Refael Ackermann, M.K., paul dariye and vipul pant
if a.js b.js and c.js are in the same folder and if you require("a") from b and c, I assume that is when the caching works, if C was in a different location and tried to require a , it would get a new instance, am I correct?
Activity
bnoordhuis commentedon Apr 21, 2017
Yes to both/all three questions, with the caveat that B.js and C.js can programmatically delete it from their
require.cacheand then require it again.svipas commentedon Apr 21, 2017
@bnoordhuis Thank you very much for answer. Right now I discovered
require.cacheandrequire.resolve. Still new to Node.js 😄refack commentedon Apr 21, 2017
Very roughly speaking modules implement the (per process) singleton pattern, with the caveat @bnoordhuis mentioned, and that if no one requires it, it will be run 0 times.
refack commentedon Apr 21, 2017
"Welcome to the dark side, we have cookies"
svipas commentedon Apr 21, 2017
@bnoordhuis @refack What do you think guys, which is the best way to define private class which will not be required and only will be used by node (it will be the main file which will be executed firstly). I like ES6 syntax (I'm from Java and C#) 😄 and code reads more easier if I use classes.
So I have these options:
1.
module.exportswhich I loved, but Node.js caches it... 😢P.S. I know this question is a little bit off topic 😄.
P.S.S. Or it's better to avoid classes in such cases? When you use Node.js do you prefer classes?
refack commentedon Apr 21, 2017
Personally I'd go with (3).
But know:
app.jsthat requiresA.jsevery time you runnode app.jsA will be executed.const a = new A();in a method it will only run once, first time the module that contains it is required.Think of all modules as closures wrapped with
This is almost exactly what actually happens (was just too lazy to lookup the exact lines), even for the "main"
.jsfile that is run.svipas commentedon Apr 21, 2017
@refack Very good code which explains a lot. Thank you very much!
refack commentedon Apr 21, 2017
i.e.
B.js
app.js
sam-github commentedon Apr 21, 2017
You want to be very careful assuming modules are singletons. It is true, but module in this case means "fully-resolved path on disk", it doesn't mean the "A" in
require('A'). In particular, npm/yarn are free to lay modules out on disk any way they want as long as the end result is that module can require its direct dependencies. Their is absolutely no requirement for them to be de-duplicated, so no requirement that they be global singletons. In other words, when module A requires X, and module B requires X, they might get different instances of the X module. When various files in A require X, they will always get the same X, but that assumption breaks down quickly.You might not notice this until your module tree gets more complicated, or until you include more modules in your app.
svipas commentedon Apr 21, 2017
@refack @sam-github Thank you guys very much. 😍
slidenerd commentedon Apr 24, 2019
if a.js b.js and c.js are in the same folder and if you require("a") from b and c, I assume that is when the caching works, if C was in a different location and tried to require a , it would get a new instance, am I correct?