Project directory path

Can a node-red flow from a node-red project discover the path to the directory where the project is checked out?

I want my flow to call a custom shell script using the exec node and I would prefer to store the shell skript in the same git repository as the node-red project that uses it.

Right now I have the exec node configured to call /home/pi/.node-red/projects/nodered-MyProject/skripts/send_sms.sh, but if I were to migrate the project in the future to a different computer that is not a Raspberry Pi, the /home/pi/.node-red/projects/nodered-MyProject part of the path needs to be replaced with the directory where the git project is checked out on the new computer. Can this part be replaced dynamically with the correct directory?

1 Like

I'm new here so please take my postings as loose thoughts.

What about using environment variables to provide run-time configuration information? See for example:

https://nodered.org/docs/user-guide/environment-variables

Maybe your flow could consult the environment variable to find the path to be used for the script execution?

Yes, I could adjust environment variables, or I could just copy the skript into a directory on the PATH, or modify the PATH for the node-red process.

But I want to avoid this additional step if possible: I am creating my home automation and I want to prepare for a migration at some point in the future when I have forgotten about all the additional steps that are required for my setup after checking out the project and one npm install. I want to minimize the additional steps.

node-red already knows the location of that directory. It loads the flows from there. The web interface detects when I manually change files there. I want to know if this information can be accessed from a node in the flow.

The information is not exposed in any way your flow can make use of it.

You'll have to make do with env vars you setup yourself.

But I can see the value in what you are trying to do, so we should look at how best to address this in the code.

5 Likes

Thanks for the info and for the possible future consideration.

1 Like

It is retrievable from a custom node of course. I do that in uibuilder. So you could always write yourself a little node to get hold of it.

Possibly a little overkill though :smile:

Just brainstorming here, but can’t you set a global function in the same way as you would when making JavaScript libraries available to function nodes (in the settings file), and have that function resolve the path? Then you can retrieve that location via a function node to pass to the correct output.

I think that the issue is that the API for retrieving the current project name is buried in RED and is only accessible to Nodes.

In a custom node, you would do something like:

const proj = RED.settings.get('projects')
console.log(proj.activeProject)
const projFolder = path.join(RED.settings.userDir, 'projects', proj)

Might not be exactly right because I collect some of the data separately in uibuilder and I have a utility function that delves into objects that may or may not exist.

Based on the code that I found in your node I have created a new node node-red-contrib-projectdir that extracts the information from node-red settings and inserts it into msg in the flow: node-red-contrib-projectdir. The palette manager has not yet picked it up but it can be installed already with npm.

Comments welcome!

4 Likes

Nice, just remember to deprecate it when Node-RED core includes something.

2 Likes