Official or standard location to have a custom node save a file?

Official or standard location to have a custom node save a file? I seem some custom nodes create directories under ~/.node-red/? Is this legit?

There is no "official" location, you are free to do what you want. But yes, this is the root folder generally used for all instance based files. Makes it a lot easier for people to know where things are and what to back up.

My uibuilder node, for example, creates a uibuilder folder and keeps all of its configuration and front-end code files within it.

Of course, it is also good practice not to create unnecessary files and folders and to be careful with naming if you do create them - don't create a folder called test off the root for example as this may clash with things that the user wants to do. You will also want to double check everything in your runtime code, never assume that a file or folder is there (or still there) nor assume that it can be read/written nor that it isn't corrupt.

Whilst ~/.node-red is the default location for the node-red user data directory, you can't assume that it exists or is where the user has chosen for their data directory to be - they can override the default with the -u option.

In the runtime, you should find that RED.settings.userDir points at the right location - assuming they are using the local filesystem at all. In some environments, such as on the IBM Cloud, a different storage plugin is used that doesn't touch the filesystem.

3 Likes

Argh! Yes, I should have said that myself.

Nick, in this example (running on IBM cloud) what is the mechanism to use to determine this and what would a custom node use for persistent storage?

The Storage API doesn't provide a generic "please store this blob of data with this name" type of API. So in those environments, there is currently no good solution for nodes to use.

The original concept, which holds true for 99% of nodes, is they don't need to store anything other than what is held in the flows file which does get stored via the Storage API.

Thats fair enough Nick however, if a custom node has some option of persistence, what is the best way to identify this situation at runtime (i.e. no file system or cloud based) so that the node can hide (or limit) any of these feature?

So, if I do decide to create a folder/file under .node-red, I am not committing a horrible act, given that there is a lack of official option? I figure I would create a directory/folder that is clearly related to the node/package. For example cronplus creates a directory called cronplus, so it is obvious what owns it.

Yes, that is reasonable as long as you don't mind it not working under the circumstances that Nick outlines.

Yeah, since the custom nodes I am working on... a few are done, and I am seriously testing before I publish, will be Pi device oriented, should be fine for now, or initially.