Paths for developing modules in node-red

1,130 views
Skip to first unread message

nzfarmer

unread,
Feb 5, 2015, 9:07:07 PM2/5/15
to node...@googlegroups.com
What is best tree layout for developing node-red modules?  

Here's a scenario: we have two nodes we are developing under a tree.

i.e.  $HOME/dev/nodes/nodeA and nodeB

This is of course under GIT version control.

Then we have node-red installed located say:

/usr/local/etc/node_modules/node-red

Up until now we've been hard linking the contents of individual .js and .html files into  /usr/local/etc/node_modules/node-red/nodes/nodeA/xx-nodeA.(js|html)

However, it is not ideal as:

A) it involves creating duplicate folders and hard linking individual files. ( we tried symlinking $HOME/dev/nodes/nodeA but no luck)
B) those links can get broken with git pulls.

So, in search of a better approach I tried using the nodesDir parameter.

However, 

A) We had to recreate a bunch of nodes in a sub directory below $HOME/dev/nodes/nodeA
B) We had to change the requires in our code to explicitly reference the actual red/events.js being invoked
C) We'll have to create .git/excludes for the node folder created below nodeA

It sort of works but we haven't yet tested integration with nodeB

In short, wondered if there was a best practice for node-red node development in terms of path structures.

thanks






nzfarmer

unread,
Feb 5, 2015, 9:26:18 PM2/5/15
to node...@googlegroups.com
Further to this, we can of course add links into our project folder and exclude them in git so ../../red/events  now works.

So am guessing this is the way to go but I'd like to hear from others.

nzfarmer

unread,
Feb 6, 2015, 1:51:22 AM2/6/15
to node...@googlegroups.com
So to summarise what worked:

- create package.json but exclude the dependency on node-red
- link red to ../../ so that require("../../red/events") works
- run npm link in each of your module's folder
- configure nodesDir to point to your dev directory's 'nodes' folder
- configure .git/info/excludes to add the red and node_modules folder (and any *.tgz if required)





Julian Knight

unread,
Feb 6, 2015, 3:27:50 AM2/6/15
to node...@googlegroups.com
Hi, I think you may be overthinking this?

For my own contribution (node-red-contrib-moment), I simply created a new folder and copied one of the other similar contributions. The root of that contains the package.json as normal with a sub-folder containing the .js and .html

AFAIK, the package.json contains some additional entries that tells NR what is available and where.

To use it, I simply npm installed'd the contribution direct from github until I was ready to publish the npm. Once you have installed it, you can make changes to the installed version and push the changes back to github or carry on developing in your original folders as you choose.

Dave C-J

unread,
Feb 6, 2015, 3:37:22 AM2/6/15
to node...@googlegroups.com
hmmm -  all I do is link the directories not the files
so under  node-red/nodes
      ln -s ~/dev/nodes ./dev

Node-RED should scan those directly  which should be all you need for most cases....
what/why are you pointing directly at ../../red/events ?

Dave C-J

unread,
Feb 6, 2015, 3:48:46 AM2/6/15
to node...@googlegroups.com
As Julian points out the package.json is only required for when you want to install from elsewhere like Github, not needed for dev.
(At which point you should add a README.md and a LICENSE file also...)

to test installs (once you have got the node(s) working) the simplest way is to go into the directory and do an
    npm pack
which will give you a tgz file. Then unlink the nodes directory - (see previous notes) - so that Node_RED can't see them and then go back to the node-red directory and do a 
    npm install {path to yourfile}/{yourfile}.tgz

PS - you can of course put multiple nodes into a single package.json / tgz / npm if it makes sense for them all to be together...

nzfarmer

unread,
Feb 6, 2015, 5:07:57 PM2/6/15
to node...@googlegroups.com

Node-RED should scan those directly  which should be all you need for most cases....
what/why are you pointing directly at ../../red/events ?


Hi Dave

I just copied the example code for this that showed the ../../red/events require.   Is your code listening to any dedicated red events like nodes-started? 

