Skip to content

Build your personal knowledge base with Trilium Notes

commonjs modules should have "this" available

azivner763f885cc744
diff --git a/src/services/script.js b/src/services/script.js
index 47364fb978..d70e6cb829 100644
--- a/src/services/script.js
+++ b/src/services/script.js
@@ -127,15 +127,15 @@ async function getScriptBundle(note, root = true, scriptEnv = null, includedNote
if (note.isJavaScript()) {
bundle.script += `
apiContext.modules['${note.noteId}'] = {};
-${root ? 'return ' : ''}await (async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
+${root ? 'return ' : ''}await ((async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
try {
${note.content};
} catch (e) { throw new Error("Load of script note \\"${note.title}\\" (${note.noteId}) failed with: " + e.message); }
if (!module.exports) module.exports = {};
for (const exportKey in exports) module.exports[exportKey] = exports[exportKey];
-})({}, apiContext.modules['${note.noteId}'], apiContext.require(${JSON.stringify(moduleNoteIds)}), apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
- modules.map(mod => `apiContext.modules['${mod.noteId}'].exports`).join(', ') + `);
+}).call({}, {}, apiContext.modules['${note.noteId}'], apiContext.require(${JSON.stringify(moduleNoteIds)}), apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
+ modules.map(mod => `apiContext.modules['${mod.noteId}'].exports`).join(', ') + `));
`;
}
else if (note.isHtml()) {