Search code examples
javascriptgoogle-apps-scriptmomentjslibrariesluxon

Error when replacing Moment.js with Luxon in Google Apps Script


I have a Google Apps script that uses Moment.js that I want to use Luxon instead. The following worked fine for using moment.js:

eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.js').getContentText());

However, eplacing the URL with the CDN URL of Luxon like so:

eval(UrlFetchApp.fetch('https://cdn.jsdelivr.net/npm/luxon@3.0.3/build/global/luxon.min.js').getContentText());

Throws an error:

console.log(DateTime.now().toString());
ReferenceError: DateTime is not defined

Copying the minified file to luxon.gs also fails with the same error.

I have no idea if the problem is with something that changed in Luxon or a limitation of Apps Script — any ideas?


Solution

  • The issue was not adding the namespace:

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