Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of pre-loading Node.js modules with command line

Tags:

node.js

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?

like image 984
Alexander Mills Avatar asked Oct 18 '22 09:10

Alexander Mills


1 Answers

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.

like image 159
Harshal Patil Avatar answered Nov 03 '22 05:11

Harshal Patil