Error: Cannot find module 'process' in Pre-Request Script when calling process.cwd()

I am trying to use some of the features of Node to get some context around my running tests. Specifically I am attempting to retrieve the current directory so that I can troubleshoot failures when attempting to use external packages.

My research directed me to cwd method of the process object - I have attempted to call this method from the Pre-request Script tab of my Postman environment but it immediately fails with this error:

ReferenceError: process is not defined

I am new to node and am trying here but from what I’ve read there is no need to specifically require the process object in the script and that anything that is core to Node is accessible.

Things I have tried to resolve this:

  • Have a specific require call to process. This generates the same error
  • Uninstalled and reinstalled postman
  • Installed Node separately apart from Postman. This is also to support command line execution via Newman

None of the above have resolved the issue. Extensive googling and perusing of this community has shed zero light on this.

Is accessing node objects not a feature of Postman?
Am I doing something wrong?

Any insight would be greatly appreciated.

Hey @JWolf

This is because scripts in Postman are not executed on direct Node environment. They are are executed in a sandboxed environment. This allows Postman to add security restrictions to user scripts and other things like tracking variable changes etc.

Now you’re scripts should not be affected by the working directory or Postman install directory. Could you elaborate on what you’re trying out, may be a code snippet?

Sure. First a little bit of context. I am attempting to develop a method to extend the validation of the response beyond the standard ‘This is my expected response, verify it’s there motif’ I would like to be able to use logic to determine if not only:

1.) My response contains what I expect it to, but also
2.) My response does not contain anything I don’t expect it to.

I want to do this using the collection runner and data files. A response from the API can contain 50+ document sets. I am trying to avoid having to submit the same request 50+ times in order to validate each document set in the response. Since this is how the collection runner with data files works, I am attempting to come up with another method.

My goal is to have the data file and validation file be one in the same. I am attempting the following:

1.) Build the per request/test case data file with the required data for the single request targeted for testing, including the expected response data for that request.
2.) Use the collection runner/newman to run just a single iteration/request for the specific data file which will generate the response containing the multiple document sets.
3.) Use the content of the data file as an array to programmatically iterate through and compare/validate the response against the array, and also flag if the response contains unexpected data.

It seems that the data object at any time only contains the content of the current iteration so that is not a viable solution to use for this.

Ultimately I found a node package csv-array - That I wanted to use. It will convert CSV content to a JSON array, which on the surface seems like it would work. However if I understand your response, this will not work either as I am unable to path to anything external to what is used by Postman in the sandbox, which includes external node packages?

This seems evident that by trying to require the package:

var csv = require(‘csv-array’);

Throws this in the Postman console:

Error | Cannot find module ‘csv-array’

Is there a way to do this with the built in Postman methods?