# Fail to load package "randexp"

**URL:** <https://community.postman.com/t/fail-to-load-package-randexp/70074>\
**Category:** Help Hub\
**Tags:** external-library\
**Created:** [October 22, 2024, 9:21am UTC](https://community.postman.com/t/fail-to-load-package-randexp/70074 "2024-10-22T09:21:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lamricky11](https://avatars.discourse-cdn.com/v4/letter/l/e9bcb4/32.png) [@lamricky11](https://community.postman.com/u/lamricky11)\
**Post date:** [October 22, 2024, 9:21am UTC](https://community.postman.com/t/fail-to-load-package-randexp/70074/1 "2024-10-22T09:21:57Z")

</div>

Hi,

I try to use the lib `randexp` but I fail to load in Postman pre-script.

I have tried using CDN:

```auto
pm.sendRequest("https://cdn.jsdelivr.net/npm/randexp@0.5.3/lib/randexp.min.js", (err, res) => {

    //convert the response to text and save it as an environment variable
    pm.collectionVariables.set("library", res.text());

    // eval will evaluate the JavaScript code and initialize the min.js
    eval(pm.collectionVariables.get("library"));

    console.log( new RandExp(/hello+ (world|to you)/).gen() );
})

```

which give an error

```auto
TypeError: Cannot read properties of undefined (reading 'text')

```

I also tired the variable method

```auto
var library = pm.collectionVariables.get('library');
 
// Invoke an anonymous function to get access to the dayjs library methods
(new Function(library))();
 
console.log( new RandExp(/hello+ (world|to you)/).gen() );

```

Error message

```auto
ReferenceError: RandExp is not defined

```

Am I missing something ?

PS.  
I also find that there is a [offical collection](https://www.postman.com/postman/postman-answers/request/rue8ufv/loading-a-library-from-a-variable?action=share&source=copy-link&creator=28596843&ctx=documentation), which is also not working

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [October 22, 2024, 10:16am UTC](https://community.postman.com/t/fail-to-load-package-randexp/70074/2 "2024-10-22T10:16:20Z")

</div>

Hi @lamricky11. Welcome to the Postman Community.

I’m not very certain what the issue could be here, but it appears that the library you’re trying to import isn’t minified. Sending a quick GET request to “[https://cdn.jsdelivr.net/npm/randexp@0.5.3/lib/randexp.min.js](https://cdn.jsdelivr.net/npm/randexp@0.5.3/lib/randexp.min.js)”, I see that it is unminified.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/b/2b8235d9e841dbbe054d8157617d995368befa38.png)

---

<div class="post-metadata">

**Author:** ![lamricky11](https://avatars.discourse-cdn.com/v4/letter/l/e9bcb4/32.png) [@lamricky11](https://community.postman.com/u/lamricky11)\
**Post date:** [October 22, 2024, 10:56am UTC](https://community.postman.com/t/fail-to-load-package-randexp/70074/3 "2024-10-22T10:56:22Z")

</div>

Hi @gbadebo-bello, thank you for the reply.

Does unminified cause a problem ?

I also tried in the variable method, using [this minified version](https://github.com/fent/randexp.js/releases), which it also not working. Is it because the lib is expoting a Class instead of a function?

---

<div class="post-metadata">

**Author:** ![appurva21](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/appurva21/32/34560_2.png) [@appurva21](https://community.postman.com/u/appurva21)\
**Post date:** [October 24, 2024, 9:46am UTC](https://community.postman.com/t/fail-to-load-package-randexp/70074/4 "2024-10-24T09:46:34Z")

</div>

Hey @lamricky11!

The code served by the CDN is not bundled, which is likely causing this issue as RandExp internally uses `ret` and `drange` packages.

I couldn’t find a CDN that provides the bundled version for RandExp but their repository has one. You can try the below snippet and use the lib as required:

```js
const window = {};
eval((await pm.sendRequest("https://raw.githubusercontent.com/fent/randexp.js/refs/tags/v0.5.3/build/randexp.min.js")).text());
const RandExp = window.RandExp;

console.log(new RandExp(/hello+ (world|to you)/).gen());

```

---

<div class="post-metadata">

**Author:** ![lamricky11](https://avatars.discourse-cdn.com/v4/letter/l/e9bcb4/32.png) [@lamricky11](https://community.postman.com/u/lamricky11)\
**Post date:** [October 25, 2024, 8:04am UTC](https://community.postman.com/t/fail-to-load-package-randexp/70074/5 "2024-10-25T08:04:26Z")

</div>

It acutally work!! Thank you @appurva21 .

Is `const window = {};` necessary for any package?

---

<div class="post-metadata">

**Author:** ![appurva21](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/appurva21/32/34560_2.png) [@appurva21](https://community.postman.com/u/appurva21)\
**Post date:** [October 25, 2024, 10:37am UTC](https://community.postman.com/t/fail-to-load-package-randexp/70074/6 "2024-10-25T10:37:52Z")

</div>

@lamricky11 It depends on how the package is bundled.

In some cases, you get a function that can be directly used, while in this case, if you closely look at [randexp.min.js](https://github.com/fent/randexp.js/blob/v0.5.3/build/randexp.min.js), it assigns RandExp to the global window object, which does not exist in Postman’s scripting environment.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/e/f/ef11b84f2c538cc92bd4b9f234a65fbcb09b7563.png)

Hence the `const window = {}` workaround to make the required code accessible.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [October 28, 2024, 10:38am UTC](https://community.postman.com/t/fail-to-load-package-randexp/70074/7 "2024-10-28T10:38:02Z")

</div>

Closed on October 28, 2024, 10:38am UTC

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