Rakesh Prasad
Rakesh Prasad

Reputation: 642

not able to refer external JS from NiFi ExecuteScript processor

  1. have a simple flow where i want to modify flowflow attribute using moment.js.
  2. code works fine, if i copy moment.js full code within ScriptBody. I did that just to check the logic. and it worked.
  3. as moment.js is a pretty big file, i thought to save it on unix box (nifi server) and refer it from "Module Directory".
  4. but now, it seems my code is not able to understand what "moment" is. I am getting error as => "moment" is not defined

enter image description here

enter image description here

import moment from '/kylo/script_files/depletions/moment-with-locales.min.js';
var flowFile = session.get();
if (flowFile != null) {
    try {
        var YYYYMMDD = flowFile.getAttribute('YYYYMMDD')
        var day = flowFile.getAttribute('firstDayOfWeekSundayorMonday')
        if (moment(YYYYMMDD, "YYYYMMDD").isValid()) {
            var startWeekAsSunday = true; //sunday
            if (day !== null && day !== '' && day.trim().toUpperCase() === 'MONDAY') {
                startWeekAsSunday = false; //monday
            }
            var date = moment(YYYYMMDD).format("YYYYMMDD");
            var startOfWeek = moment(date).startOf(startWeekAsSunday ? 'week' : 'isoWeek').format('YYYYMMDD');
            var endOfWeek = moment(date).endOf(startWeekAsSunday ? 'week' : 'isoWeek').format('YYYYMMDD');
            flowFile = session.putAttribute(flowFile, 'startOfWeek',startOfWeek)
            flowFile = session.putAttribute(flowFile, 'endOfWeek',endOfWeek)
        } else {
            throw "invalid date " + YYMMDD;    
        }
        session.transfer(flowFile, REL_SUCCESS)
    } catch (err) {
        flowFile = session.putAttribute(flowFile, 'errorMessage', err.message)
        session.transfer(flowFile, REL_FAILURE)
    }
}

Upvotes: 0

Views: 410

Answers (2)

Rakesh Prasad
Rakesh Prasad

Reputation: 642

able to figure out. It seems import work differently with "nashorn", which Nifi is using to run javascript.

so rather than using ECMA way of import, i used load() method of nashorn

load("/kylo/script_files/depletions/moment/moment-with-locales.min.js");
var flowFile = session.get();
if (flowFile != null) {
    try {
        var YYYYMMDD = flowFile.getAttribute('YYYYMMDD')
        var day = flowFile.getAttribute('firstDayOfWeekSundayorMonday')
        if (moment(YYYYMMDD, "YYYYMMDD").isValid()) {
            var startWeekAsSunday = true; //sunday
            if (day !== null && day !== '' && day.trim().toUpperCase() === 'MONDAY') {
                startWeekAsSunday = false; //monday
            }
            var date = moment(YYYYMMDD).format("YYYYMMDD");
            var startOfWeek = moment(date).startOf(startWeekAsSunday ? 'week' : 'isoWeek').format('YYYYMMDD');
            var endOfWeek = moment(date).endOf(startWeekAsSunday ? 'week' : 'isoWeek').format('YYYYMMDD');
            flowFile = session.putAttribute(flowFile, 'startOfWeek',startOfWeek)
            flowFile = session.putAttribute(flowFile, 'endOfWeek',endOfWeek)
        } else {
            throw "invalid date " + YYMMDD;    
        }
        session.transfer(flowFile, REL_SUCCESS)
    } catch (err) {
        flowFile = session.putAttribute(flowFile, 'errorMessage', err.message)
        session.transfer(flowFile, REL_FAILURE)
    }
}

now everything is working as expected. now i dont even need "Module Directory" attribute.

Upvotes: 4

Andy
Andy

Reputation: 14194

I don't see anywhere in your script body where you import moment. You'll have to explicitly import the external module if the code itself isn't in the script body.

Upvotes: 0

Related Questions