Installing and Running Newman
Prerequisites
Newman is distributed as an npm package, so Node.js and npm must be installed on your machine before you can install it.
Installing Newman
| Command | Purpose |
|---|---|
| npm install -g newman | Installs Newman globally on the system |
| newman -v | Confirms the installed version |
Exporting a Collection
Before running with Newman, export the collection (and environment, if used) as JSON files from Postman using the three-dot menu next to the collection name.
Running a Collection
The basic command is newman run collection.json. Adding -e environment.json applies a specific environment, and results are printed directly in the terminal.
Useful Options for Everyday Runs
The basic run command is just the start. A few flags cover most real needs.
# Run with an environment and a globals file
newman run collection.json -e staging.json -g globals.json
# Run only one folder inside the collection
newman run collection.json --folder "Login APIs"
# Repeat the collection 5 times
newman run collection.json -n 5
# Feed rows from a CSV file into each iteration
newman run collection.json -d users.csv
# Override a single environment variable from the command line
newman run collection.json -e staging.json --env-var "baseUrl=https://qa.example.com"
# Pause between requests and fail fast on the first error
newman run collection.json --delay-request 200 --bail
When you pass a data file, each row becomes one iteration, and column names become variables you can use as {{username}} in requests and scripts.
Running a Collection Straight from Postman
Exporting JSON files works, but files can go stale as people edit the collection in the app. Newman can also fetch a collection through the Postman API using a URL that includes your API key. Keep that key out of your code: store it as a secret in your CI tool and read it from an environment variable.
Newman Inside a Node.js Script
Newman is also a library, so you can start runs from JavaScript and react to the results.
const newman = require("newman");
newman.run({
collection: require("./collection.json"),
environment: require("./staging.json"),
reporters: "cli"
}, (err, summary) => {
if (err) { throw err; }
const failures = summary.run.failures.length;
console.log(`Finished with ${failures} failure(s)`);
process.exitCode = failures > 0 ? 1 : 0;
});
How Exit Codes Help Automation
Newman exits with code 0 when every request and test passes and a non-zero code when something fails. CI tools such as Jenkins, GitHub Actions and GitLab CI read that code to mark the build as failed. That is why a Newman step can act as an automatic quality gate after every deploy.
# Inside a CI job
npm install -g newman
newman run tests/collection.json -e tests/ci.json
Troubleshooting Checklist
- "newman: command not found": the global npm folder is not on your PATH, or install with
npx newmaninstead. - Variables are empty: the environment file was not passed, or the variable names differ in case.
- Works in Postman, fails in Newman: check for values saved only as "current value", which are not exported.
- SSL errors on internal test servers: configure the proper CA certificate with
--ssl-extra-ca-certsrather than turning certificate checks off. - Local files not found: use
--working-dirso file uploads resolve correctly.
Keeping Versions Consistent Across Machines
A global install is fine on your own laptop, but different team members and CI servers can end up with different Newman versions. A safer option is to add Newman as a development dependency of a small Node.js project inside your test repository with npm install --save-dev newman, and run it through an npm script. The lock file then pins the exact version everyone uses, and new joiners only need npm ci to get started. Check periodically that your Node.js version is still supported by the Newman release you use.
Ready to master Postman API Testing Course?
Join Uncodemy's hands-on training and build real-world API testing skills with expert mentors.