Added in: v0.8.0
There is a project runner helper script in the Moleculer project. With it you don’t need to create a ServiceBroker instance with options, but a
moleculer.config.json file in the root of repo, fill it with your options, then call the
moleculer-runner NPM script.
Another solution is to put it into the environment variables instead of putting options to file.
In production, we recommend putting options into the environment variables! Use the
moleculer.config.jsonly during development or store only common options.
Note: It runs in this format in NPM scripts only. To call it directly from your console, use the
node ./node_modules/.bin/moleculer-runner.js --replformat.
||If true, it switches to REPL mode after broker started.|
||Disable the broker logger. It prints nothing to the console.|
||Hot reload services when they change.|
||Load configuration file from a different path or a different filename.|
||Load envorinment variables from the ‘.env’ file from the current folder.|
||Load envorinment variables from the specified file.|
||Launch [number] instances node (in cluster)|
Example NPM scripts
dev script loads development configurations from the
moleculer.dev.config.js file, start all services from the
services folder, enable hot-reloading and switches to REPL mode. Run it with the
npm run dev command.
start script is to load the default
moleculer.config.js file if it exists, otherwise only loads options from environment variables. Starts 4 instances of broker, then they start all services from the
services folder. Run it with
npm start command.
The runner does the following steps to load & merge configurations:
- It loads config file defined in CLI options. If it does not exist, it throws an error.
- If not defined, it loads the
moleculer.config.jsfile from the current directory. If it does not exist, it loads the
- Once a config file has been loaded, it merges options with the default options of the ServiceBroker.
- The runner observes the options step by step and tries to overwrite them from environment variables. Once
logLevel: "warn"is set in the config file, but the
LOGLEVEL=debugenvironment variable is defined, the runner overwrites it, and it results:
The structure of the configuration file is the same as that of the broker options. Every property has the same name.
Example config file
The runner transforms the property names to uppercase. If nested, the runner concatenates names with
Example environment variables
The runner loads service files or folders defined in CLI arguments. If you define folder(s), the runner loads all services
*.service.js from specified one(s). You can set services & service folder with
SERVICEDIR environment variables.
SERVICEDIRenv found, but no
SERVICESenv, it loads all services from the
SERVICESenv found, it loads the specified services from the
- If no
SERVICESenv found, it loads the specified services from the current directory.
It loads the
user.service.js files from the
It loads all
*.service.js files from the
my-services folder (including sub-folders too).
Moleculer Runner has a built-in clustering function. With it, you can start multiple instances from your broker.
Example to start all services from the
services folder in 4 instances.
Clustered Node ID
nodeIDwill be suffixed with the worker ID. E.g. if you define
my-nodenodeID in options, and starts 4 instances, the instance nodeIDs will be
Moleculer runner can load
.env file at starting. There are two new cli options to load env file:
-e, --env- Load envorinment variables from the ‘.env’ file from the current folder.
-E, --envfile <filename>- Load envorinment variables from the specified file.