Seeking comments from the team on node-red intended use/design.
Version used: 4.0.2
Node-runtime - 20.14.0 & 18.x
os: windows, linux
Steps:
- Setup a new nodejs project and install node-red as dependency
- Author a custom nr node inside project - location
nodes/cust-node1
. Files supplied further below. (Assets 1 & 2) - Added corresponding section in package.json for node-red to discover custom node. (Asset # 3)
- Start node red using npx. (
npx node-red
)
Asset # 1 - cust-node1.js
module.exports = function(RED) {
let delayArray = [5, 10, 12];
let delayLength = delayArray.length;
function CustNode1(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
let idx = Math.floor(Math.random() * delayLength);
let timeoutSeconds = delayArray[idx];
setTimeout(() => {
let payload = { timeoutSeconds };
msg.payload = { ...msg.payload, ...payload };
node.send(msg);
}, timeoutSeconds * 1000);
});
}
console.log('Node registered');
RED.nodes.registerType("cust-node1",CustNode1);
}
Asset # 2 - cust-node1.html
<script type="text/javascript">
RED.nodes.registerType('cust-node1',{
category: 'function',
color: '#a6bbcf',
defaults: {
name: {value:""}
},
inputs: 1,
outputs: 1,
// icon: "file.svg",
label: function() {
return this.name||"cust-node1";
}
});
</script>
<script type="text/html" data-template-name="cust-node1">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/html" data-help-name="cust-node1">
<p>A simple node that adds a random delay in the flow to simulate something async</p>
</script>
Asset #3 - snippet that should go into package.json
"node-red" : {
"nodes": {
"cust-node1": "nodes/cust-node1/cust-node1.js"
}
}
On starting node-red in manner mentioned above, the custom node does not get registered.
To workaround this issue I did the following:
- copied
package.json
into thenode_modules
folder - modified the
"cust-node1": "nodes/cust-node1/cust-node1.js"
line in the package.json file to"cust-node1": "../nodes/cust-node1/cust-node1.js"
- Started node-red in the manner above (using npx).
Narrowed down the issue to here: node-red/packages/node_modules/@node-red/registry/lib/localfilesystem.js at 4.0.2 · node-red/node-red · GitHub
In this loop it misses checking out on the project's package.json
I don't understand why it was written that way.
&