loading big txt files through node.script

Stevie J. Sutanto's icon

Hi all!

I'm trying to load a 75mb txt file into node.script, here is my code:

const fs = require('fs');
const max = require('max-api');
let input = fs.readFileSync('training_data/orbit[input].txt', 'utf8');


However, I got an error message: Max API Message request timed out.
It worked with smaller files though. Is someone else experiencing the same and can help me through?

Many thanks,
Stevie

Florian Demmer's icon

Hey Stevie.

Are you doing any other calls to max or sending messages from Max to your node.script that could cause that error?

In general I'd advise to use the asynchronous calls, especially when dealing with files at this size, as this won' block Node's event loop and your script will still be able to handle any other requests etc that happen in the meantime.

For your case that could look something like this:

fs.readFile('training_data/orbit[input].txt', 'utf8', input => {
    // handle input here
});

Another alternative if you are not keen on the callback style would be to turn readFile into an async, promise-style function. Here an idea how this could look like:

const { readFile } = require("fs");
const { promisify } = require("util");
const max = require("max-api");

const readFileAsync = promisify(readFile);

const main = async () => {
    const input = await readFileAsync("training_data/orbit_[input].text", "utf8");
    // handle input
};

main();

Hope that helps.
Florian

Stevie J. Sutanto's icon

Hey Florian.

Thank you for the answer! I changed it to asynchronous call and it worked fine.

Best wishes,
Stevie