Search code examples
netsuitesuitescript2.0luxon

Using Luxon.js within NetSuite Server Side Scripts


I've successfully used Luxon many times within NetSuite client scripts.

Is there a way to use it within server-side scripts?

I've tried with the same naming convention as client script

define(['N/https', './luxon'], (https) => {

        const beforeLoad(ctx) {
            let DateTime = luxon.DateTime;
            let Duration = luxon.Duration;

            let today = DateTime.now()

        }

    etc....
}

The library is found with no issue, but always get the following when I declare let today = DateTime.now()

"error": {
    "name": "ReferenceError",
    "message": "Intl is not defined",
    "stack": "ReferenceError: Intl is not defined
at systemLocale (/SuiteScripts/luxon.js:963:28)
at Function.create (/SuiteScripts/luxon.js:...:65)
at new DateTime (/SuiteScripts/luxon.js:...:39)
at Function.now (/SuiteScripts/luxon.js:...:14)
at Object.onRequest (/SuiteScripts/suitelet.js:...:39)"
  }

I'm assuming this is because it's not called within a browser.

I was hoping it would return the object in the timezone of the server.


Solution

  • And a couple of googles after I made the post above, I came across an answer:

    globalThis.Intl = {
        DateTimeFormat: function() {
            this.resolvedOptions = function(){
                return {
                    locale: "en-US"
                }
            }
        }
    }
    

    Declared this at the start of the onRequest function, and now it works.