ConvertCSV

How to Read a CSV File in JavaScript (Browser and Node.js)

By Convert CSV Editorial TeamLast updated August 1, 2026

Read CSV files in JavaScript reliably. Use FileReader in the browser, streams in Node.js, and CSV libraries that respect quoting and encoding.

Why Not Just Split on Commas?

A naive text.split(",") breaks the moment a field contains a comma inside quotes, a line break inside a description, or an escaped double quote. Real CSV parsing respects quoting rules described in RFC 4180.

Use a small library or the browser’s built-in APIs instead of hand-rolling a parser. Your future self will thank you.

Where You Read the CSV Matters

The right approach depends on where the code runs.

EnvironmentRead strategy
Browser file inputFileReader / File API + a CSV parser
Browser fetch (URL)fetch → text → CSV parser
Node.js small filesfs.readFileSync + parser
Node.js large filesstreaming parser (csv-parse, papaparse worker)
ServerlessDepends on runtime; treat as Node.js

Step-by-Step: Read CSV in the Browser

This works for user-uploaded files and small fetched files.

  • Add an <input type="file" accept=".csv"> to the page.
  • Read the file as text (respect UTF-8).
  • Pass the text to a CSV parser.
  • Iterate rows or turn them into objects with headers.
  • Preview or upload the parsed rows.

Vanilla browser example

Uses FileReader + a small parser (Papa Parse example).

<input id="csv" type="file" accept=".csv" />
<script src="https://unpkg.com/papaparse@5.4.1/papaparse.min.js"></script>
<script>
  document.getElementById("csv").addEventListener("change", (e) => {
    const file = e.target.files[0];
    Papa.parse(file, {
      header: true,
      skipEmptyLines: true,
      encoding: "UTF-8",
      complete: (result) => {
        console.log(result.data);
      },
    });
  });
</script>

Fetch a CSV from a URL

Handle CORS and encoding explicitly.

const res = await fetch("/data/orders.csv");
const text = await res.text();
const result = Papa.parse(text, { header: true, skipEmptyLines: true });
console.log(result.data.slice(0, 5));

Step-by-Step: Read CSV in Node.js

For anything larger than a small config file, use a streaming parser.

  • Install a CSV library (csv-parse, papaparse, fast-csv).
  • Read the file as a stream to keep memory low.
  • Pipe through the CSV parser.
  • Consume rows as they arrive.

csv-parse streaming example

Efficient for large files.

import fs from "node:fs";
import { parse } from "csv-parse";

fs.createReadStream("orders.csv")
  .pipe(parse({ columns: true, skip_empty_lines: true }))
  .on("data", (row) => {
    // process one row at a time
  })
  .on("end", () => {
    console.log("done");
  });

Sync parse for small files

Simple and readable when files are small.

import fs from "node:fs";
import { parse } from "csv-parse/sync";

const text = fs.readFileSync("small.csv", "utf8");
const rows = parse(text, { columns: true, skip_empty_lines: true });
console.log(rows[0]);

Handling Encoding

UTF-8 is the default assumption. When you know the file is a legacy encoding (Windows-1252, etc.), decode bytes explicitly instead of relying on the browser default.

const buffer = await file.arrayBuffer();
const text = new TextDecoder("windows-1252").decode(buffer);
const rows = Papa.parse(text, { header: true }).data;

Real-World Examples

These are common patterns from real apps.

Upload → preview grid

A SaaS onboarding flow accepts a CSV, parses in the browser, and shows a preview grid so users can confirm before upload.

CLI report

A Node.js script streams a nightly export, filters rows, and writes a summary CSV.

Serverless API

An HTTP endpoint accepts a CSV upload, parses it, validates, and returns JSON errors to the client.

Common Mistakes

Skip these to keep parsing robust.

  • Using text.split(",") for anything beyond toy inputs.
  • Assuming the browser will pick the right encoding.
  • Reading a 500 MB CSV into memory instead of streaming.
  • Ignoring the BOM at the start of a UTF-8 file.
  • Treating every value as a number—keep IDs as strings.

Best Practices

Small habits pay off.

  • Prefer a library over a regex.
  • Stream large files.
  • Preserve string types for IDs.
  • Handle encoding explicitly.
  • Validate row shape and required columns.
  • For quick previews, the Online CSV Editor is faster than writing throwaway code.

Why Use Convert CSV Online?

Sometimes you just want to see the CSV without writing code. Convert CSV Online is free, browser-based, and requires no account for everyday conversions. Preview in the Online CSV Editor, or convert to JSON/Excel/SQL directly. Client-side workflows work on Windows, macOS, and Linux browsers.

Verify before you code

Preview the file online to confirm structure and encoding, then wire up your JavaScript parser against a known-good CSV.

Conclusion

Reading CSV in JavaScript is a two-liner with the right library and a lot of pain without one. Choose FileReader + a parser in the browser and streams in Node.js.

FAQ

How do I read a CSV file in JavaScript?

Use the browser File/FileReader API or fetch to get the text, then parse it with a CSV library like Papa Parse or csv-parse. Do not split on commas manually.

What is the best CSV library for JavaScript?

Popular options include Papa Parse (browser and Node), csv-parse (Node), and fast-csv (Node). Choose based on streaming and browser needs.

How do I handle large CSVs in Node.js?

Stream the file with fs.createReadStream and pipe it into a streaming CSV parser to keep memory usage low.

How do I read CSV from a URL?

Use fetch to get the response text, then feed it to a CSV parser. Handle CORS and encoding explicitly.

Do I need to handle UTF-8 BOM?

Some parsers strip the BOM automatically; others do not. Check the first header value if you see a stray character.

How do I preserve leading zeros in JS parsing?

Keep values as strings. Avoid Number() casting on ID and postal columns.

References

Convert your CSV in the browser

Preview, clean, and convert CSV files free with Convert CSV Online—no installation and no account required for everyday conversions.