License Type | SaaS | On-Premise |
Agent Mode | Assess | Protect |
Main Product Category | Node.js Agent |
Sub Category | Command line |
You should use YAML configuration and environment variables for most Node.js agent configurations, however, it is possible to use command line arguments for some settings.
The Contrast Node.js agent has a help parameter that will list all the valid command line values:
Usage: node -r @contrast/agent app-main.js [agent arguments] -- [app arguments] Options: -h, --help output usage information -V, --version output the version number -c, --configFile <path> path to agent config file
To pass configuration options to the Node.js application being run with Contrast, use the
--application.args
flag, or append --
to the run command, followed by the arguments for the application.For example, to pass
appArg0 foo
and appArg1 bar
directly to the application, use::npm run contrast -- --agent.logger.level debug -- --appArg0 foo --appArg1 bar
For command line arguments, the Node.js documentation shows that scripts are executed like this:
node [options] [V8 options] [script.js] [--] [arguments];
For example, if you have added a "contrast" script to your application's
package.json
, use: "scripts": {
"contrast": "node -r @contrast/agent server.js --configFile=/home/contrast-user/contrast_agents_RHEL76/node/3.4.0/contrast_security.yaml",
...Background
The Contrast agent is a Node.js wrapper (runner) that invokes
node
to start the application. The agent doesn't pass any flags to the underlying Node.js executable, or provide the ability to do so with agent configuration options.To pass command line flags to Node.js, you must invoke
node
explicitly with the agent within the scripts
section of your application's package.json, followed by the name of the application's entry point file and any configuration flags, as shown above.When the agent is installed, a symlink is created, <app-dir>/node_modules/.bin/node-contrast, which points to the file <app-dir>/node_modules/node_contrast/cli.js. You can use either of these in the
script
section of your application's package.json when starting the application this way.For example:
-
Without the Contrast agent, you start your application like this:
node --title=MyWebsite --stack-trace-limit=25 ./index.js --env development
-
To run the application with the same Node.js flags and the Contrast agent, you could use either of these commands:
node --title=MyWebsite --stack-trace-limit=25 ./node_modules/.bin/node-contrast ./index.js -- --env development
node --title=MyWebsite --stack-trace-limit=25 ./node_modules/node_contrast/cli.js ./index.js -- --env development