Using Luxon with Postman?

I am attempting to use Luxon with Postman, but I’m struggling to initiate calls via an extenal library. I continue to get the following error:

There was an error in evaluating the Pre-request Script:Error: luxon is not defined

Here’s a copy of the pre-request script I’m using to fetch the library:

if (!pm.globals.has("luxon_js")) {
    pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/luxon/3.4.4/luxon.min.js", (err, res) => {
            pm.globals.set("luxon_js", res.text());
    })
}

(new Function(pm.globals.get("luxon_js")))();
let DateTime = luxon.DateTime;

I was able to spin up DAYJS using a similar approach, but am now looking into Luxon as an alternative due to some time zone conversion issues I’ve uncovered using DAYJS.

The library needs to be completely self contained before you can use eval or the function as a global variable.

eval((await pm.sendRequest('https://cdnjs.cloudflare.com/ajax/libs/luxon/3.4.4/luxon.min.js')).text());

console.log(luxon.DateTime.now());

This complains about Intl not being defined.

image

Looking at the documentation.

Intl (moment.github.io)

Luxon uses the native Intl API to provide easy-to-use internationalization.

Which is where I think the problem lies. It can’t use the Intl libraries built into the Postman sandbox, so fails at this point.

Someone with JavaScript knowledge may be able to pull apart the minified json and tell you if there is a workaround or not.

I did try and include a standalone version of Intl but this just produces another reference error.

// https://cdnjs.com/libraries/intl
eval((await pm.sendRequest('https://cdnjs.cloudflare.com/ajax/libs/intl/1.2.5/Intl.complete.min.js')).text());

It looks like this library is meant to be installed as a node.JS application, so not sure you can just include it as it does appear to have dependencies despite what the Github repository says. Intl is installed with a Node.JS application as a base library, so most wouldn’t even know its there.

1 Like

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