Pass Environment Variables out of Newman into NodeJs environment for further use

Archived topic — this may be out of date.
Newman has been replaced by the Postman CLI, Postman’s official, signed command-line tool. It runs collections as Newman did, and also lints API specs, runs mocks and monitors, and syncs local files with Postman from your terminal, CI/CD pipeline or coding agent.
Working with an AI coding agent? The Postman plugin lets agents in Claude Code, Cursor, Codex and others design, mock, test and ship APIs with Postman.
For current help, see the Postman docs, try the free Postman tools (no account needed), or join us on Discord.

In my scenario I need to use Node as the operating environment and pass variables out of Newman and used from within the .js that will be executing the script to start the collection:

newman.run({
    collection: require('./somename.postman_collection.json'),
    reporters: ['cli', 'html']
	delay: ['200']
	}).on('beforeRequest', function (error, args) {
    if (error) {
        console.error(error);
    } else {
        fs.writeFile(`request.txt`, args.request.body.raw, function (error) {
            if (error) { 
                console.error(error); 
            }
        });    
    }
}).on('request', function (error, args) {
    if (error) {
        console.error(error);
    }
    else {
        fs.writeFile(`response${getDateString()}.txt`, args.response.stream, function (error) {
            if (error) { 
                console.error(error); 
            }
        });        
    }
});

This .js will also be processing some data-files (which are the names of the variables that would be passed out of Newman) then use the new data-file (using appendFile to make multiple files one larger file) as input for the collection to be run. The alternative would be to use JS to make the HTTP:POST request and parse the JSON outside of Newman; however, this solution has eluded me as well. Any help would be GREATLY appreciated.

hi,

Had you found a solution?
I have the same problem. I want to get variable from Newman using js.

I was unable to solve this problem; however, I just worked around it by only using Newman to perform small functions, not as I originally intended but in the end the solution was resolved/fixed. I have spoken to others familiar with node and Newman, and to their knowledge, passing variables out of newman and into another js is not possible.

I was originally using Newman to run everything in the collection; however, I settled for using Newman for a small portion of the original requests in the collection and the requests that I needed to have variables for - I wrote some small JS scripts using Axios to make the requests, parse the info I needed and create variables from them, then utilized basic callbacks and/or Promises when needed. Hope this helps.

In Axios, responses are already served as a javascript object, no need to parse, simply get a response and access data.

I was originally using Newman to run everything in the collection; however, I settled for using Newman for a small portion of the original requests in the collection and the requests.