TriliumNext / Notes

Build your personal knowledge base with TriliumNext Notes
GNU Affero General Public License v3.0
684 stars 33 forks source link

"require is not a function" when running a script note #433

Open riegera2412 opened 4 days ago

riegera2412 commented 4 days ago

TriliumNext Version

v0.90.4/ also checked on latest

What operating system are you using?

Other Linux

What is your setup?

Server access only

Operating System Version

Win1122H2

Description

Hey there,

since I am not an experienced software guy, I might be wrong about claiming this to be a bug, so I apologize in advance.

I just came to realize, that the scripts I used in my zadam/trilium docker container are no longer working since I migrated to triliumnext/notes. When I run them manually I get the following error message:

JS Error: 500 POST script/run/WxG3UAQove1b - Load of script note "agenda_js_backend" (WxG3UAQove1b) failed with: require is not defined

Stack: Error
    at Object.w [as logError] (https://notes.my-domain.tld/assets/v0.90.4/app-dist/desktop.js:2:159531)
    at Object.g [as throwError] (https://notes.my-domain.tld/assets/v0.90.4/app-dist/desktop.js:2:149421)
    at c (https://notes.my-domain.tld/assets/v0.90.4/app-dist/desktop.js:2:145614)
    at async Object.error (https://notes.my-domain.tld/assets/v0.90.4/app-dist/desktop.js:2:144370)

The script note itself looks like this

/*
    Instructions: Paste the below into a new JS Backend Script note. Set the note variables to the IDs of the notes you want to use for your categories.
    Remember to set #run=hourly as a label for this note.
    To configure date formats use https://day.js.org/docs/en/display/format
*/

var isBetween = require('dayjs/plugin/isBetween')
api.dayjs.extend(isBetween)

async function run_script() {

    // User Set Variables
    let overdueNote = "8b3rzwL67j0f"
    let todayNote = "mv2ABG0eoPJt"
    let thisWeekNote = "INybeVMsh0N2"
    let thisMonthNote = "hmniEeFHVZv8"
    let thisYearNote = "6Na3iQc5jFzA"
    let futureNote = "ygDb4oauwS9v"
    let dueDateLabel = "dueDate"
    let dueTimeLabel = "dueTime"
    let useNumberOfDays = true     // If set to true, intervals are determined based on number of days

    // Dynamic Variables
    let now = api.dayjs()
    let startOfToday = now.startOf("day")
    let endOfToday = now.endOf("day")
    let endOfThisWeek = useNumberOfDays ? startOfToday.add(7, "day") : now.endOf("week")
    let endOfThisMonth = useNumberOfDays ? startOfToday.add(30, "day") : now.endOf("month")
    let endOfThisYear = useNumberOfDays ? startOfToday.add(365, "day") : now.endOf("year")

    for (let note of api.getNotesWithLabel(dueDateLabel)){
        // Get due date and time
        let dueDatePresent = note.getLabelValue(dueDateLabel) ? true : false
        let dueDate = api.dayjs(note.getLabelValue(dueDateLabel))
        let dueTime = note.hasLabel(dueTimeLabel) ? note.getLabelValue(dueTimeLabel) : ""
        let dueDateString = ""

        // Set Overdue Notes
        let isOverdue = (dueDatePresent && dueDate.isBefore(startOfToday))
        dueDateString = dueDate.format("MMM D, YYYY")
        api.toggleNoteInParent(false, note.noteId, overdueNote)
        api.toggleNoteInParent(isOverdue, note.noteId, overdueNote, dueDateString)

        // Set Today Notes
        let isToday = (dueDatePresent && dueDate.isBetween(startOfToday, endOfToday, null, '[)'))
        dueDateString = dueTime
        api.toggleNoteInParent(false, note.noteId, todayNote)
        api.toggleNoteInParent(isToday, note.noteId, todayNote, dueDateString)

        // Set This Week Notes
        let isThisWeek = (dueDatePresent && dueDate.isBetween(endOfToday, endOfThisWeek, null, '[)'))
        dueDateString = dueDate.format("ddd")
        api.toggleNoteInParent(false, note.noteId, thisWeekNote)
        api.toggleNoteInParent(isThisWeek, note.noteId, thisWeekNote, dueDateString)

        // Set This Month Notes
        let isThisMonth = (dueDatePresent && dueDate.isBetween(endOfThisWeek, endOfThisMonth, null, '[)'))
        dueDateString = dueDate.format("D")
        api.toggleNoteInParent(false, note.noteId, thisMonthNote)
        api.toggleNoteInParent(isThisMonth, note.noteId, thisMonthNote, dueDateString)

        // Set This Year Notes
        let isThisYear = (dueDatePresent && dueDate.isBetween(endOfThisMonth, endOfThisYear, null, '[]'))
        dueDateString = dueDate.format("MMM D")
        api.toggleNoteInParent(false, note.noteId, thisYearNote)
        api.toggleNoteInParent(isThisYear, note.noteId, thisYearNote, dueDateString)

        // Set Future Notes
        let isFuture = (dueDatePresent && dueDate.isAfter(endOfThisYear))
        dueDateString = dueDate.format("YYYY")
        api.toggleNoteInParent(false, note.noteId, futureNote)
        api.toggleNoteInParent(isFuture, note.noteId, futureNote, dueDateString)

    }
}
run_script()

It is the agenda function found on awesome trilium if I recall correctly and it worked perfectly before. "agenda_js_backend" is the name of the note containing the script and "WxG3UAQove1b" is the notes id.

Any help or insight on how to resolve this is very much appreciated.

Error logs

No response

eliandoran commented 4 days ago

@riegera2412 , thank you for the report. Could be a change since we did quite a bit of refactoring on the code. I will test the script later this week and come back to you.