Access file absolute path in Serverless Custom App

Hello Everyone! Hope you all are good.

I’m developing a serverless custom app in freshsales suite and in there, I need to send logs to Google Cloud Logging, for this I’m using the Cloud Logging Library for Nodejs.

The issue is, to use this library outside of Google Cloud Plataform a need to use an API key (provided by GCP), that consists of a json file, in order authenticate the requests.

To inicialize the client I need to provide the API key path:

image

That variable filePath is defined as follows:

image

When I test locally, it works as expected, the logs are created, but when I deploy the app in the crm, the logs are not created in GCP, and the filePath returns /var/task/developer/server/lib/config/APIKEY.json. This is probably not the right path to the file…

So, how can I access this file correctly in the serverless custom app?

Thanks!

@Matheus_Souza_Silva

What helped you conclude —

The path that is resolved is the wrong path?

Can you try using require() to load the JSON file with a relative file path? For example, if the JSON file is in ./server/lib/config/APIKEY.json and you are accessing it from ./server/server.js, then use:

const credentials = require("./lib/config/APIKEY.json");
1 Like

@Saif I think it is the wrong path because the log wasn’t created in GCP, if I run the app locally, the log is created with the file path provided by my computer. The only difference of the app running locally and in production is the file path, so that is why I think the path that process.cwd() provides when running in production is invalid.

Do you know the absolute path of the server folder when the custom app is deployed? It would help immensely.

@kaustavdm When I try using require(), the following error occurs:

Error: Cannot find module "./config/APIKEY.json"

I think this happens because APIKEY.json is not a module, it’s just a json file. I need to provide the file path itself.

One other thing I forgot to mention, is that I use this library in a separate file, I export the functions that I want and import in server.js using require(./lib/logger.js), in there I need to access my credentials file path. The logger.js file is located in the lib folder, but I think this doesn’t change much what I’ve already said :laughing:.

Just for clarification, process.cwd() provides var/task/developer, I want to be able to access the server folder.

1 Like

I have the same intuition as @kaustavdm in this case. I hoped require(..) might have worked.

Requesting help from @App-Platform-Squad

Looks like @Matheus_Souza_Silva is trying to get data in a credentials.json file into server.js at runtime. However, process.cwd() in production resolves differently to what’s in local, and using require(..) doesn’t treat credentials.json as a JSON. Any alternative suggestions?

1 Like

This is curious. I suspect it has something to do with how Lambda works. I also wonder if our platform runtime overloads the require() function. Let me try to see if I can make it work.

1 Like

Hey @Matheus_Souza_Silva

I would not rely on absolute paths within a serverless app. For security reasons, we will block access to the filesystem within a serverless execution. Do you mind trying with a relative path instead?

Meanwhile, I think we have other example apps that might be trying to do this. I will request @Saif to check them out and get back to you.

1 Like

Hello everyone!

I’ve tested with a relative path but, it didn’t work…

Any updates @satwik?