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
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 , you just have trigger momentjs first to make moment timezone work
1 Like
jeffdavis
(Jeffrey Davis)
June 16, 2021, 7:30pm
24
Thanks, This worked for me. But copying the code gave me the wrong quotes and it added a 59 to the code.
hbarba
(Heber Barba)
July 27, 2021, 9:38pm
25
worked for me AS-IS thanks
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
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.
system
(system)
Closed
August 23, 2023, 2:55pm
29
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
Hey everyone ,
We have released support for external packages in the latest version of Postman, you can read more about this here:
opened 03:41PM - 11 Apr 25 UTC
product/api-client
external-packages
We’re excited to announce a powerful new capability in Postman’s scripting envir… onment — support for **external public packages from the [NPM](https://www.npmjs.com/) and [JSR](https://jsr.io/) registries**! 🎉
With this release, you can now seamlessly supercharge your pre-request and post-response scripts using your favorite open-source packages right from inside Postman.
## 📽️ Feature Walkthrough
Getting up and running is easy:
1. Open the **Scripts** tab (Pre-request or Post-response) for any HTTP Request.
2. Click on the **Packages** dropdown to search and select a package from **NPM** or **JSR**.
3. Or, directly import packages into your script using [pm.require](https://learning.postman.com/docs/tests-and-scripts/write-scripts/postman-sandbox-api-reference/#import-packages-from-scripts). Examples:
```js
// From NPM
const lodash = pm.require("npm:[email protected] ");
// From JSR
const shortUUID = pm.require("jsr:@opensrc/short-uuid")
```
https://github.com/user-attachments/assets/c39c4951-f92d-4477-8b68-f2a573c80141
> 📚 Read the [full documentation here](https://learning.postman.com/docs/tests-and-scripts/write-scripts/packages/external-package-registries/) for more examples, caveats, and advanced usage.
## 🛠️ Planned Work Ahead
We’re just getting started! Here's what we're working on next:
- **Adding auto-suggestions** in the scripts tab for packages you import — making it even easier to discover and use package APIs as you type.
- **Extending package support across all protocols**, including gRPC, GraphQL, etc.
- **Bringing this feature to Postman CLI and Monitors** so your workflows stay consistent across all Postman tools.
Stay tuned! 💥
## ⚠️ Known Limitations
While this is a huge leap forward, here are a few limitations to be aware of:
- Postman's script environment is platform-agnostic. This means Packages that rely on [Node.js built-ins](https://nodejs.org/api/modules.html#built-in-modules) like `child_process` or `worker_threads` may not work as expected. We offer best-effort implementations of Node built-ins, but they may not have complete parity with Node.js.
- Packages that reference globals outside the [allowed sandbox environment](https://learning.postman.com/docs/tests-and-scripts/write-scripts/postman-sandbox-api-reference/#use-global-objects) may not behave correctly, depending on the part of the package that is executed.
- Packages that use Top-level `await` are not supported at the moment.
## 🙌 Thank You!
This was long overdue and wouldn’t have been possible without the incredible feedback and support from our Postman community. Your insights and use cases helped shape this—please keep it coming!
Happy Scripting!