Adding external libraries to postman

let a = (res) => new Function(pm.variables.get(res))
pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.33/moment-timezone-with-data-2012-2022.min.js", (err, res) => {
    pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js", (err2, res2) => {

        pm.environment.set("res2", res2.text());
        pm.environment.set("res", res.text());

        eval(pm.environment.get("res2"));
        eval(pm.environment.get("res"));


        console.log(this.moment.utc().tz("America/Los_Angeles", "UTC").format())


    })
})

use min build instead as it won’t mess up global space. you just have to call eval on the response to use it and then use this.

now to reuse the code you just have to use :slight_smile:

eval(pm.environment.get("res2"));
eval(pm.environment.get("res"));


console.log(this.moment.utc().tz("America/Los_Angeles", "UTC").format())
1 Like

Hi @praveendvd,

Just a thought, if we directly save the js data in global/collection/environment variable.

then can we use the eval function on that?

1 Like

Yes yes you can do that :smiley:, you just have trigger momentjs first to make moment timezone work

1 Like

Thanks, This worked for me. But copying the code gave me the wrong quotes and it added a 59 to the code.

worked for me AS-IS thanks

more details

Hi all! Because this is still a highly viewed thread, I made a collection you can fork that shows a few different ways to import an external library. You can either pull a library directly from a CDN or load the contents of an external library into an environment variable. Either way, check out this collection to get started importing your favorite library today :slight_smile:

any tips in using alasql ( https://cdnjs.com/libraries/alasql/0.6.2 ) ? I am getting alasql undefined property, both by setting it as an environment var or pm.sendRequest to the cdnjs repo. also tried using window = {} but still not able to.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.