Search code examples
javascriptnode-redmonaco-editor

How can I add a module as a global const in the monaco editor in a Node-RED contrib package?


I have build a framework for an industrial protocol I often use and now wanted to create a Node-RED package from that. That worked like a charm but I thought it would be nice if user could write their own custom server with my framework. As I do not want to publish that publicly to npm, I used a lot of effort to make it installable for the Node-RED package but not to anyone typing "npm install x-framework". So to make it usable I thought of the function node in Node-RED.

I looked into how the function node exposes the globals and installs modules and basically copied that for now.

If what I understood is correct, there is the node js vm module which is used to create a sandbox where the scripts are executed and also the Node-RED modified version of the monaco (or ace, but I want to only use the monaco editor).

I already inserted my framework into the sandbox and the code inside the "function" node runs just fine.

My problem now is the typing and intellisense. In the editor the framework const is underlined in red and there is no auto completion or anything like that.

I have added the name of the framework to "buildEditor" in the node.html file of the "function" node to the global object and set that to true like the other exposed and changed functions/objects. But that didnt change anything.

Then I tried to just let Node-RED do the work and just added the frameworks npm name to the "getLibsList" function in the same file if it is not already installed and that solves half of the problem after the edit window of the node in Node-RED is closed and reopened. It does not get underlined anymore. But still no typing or other intellisense features.

As we are not in a module in the scope of the vm sandbox script, we cannot import and we also cannot require anything. But I tried that anyway.

The framework is in a scope so my regular npm install name looks something like this: "@scope/x-framework"

To let Node-RED be able to install the framework, I had to escape a backkslash in getLibList. So it currently looks something like this: {module: "@scope\\/x-framework", var: "xFramework"}

Let me know if you need any more information and thanks in advance :D


Solution

  • I did not solve the problem of the question but solved my original problem by just distributing Docker images where the framework is already installed in the .node-red folder and placed into the global scope of Node-RED in the settings.json file upon building the image. At the end of the build, I just remove any tokens used to accessing private repositories and this gets it done.

    Looking at this question now, it seems like a XY-problem, so sorry for that... Would be still interesting how to do something like this within the monaco editor but this can wait for another use case, I guess.