Can I use the
require() without assigning it into a variable?
Technically yes, because
require() only executes code written in the module.
The code may return something or not. When it doesn't matter if the code returns anything and you want to execute the code only once you can use the
require() without assigning it to a variable.
Even if you
require()that module multiple times the code would execute only once.
console.log('Hello from example.js');
require('./example.js'); console.log('Hello from index.js');
Now, when you run the
node index.js command, you will get the following output:
Hello from example.js Hello from index.js