Looking at the Node.js documentation for command line options to node
https://nodejs.org/api/cli.html
I see
-r, --require module#
Preload the specified module at startup.
Follows require()'s module resolution rules. module may be either a path to a file, or a node module name.
I am pretty certain that with some Node.js frameworks, this option is suggested, but I cannot figure out why it's used. I can't find any use cases online, although I have a slight suspicion that it may be useful for a library I am writing.
Does anyone know what it's used for?
This option is used to preload certain configuration data like secrets, DB settings, environment variables before your application actually runs. When the number of environment variables grows beyond say 10, it becomes hard to manage it manually.
For example, have a look at dotenv
package - https://github.com/motdotla/dotenv. It makes smart use of preload scripts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With