How can I use other packages than momentjs in postman

How can I use other packages than moments in postman’s pre-request scripts?

Hey @fleetadmiraljakob :wave:

Welcome to the Postman community! :postman:

Only specific packages can be used in the sandbox environment our of the box:

There are other ways to bring in different modules, a couple of ways are mentioned in this Postman Answers Public Collection:

https://www.postman.com/postman/workspace/postman-answers/collection/13455110-7a6c90f0-0062-4089-b206-27c803dc1c37?action=share&source=copy-link&creator=8213448

Hi, I now have this code:

const moment_code = pm.collectionVariables.get('moment_code');

(new Function(moment_code))();

let date = new Date();

pm.globals.set("currentdate", moment(date).tz("Asia/Shanghai").format("YYMMDD"))

And set my variable properly


cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.44/moment-timezone.min.js

Why isn’t it working?
There was an error in evaluating the Pre-request Script:TypeError: Cannot read properties of undefined (reading ‘version’)

Moment Timezone | Docs (momentjs.com)

To use moment-timezone, you will need moment@2.9.0+ , moment-timezone.js , and the moment-timezone data.

This isn’t something you can import into Postman using the Eval method as it has dependancies.

You can only use libraries that are completely standalone where everything is contained in the minified JS.

I assume you are trying to do this because the sandbox version of moment doesn’t include the timezone plugin.

Hi, so how can I get the current time in a timezone inside of Postman?

I literally found now way.

I don’t think you can with the moment library that is available in the Postman sandbox.

You may want to consider calling another API to return the timezone information.

For example…

https://timezonedb.com/

https://developers.google.com/maps/documentation/timezone/overview

But now I have the problem that postman does not await my async function:

async function fetchDataAndSetDate() {
    const jsonResponse = await fetch("http://api.timezonedb.com/v2.1/get-time-zone?key={{hastobeset}}&format=json&by=zone&zone=Asia/Shanghai");
    const result = await jsonResponse.json();

    const currentDateInShanghai = result.formatted;

    const dateObject = new Date(currentDateInShanghai);
    const formattedDate = dateObject.toISOString().slice(0, 10).replace("T", " ").replace(/-/g, "");

    pm.globals.set("currentdateinshanghai", formattedDate);
}

fetchDataAndSetDate();

Why does this need to be wrapped in a function?

Can’t you just use the Postman sendRequest() function instead to call the timezone API. This will have its own response that you can parse and then do the formatting. (I don’t know if Fetch is supported in the Postman sandbox).

Or even better, can’t this be a normal GUI request where you can just parse the response in the tests tab and set the appropriate variable.

On a side note, does this really need to be a global variable? Wouldn’t a collection variable be more suitable. (Does every collection in your workspace need access to this date?)

There are quite a few topics on here about async functions, so I would recommend searching the forum first if you want to continue with that route.

Also, have a look at the following.

Send asynchronous requests with Postman’s PM API | Postman Blog

I don’t know if this helps but I have added a Collection to my Public Workspace that shows a method of using moment with moment timezone in the sandbox.

https://www.postman.com/dannysbattlefield/workspace/danny-dainton-s-public-workspace/collection/8213448-0e2b03a4-c75a-4971-924b-2286151489ee?action=share&source=copy-link&creator=8213448

It was basically copied from something that @praveendvd had created. :heart:

1 Like

Learn something new every day. Didn’t realise that you could do that.

I always thought the library had to be completely self contained.

Same - I stumbled across this after searching for something else. :grimacing: @praveendvd has some great little helpers dotted around the internet :smiley:

I usually follow the same pattern if I need to bring in a different module but I’ve not seen it done like this for one that has a dependency on the other.

It’s not the perfect solution and but it will work and hopefully help with @fleetadmiraljakob current use case. :pray:

Works! Thank you soooo much!

1 Like

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