When I used a symbolic link to the directories things started breaking and I initially resorted to hard linking individual files but the above approach now works.

Nicholas O'Leary

unread,
Feb 6, 2015, 5:14:08 PM2/6/15
to node...@googlegroups.com
Hi,

which example code are you copying from - it may be quite out of date.

Nodes should not 'require' the runtime code directly. If you look at the example node (https://github.com/node-red/node-red/blob/master/nodes/99-sample.js.demo#L22), you can see it should export a single function that will get called by the runtime when it is loaded, passing in the RED object. This object gives you access to the node-red api - for example RED.events.

Nick

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

nzfarmer

unread,
Feb 6, 2015, 6:39:15 PM2/6/15
to node...@googlegroups.com
Look at the red code - i.e. flows.js

If you have a better way of implementing:

        events.on("nodes-started",function(){
           // Add Logic here
        });

let me know!
        

Nicholas O'Leary

unread,
Feb 6, 2015, 6:55:47 PM2/6/15
to node...@googlegroups.com

As I said, you can access the internal events module as RED.events.

    module.exports = function(RED) {
        
        RED.events.on("nodes-started", function () {
            // ...
        });

    }

What are you using this for? With the new deployment modes I wrote about on the list a couple weeks ago, the event doesn't get emitted unless a full deployment is done.

Nick


nzfarmer

unread,
Feb 6, 2015, 7:17:00 PM2/6/15
to node...@googlegroups.com
Thanks Nick

Essentially I was just looking for a good place to hang our boot up code.  Perhaps it is not necessary at all.

nzfarmer

unread,
Feb 6, 2015, 7:24:15 PM2/6/15
to node...@googlegroups.com
Further to this, I've removed the events trigger and all works fine.  You may recall Nick I was searching for method of establishing when level db was going to become available.

nodes-started snuck into my code then and hadn't been removed.   

There may still be a bigger question around knowing when all nodes are loaded and ready to respond ....

thanks again.

nzfarmer

unread,
Mar 22, 2015, 9:41:02 PM3/22/15
to node...@googlegroups.com
Hmm.  With these versions: 

23 Mar 01:32:20 - [info] Node-RED version: v0.10.4.git
23 Mar 01:32:20 - [info] Node.js  version: v0.10.28

I now see something different.

I have 

/usr/local/node-red

and  say

/usr/local/mynodes

In setting I set 
 nodesDir: '/usr/local/mynodes,


All the dependencies of packages under /usr/local/mynodes have been installed under /usr/local/node-red/node_modules

However, it seems I have to install them under /usr/local/mynodes - even when running node red.sh from /usr/local/node-red

Have tried setting NODE_PATH but no joy.

Is it just me or are others having issues with locating nodes?

thanks








Nicholas O'Leary

unread,
Mar 23, 2015, 6:39:44 AM3/23/15
to Node-RED Mailing LIst
Hi,

I believe you've always had to install dependencies of external nodes relative to where they are located.

I have just installed 0.9.0, and did the following test:

- Node-RED installed at: /tmp/test/node_modules/node-red
- nodesDir set to "/home/nol/.node-red/nodes" <- somewhere that is nowhere near the NR install dir so the node paths don't overlap
- put the mysql node files in to nodesDir - this node requires the 'mysql' module.
- run npm install mysql from the Node-RED install directory (/tmp/test/node_modules/node-red/node_modules/mysql now exists)

Now, when I run "node red.js" from in the NR install directory, the mysql node fails to find the mysql node. In fact, it doesn't matter where I run from, it doesn't find the module.

Only when I npm install mysql in "/home/nol/.node-red/" does it work.


Would be good to understand more on how you had it setup and how you were running.


Nick



nzfarmer

unread,
Mar 24, 2015, 4:50:09 PM3/24/15
to node...@googlegroups.com


"I believe you've always had to install dependencies of external nodes relative to where they are located."

Ahah. Now there's a gem of wisdom.  Will assume this limitation and install locally. 

thanks Nick.
Reply all
Reply to author
Forward
0 new messages