Node-RED in Bluemix - current working directory?

605 views
Skip to first unread message

jo2

unread,
May 7, 2015, 5:51:31 AM5/7/15
to node...@googlegroups.com
Hi!

I am trying to read a file (using fs.readFileSync) from a function node in Node-Red in Bluemix.

Since it seems impossible to get hold of the Node.js global process object, I cannot use process.cwd(),
or globals __dirname / __filename.

So I wonder where to put a file in the directory structure to find it from a function node.

Cheers
-jo

jo2

unread,
May 7, 2015, 6:01:47 AM5/7/15
to node...@googlegroups.com
As of now, I have tried the main directory (of the Bluemix application), and the "nodes" and "public" subdirectories...

Nicholas O'Leary

unread,
May 7, 2015, 11:00:15 AM5/7/15
to Node-RED Mailing LIst
Hi Jo,

is this just trying to read a local file? By which I mean, are you planning to write to it as well? The local filesystem isn't persisted across app restart - which is why we use cloudant for persistent storage on bluemix. But if this is trying to read a file that was part of the app you cf pushed into bluemix, then it will be doable, once you figure out the path information.

Off the top of my head, I don't know what path the app gets installed to. Poking around one of my instances, it appears to get installed to ~/app/

You could add an entry to functionGlobalContext within bluemix-settings.js to set the directory name for the Function node to use. You must have already added the fs module to global context to access it from the function node, so you'd have something like:

    functionGlobalContext: {
    fs: require("fs"),
    myDir: __dirname
  },

Then, in the function node:
    
    context.global.fs.readFileSync(context.global.myDir+"/filename");

Nick


On 7 May 2015 at 11:01, jo2 <jo.to...@gmail.com> wrote:
As of now, I have tried the main directory (of the Bluemix application), and the "nodes" and "public" subdirectories...

--
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.

jo2

unread,
May 7, 2015, 4:04:39 PM5/7/15
to node...@googlegroups.com

Hi Nick

Yes - the file(s) are included in the app pushed to Bluemix. I do not use cf for this app, I use IBM DevOps. And the file(s), in this case, is only for reading in.

Good tip with getting __dirname from functionGlobalContext.

I did that, and also added the HOME and PWD "deployment" environment variables.

All 3 has same value:

/home/vcap/app

I have tried to put the file in that directory, in the app structure, and also /app.

But fs.readSync does not find it. 

I do not know the Bluemix deployment process / structure well, and kinda hard to find details on this in the Bluemix documentation.
I will try to look more into it, maybe contact Bluemix support. In general it would be handy to know this, for other applications as well...

Cheers
-jo2

Nicholas O'Leary

unread,
May 7, 2015, 4:06:48 PM5/7/15
to Node-RED Mailing LIst
You could always put a fs.readdirSync in to explore the filesystem.... a bit brute force, but will help you find things.

Nick

jo2

unread,
May 7, 2015, 4:27:23 PM5/7/15
to node...@googlegroups.com

He he, right on! - I just did and voila - readdirSync on the __dirname (/home/vcap/app) lists the files in the root of my app structure.
And - of course - it was (almost) the only directory where the file I want to read was not added. I had it there initially, but then
tried the readFileSync without the HOME / __dirname path.

So now it seems to work just smooth,  thanks a lot for the help Nick ! :-)

Cheers
-jo
Reply all
Reply to author
Forward
0 new messages