Importing files in Cloud Code

How can I import another file in Cloud Code?

I am seeking to use a test json file that I created but cannot access it via the fs library.

var file = fs.readFileSync('test.json', 'utf8');

This does not work. Is there a work around or something I am missing?

To read a file in the directory of a CloudCode task you need to include the __dirname in the path e.g.

const jsonFile = fs.readFileSync(__dirname + "/test.json", 'utf8');

This will not work if you want to read a file from another CloudCode task.

Unfortunately, this is not working for me. I’ve tried the following:

const file = fs.readFileSync('./test.json', 'utf8');
const file = fs.readFileSync('cloudcode/ccTask/test.json', 'utf8');

I am not sure the issue. I also tried creating a folder with the test file inside of the CC Task but that did not seem to recognize the file either. I keep receiving the error:

Error: ENOENT: no such file or directory

@alex-valdez I believe CloudCode tasks are sandboxed for security reasons. If you need to share code between tasks, you can use “shared” tasks (see the “Shared” option when OXIDE prompts you for a CloudCode template).

More info: JourneyApps Documentation

Hope this helps?

Unfortunately, this does not seem to be working for me. It cannot find the test json file. It is able to see the shared auto-generated index file but not the test json file.

In the case you are reading a JSON file you can use a require statement e.g.

const testFile = require('../shared/test.json');

export async function run() {