This brings us to an important point: what happens when you import a module mul‐
tiple times? For example, consider what happens if we import our home-grown
debug
module twice:
const
debug1
=
require
(
'./debug'
)(
'one'
);
const
debug2
=
require
(
'./debug'
)(
'two'
);
debug1
(
'started first debugger!'
)
debug2
(
'started second debugger!'
)
setTimeout
(
function
() {
debug1
(
'after some time...'
);
debug2
(
'what happens?'
);
},
200
);
You might expect to see something like this:
one started first debugger! +0ms
two started second debugger! +0ms
one after some time... +200ms
two what happens? +200ms
But what you will actually see is this (plus or minus a few milliseconds):
one started first debugger! +0ms
two started second debugger! +0ms
one after some time... +200ms
two what happens? +0ms
As it turns out, Node only ever imports any given module once (every time a Node
app is run). So even though we import our
debug
module twice, Node has “remem‐
bered” that we imported it before, and used the same instance. Thus, even though
debug1
and
debug2
are separate functions, they both share a reference to
lastMes
sage
.
This behavior is safe and desirable. For reasons of performance, memory usage, and
maintainability, it’s better for modules to only ever be included once.
The way we’ve written our home-grown debug module is similar to
the way its npm namesake works. However, if we did want multiple
debug logs that had independent timing, we could always move the
lastMessage
timestamp into the body of the function that the
module returns; then it will receive a new, independent value every
time a logger is created.
288 | Chapter 20: Node