PedroG333 Productios
PedroG333 Productios

Reputation: 35

Create a file in the Cloud Functions / temp directory

I'm trying to create a file that stores room information so I can use it in other functions

I have the following code

const fs = require('fs')
const { tmpdir } = require('os');
fs.writeFileSync(`${tmpdir}/room-keys.tmp`, `${room.key}\n`, (err) => {
        if (err) throw err
        functions.logger.info(`New room registered, ${JSON.stringify(room)}`)
     })

Every time I try to create a file, it returns an error saying that the directory does not exist

Upvotes: 1

Views: 1151

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317487

Firstly, you're not using the os.tmpdir import correctly. If you want to generate a path to the temp directory, you would do it like this:

const os = require('os');
const path = require('path');
const tmp = os.tmpdir();
const file = path.join(tmp, "file.ext");

Secondly, you should not expect that the temp directory will be present for future function invocations. It is also not shared at all between different deployed functions. It should only be used during the processing of a single request, and deleted before the function ends. It is a memory-based filesystem, so writing files there consumes memory.

Upvotes: 3

Related Questions