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

173
Views
(JavaScript) data.split('\n') is not a function

I am trying to make a random string picker with a text document that splits at every line.

However, when I try to run it, it says that data.split is not a function.

fs.readFile('list.txt', function(err, data){
        if(err) throw err;
        var lines = data.split('\n');
        var rand = [Math.floor(Math.random()*lines.length)];
        var rlist = lines[rand]
})
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As stated in the documentation:

The callback is passed two arguments (err, data), where data is the contents of the file. If no encoding is specified, then the raw buffer is returned.

A raw buffer is just an array, and JavaScript arrays don't have the split function. So you need to specify an encoding to get the text result.

fs.readFile('list.txt', 'utf8', ...)
over 4 years ago · Santiago Trujillo 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!