fs.writeFileSync is not working in cloud functions [closed]
I am facing an issue in google cloud function while executing the following code
const fs = require('fs');
let data = "Test";
fs.writeFileSync("text.txt", data);
console.log (fs.readFileSync("text.txt", "utf8"));
Result: EROFS: read-only file system, open 'text.txt'
Solution 1:
Although it's not recommended to use local file system in Cloud Function there is a possibility to use /tmp
folder. So you can only write file to this folder.
Documentation mentions few things to remember using temporary directory
The only writeable part of the filesystem is the /tmp directory, which you can use to store temporary files in a function instance. This is a local disk mount point known as a "tmpfs" volume in which data written to the volume is stored in memory. Note that it will consume memory resources provisioned for the function.
And there is Tips & Tricks well to delete the temporary files as it may cause some future problems:
Local disk storage in the temporary directory is an in-memory filesystem. Files that you write consume memory available to your function, and sometimes persist between invocations. Failing to explicitly delete these files may eventually lead to an out-of-memory error and a subsequent cold start.