1

I am trying to load external javascript in the pre-request tab of postman request by following the steps mentioned in https://blog.postman.com/adding-external-libraries-in-postman/ but seems like it is not working. Below is the code I am trying and it is not loading. I am not sure what is wrong and what is not working

pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/chance/1.1.8/chance.min.js", (err, res) => {
   //convert the response to text and save it as an environment variable
   pm.collectionVariables.set("chancejs_library", res.text());
   eval(pm.collectionVariables.get("chancejs_library"));
   console.log(this.chance().string()) // --> not working
   console.log(this.chance()) // --> not working
   console.log(this.Chance()) // --> not working
   console.log(this.Chance().string()) // --> not working
})

In all the above cases, I get the same error

ReferenceError: chance is not defined
Venu
  • 1,513
  • 3
  • 19
  • 37
  • What does a console.log(this) output? you have absolute zero error handling, you sure the request is successful? – Marc Sep 14 '22 at 05:37
  • Yes the request is successful and I was able to see the entire source code in the collection variable. `console.log(this)` gave all the existing objects and functions, except `chance` – Venu Sep 14 '22 at 05:56
  • I've tried this myself and the issue seems to be with the source. I also ran a try/catch and no errors from eval. If I replace this for another library I see the object immediately. I tried other versions of the same library same problem, I tried the non min source, same problem. – bitoiu Sep 14 '22 at 07:58
  • @bitoiu Even I tried all those before posting the question. Thanks for trying from your end. I raised an issue on their git repo. – Venu Sep 15 '22 at 20:16

1 Answers1

0

It is indeed chancejs's "fault". I don't know what kind of "environment"/"runtime" Postman is providing for running js in general, but it seems that Chance() instance is never created in it - no matter if using eval() or IIFE... Last lines of chance.js are to blame (starting with the comment "// CommonJS module" where - to my understanding - instantiation happens).

So I figured a dirty "hack" which makes chancejs usable in Postman:)

I took version 1.1.9 of chancejs from github, then I simply added "else" statement to one of those "ifs" from instantiation section:

// if there is a importsScrips object define chance for worker
// allows worker to use full Chance functionality with seed
if (typeof importScripts !== 'undefined') {
    chance = new Chance();
    self.Chance = Chance;
} 
else {
    chance = new Chance();
}

Then I followed Load a library from a variable section from https://blog.postman.com/adding-external-libraries-in-postman/ (when setting chancejs collection variable I obviously had to use the "hacked" version of the chancejs)

var chancejs = pm.collectionVariables.get('chancejs');
(new Function(chancejs))();
// testing if this works by calling sentence() method :)
console.log(chance.sentence());

Seems to work :)