What does the command `newman run collection.json` do, and how does it relate to `newman.run` in Node?
answer
- Only one verb exists on the executable
- The positional argument may be a URL
- The command's action is a thin wrapper
- require('newman') exports a single function
- An EventEmitter comes back, not a promise
basics
~20 snewman run <collection> is the CLI's single command: it takes a collection as a path or URL, executes it, and reports through the cli reporter. Its action just builds options and calls newman.run(options, callback) — the same runner.
solid answer
~40 s`run <collection>` is the only command the Newman CLI declares, alongside a `-v, --version` flag on the program. The collection argument may be a filesystem path or a URL. The command's action is small: it reduces the parsed command into a plain options object, sets `options.collection`, and calls `newman.run(options, callback)`. That library entry point is the whole runner — `require('newman')` exports exactly `{ run }`. It returns an `EventEmitter` that emits the run's events (`start`, `beforeItem`, `request`, `assertion`, `item`, `done`) and also carries `summary`, `runner` and `exports`; the callback fires once as `callback(err, summary)`. So a terminal command and a Node script drive identical code. The CLI adds argument parsing, the `cli` reporter default, and the process exit code on top of it.
code
bash · 1 linenewman run ./collection.json --reporters cli,jsongo deeper
Be ready to type the command from memory: the verb is run, the argument is the collection, and everything else is an option. Know that the argument can be a URL as well as a file path.
Explain that the executable's action does little more than build an options object and call newman.run(options, callback), and that the callback receives (err, summary) while the call itself returns an event emitter.
Show judgment about when a Node script beats a shell line: runtime collection selection, live assertion streaming, merging summaries. Say plainly which CLI behaviours — default reporter, exit code — you must reimplement yourself.
Own the decision of whether the team's runs are invoked as commands or as code. A wrapper is a maintained internal tool with its own bugs; a command line is portable but grows unreadable option strings.
## One command, one argument The Newman executable is a `commander` program that declares exactly **one command**: `run`, written as `run <collection>` and described in its own help text as "Initiate a Postman Collection run from a given URL or path". The program itself adds `-v, --version`, and the automatic help *command* is switched off. Every Newman invocation therefore has the same shape: - the verb is always `run`; - the positional argument is the collection, given as a filesystem path or a URL; - everything else is an option; - an unknown verb prints `error: invalid command` followed by the help text. There is no `newman test`, no `newman exec`, no verb per environment. If a candidate reaches for a second command, they have not run the tool. ## What the command's action actually does The body of that command is short, and reading it is the fastest way to understand Newman. It reduces the parsed `commander` command into a plain object, parses the `--reporter-*` nested options out of the original argument vector, sets `options.collection` to the positional argument, and then calls the library: ```javascript newman.run(options, function (err, summary) { const runError = err || summary.run.error || summary.run.failures.length; runError && !options.suppressExitCode && (process.exitCode = 1); }); ``` That is the whole CLI. No second engine, no separate execution path, no child process per request. `newman run` is argument parsing wrapped around a function call, plus the exit-code decision above. ## The library entry point `require('newman')` resolves to a module whose entire export is `{ run: require('./run') }`. `newman.run(options, callback)` accepts the same option keys the CLI produces — `collection`, `environment`, `globals`, `iterationData`, `reporters`, `cookieJar`, `workingDir` and the various `export*` paths — and it is **polymorphic**: pass a function alone and it is treated as the callback with empty options. It builds a runtime `Runner`, resolves options, and starts the run. What comes back matters as much as what goes in: 1. The **return value** is an `EventEmitter`. Newman re-emits the runtime's events on it — `start`, `beforeIteration`, `beforeItem`, `beforePrerequest`, `prerequest`, `beforeRequest`, `request`, `beforeTest`, `test`, `beforeScript`, `script`, `assertion`, `item`, `iteration`, `beforeDone`, `done`. 2. That emitter also carries properties: `summary` (the accumulating run summary), `runner` (the runtime runner instance), and `exports` (the list of files reporters asked to have written). 3. The **callback** is invoked once, as `callback(err, summary)`, after those exported files have been written. A reporter is nothing more than a constructor invoked with that emitter — `new Reporter(emitter, reporterOptions, options)` — which subscribes to the events it cares about. The built-in `cli` reporter and a reporter written in your own repository see exactly the same stream. ## CLI and library, side by side | Concern | `newman run <collection>` | `newman.run(options, cb)` | |---|---|---| | Options | parsed from `argv` by `commander` | you build the object yourself | | Collection | positional argument, path or URL | `options.collection` | | Default reporter | `cli`, from the option's default value | none unless you set `reporters` | | Result | printed by whichever reporters ran | `cb(err, summary)` plus emitter events | | Process exit code | set to `1` when the run failed | never set — your code decides | The last two rows are the ones people get wrong. `suppressExitCode` — the option behind `-x` — is read in the executable and nowhere else in the library. The `['cli']` reporter list is a `commander` option default, not a library default: call `newman.run` with no `reporters` key and the run happens in complete silence. ## When to reach for each - **CLI** for a build step, a local check or a container entry point: it already parses arguments, defaults to readable output, and converts the result into a process exit status. - **Library** when the run is one step inside a larger Node program: choosing collections at runtime, streaming `assertion` events into your own dashboard, merging several runs' summaries, or deciding the exit status on your own terms. - **Both** in one repository is normal. A short Node script calling `newman.run` is often easier to maintain than a shell line carrying fifteen options. ## Misreadings to avoid - Treating the CLI as "Newman" and the library as an add-on. It is the other way round: the library is Newman; the CLI is its argument parser. - Expecting `newman.run` to return a promise, or the summary itself. It returns an emitter; the summary arrives in the callback. - Assuming a Node wrapper inherits the CLI's exit-code behaviour. It does not, and a wrapper that never inspects `summary.run.failures` reports success for every run.
- Does `newman.run` set the process exit code the way the command line does?No. The exit code is assigned inside the executable's action for the `run` command, which inspects the summary and sets `process.exitCode = 1`. `newman.run` only hands you `(err, summary)` and an emitter. A Node wrapper has to read `summary.run.failures` and `summary.run.error` itself and decide what the process should exit with.
- Why would you use the emitter that `newman.run` returns instead of just the callback?The callback fires once, at the end. The emitter streams the run as it happens — `start`, `beforeItem`, `request`, `assertion`, `item`, `done` — which is how every reporter is written and how a wrapper reports progress live. The emitter also exposes `summary`, `runner` and `exports` while the run is still going.
- What happens if you call `newman.run` with no collection option at all?The run never starts. After the options are resolved, Newman checks that `options.collection` is an object and, when it is not, invokes the callback with an error reading `expecting a collection to run`. Nothing is sent and no reporter is initialised.
The CLI is a keyboard shortcut for a function call: the shortcut interprets your keystrokes, but the same function underneath does all the work.
saying these in an interview costs you the question
- Believes the CLI and the library are two different engines
- Thinks the collection argument must be a local file path
- Expects newman.run to return a promise or the summary
- Assumes a Node wrapper inherits the CLI's exit code
- Looks for extra commands such as newman test or newman exec
- Expects terminal output from newman.run without naming a reporter