Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

362
Views
How to create a file with a specific size with NodeJs

Doing several tests of files validation, I need a file with more than 250MB, but I don't want to store it in the repo.

I'm trying to create a file with NodeJs in time execution with several functions I've found, but they don't work for me.

This one creates the file, but it doesn't have the size I'm passing. Files have just 1 byte.

createFile(filePath, size) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    try {
                        const fd = fs.openSync(`${filePath}`, 'w');
                        //fs.writeSync(fd, 'ok', Math.max(0, size - 2));
                        fs.writeSync(fd, Buffer.alloc(1, 'ok', 'base64'), 0, 1,
                            //Math.max(0, size - 1)
                            size - 1
                        );
                        fs.closeSync(fd);
                        resolve(true);
                    } catch (error) {
                        reject(error);
                    }
                }, 0);
            });
        }

I'll appreciate any help

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Buffer.alloc(1...) may be the issue

When I said that, I meant that Buffer.alloc(someNumber) would be the length of your buffer, which is why the length of the file written to ended up as 1

I kept your return promise logic in the createFile function below

createFile(filePath, size) {
  return new Promise((work,fail)=>{
    fs.writeFile(filePath, Buffer.alloc(size), err=>err?fail(err):work())
    //fs.writeFile(desiredPath, desiredSize, (reject if error and resolve if none))
  })
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!