2

When I deploy my Cloud Function to GCP (written in Node.js), how can I see my file system environment for debugging purposes? What if I want to know what my current directory is or what files are present alongside my application?

Kolban
  • 13,794
  • 3
  • 38
  • 60

1 Answers1

2

When we deploy a Cloud Function, the full Node.js environment is present. We can run arbitrary Node.js logic within. This includes logging information which will then show in the Stackdriver logs. We can thus log our current working directory path as well as a list of all the files in our current directory. We can use this as a diagnostic aid. Here is an example:

const fs = require('fs');
exports.helloWorld = (req, res) => {
  console.log(`CWD: ${process.cwd()}`);
  fs.readdir('.', function (err, files) {
    if (err) {
        return console.log('Unable to scan directory: ' + err);
    } 
    files.forEach(function (file) {
        console.log(file); 
    });
    res.status(200).send('Done!');
  });
};

You can incorporate this logic in your own apps for testing.

And here is an alternate version which shows a recursive listing of all files and sub directories.

const fs = require('fs');
const walk = function(dir) {
  var results = [];
  var list = fs.readdirSync(dir);
  list.forEach(function(file) {
    file = dir + '/' + file;
    var stat = fs.statSync(file);
    if (stat && stat.isDirectory()) { 
      results = results.concat(walk(file));
    } else { 
      results.push(file);
    }
 });
 return results;
}

exports.helloWorld = (req, res) => {
  let message = req.query.message || req.body.message || 'Hello World!';
  console.log(`CWD: ${process.cwd()}`);
  console.log(`Dir Listing: ${walk('.')}`);
  res.status(200).send('Done!');
};

All credit to the above algorithm to node.js fs.readdir recursive directory search.

Kolban
  • 13,794
  • 3
  • 38
  • 60
  • 1
    Bonus points if you can make this recursive and show child directories and files. – John Hanley Dec 04 '19 at 02:02
  • 1
    Challenge accepted John :-) .... will build, test and update :-) – Kolban Dec 04 '19 at 03:11
  • The community needs a lot more examples like this. Demonstrates good implementation tips and helps to develop a better understanding of cloud services. – John Hanley Dec 04 '19 at 03:21
  • What is a practical application of this? The cwd should always be the root of the deployment, and the files in the deployment will be exactly what you deployed. There's not a whole lot of uncertainty here. I can't imagine why any code would change the cwd, and why a developer would be unaware of the contents of their deployment (since they are the ones who built the files to deploy). – Doug Stevenson Dec 04 '19 at 03:37
  • Howdy Doug, the thinking was some back-thought on this question ... https://stackoverflow.com/questions/59167484/how-to-pass-certificate-key-file-when-deploying-cloud-function – Kolban Dec 04 '19 at 04:28
  • I join @DougStevenson doubt: why this feature is interesting? The current directory is this one where the function is executed and the subfolders are copied as is. There is no surprised in the file organisation in the Cloud Function. Only the /tmp directory which is a bonus and in-memory tmpfs for local read/write – guillaume blaquiere Dec 04 '19 at 10:15
  • Howdy guys ... I'll be happy to delete this question/answer if we feel it is distracting. In a recent question ... that prompted this question ... a poster couldn't load a JSON file and wasn't sure where to put it ... he had tried to load it from "./myfile.json" and "lib/myfile.json" and neither worked. By providing this snippet, we gave him the opportunity to dump his files and say "Ahhh there it is". – Kolban Dec 04 '19 at 15:20
  • If you look at my answer to that question, you'll see that they just needed to know how to reference a file using a relative path. They were very much aware of the contents of their deployment. – Doug Stevenson Dec 04 '19 at 15:45
  • @DougStevenson - It is not that the contents of the Cloud Functions file system that is interesting. It is how to do this that mistifies newbies. The same techniques can be applied to other projects. When you build from small building blocks that are easy to get working, bigger projects become easier. Cloud Functions is a complete mystery to some developers. So is App Engine, Firebase, etc. – John Hanley Dec 05 '19 at 05:39