csv-parser、csvtojson、fast-csv、papaparse はいずれもCSVデータをJavaScriptオブジェクトやJSONに変換するためのnpmパッケージですが、対応環境やAPI設計、パフォーマンス特性に大きな違いがあります。csv-parser と csvtojson、fast-csv はNode.js専用で、ストリーム処理やファイル読み込みに特化しています。一方、papaparse はブラウザとNode.jsの両方で動作し、フロントエンド開発者にとって唯一の選択肢となっています。これらのライブラリは、データの読み込み方法、メモリ使用量、変換機能、エラーハンドリングなど、実際の開発現場で直面する課題に対して異なる解決策を提供しています。
WebアプリケーションでCSVファイルを扱う際、適切なパーサーを選ぶことはパフォーマンスや開発効率に大きく影響します。この記事では、代表的な4つのnpmパッケージ — csv-parser、csvtojson、fast-csv、papaparse — を実践的な観点から比較し、それぞれの強みと使いどころを明らかにします。
csv-parser はNode.js向けに設計されており、主にストリーム経由での処理を想定しています。ファイルパスやReadable Streamを直接扱えますが、ブラウザ環境では使えません。
// csv-parser: Node.js ストリーム処理
const csv = require('csv-parser');
const fs = require('fs');
fs.createReadStream('data.csv')
.pipe(csv())
.on('data', (row) => console.log(row));
csvtojson もNode.js専用ですが、文字列やファイルパス、Bufferなど多様な入力をサポートしています。特に文字列からの変換が簡単です。
// csvtojson: 文字列からの変換
const csv = require('csvtojson');
const csvString = 'name,age\nAlice,30\nBob,25';
const jsonArray = await csv().fromString(csvString);
console.log(jsonArray);
fast-csv はNode.js向けで、ストリームとPromiseの両方に対応しています。ファイル、文字列、配列など柔軟な入力を受け付けます。
// fast-csv: Promiseベースで文字列を処理
const { parse } = require('@fast-csv/parse');
const csvString = 'name,age\nAlice,30\nBob,25';
const rows = await parse(csvString).promise();
console.log(rows);
papaparse は唯一、フロントエンドとバックエンドの両方で動作します。Fileオブジェクト、Blob、文字列、URLなど、ブラウザ特有の入力にも対応しています。
// papaparse: ブラウザでFileオブジェクトを処理
Papa.parse(fileInput.files[0], {
header: true,
complete: (results) => console.log(results.data)
});
すべてのライブラリが「ヘッダー行あり」のCSVをオブジェクト配列に変換できますが、細かい挙動に違いがあります。
csv-parser はデフォルトでヘッダー行をキーとして使用し、各行をオブジェクトに変換します。
// csv-parser: 自動でヘッダーをキーに
fs.createReadStream('users.csv')
.pipe(csv())
.on('data', (row) => {
// row = { name: 'Alice', age: '30' }
});
csvtojson も同様ですが、{ noheader: false }(デフォルト)でヘッダーを使用します。ヘッダーなしCSVも簡単に扱えます。
// csvtojson: ヘッダーなしCSV
const jsonArray = await csv({ noheader: true }).fromString('Alice,30\nBob,25');
// 結果: [{ field1: 'Alice', field2: '30' }, ...]
fast-csv は明示的に{ headers: true }を指定する必要があります。
// fast-csv: ヘッダーを明示
const rows = await parse(csvString, { headers: true }).promise();
// rows = [{ name: 'Alice', age: '30' }, ...]
papaparse はheader: trueオプションでヘッダーを有効にします。
// papaparse: ヘッダー有効化
Papa.parse(csvString, {
header: true,
complete: (results) => console.log(results.data)
});
大規模CSVを処理する場合、メモリ使用量が重要になります。
csv-parser はストリーム専用で、巨大ファイルでも低メモリで処理可能です。ただし、同期的な全データ取得はできません。
csvtojson は内部でバッファリングを行うため、大きなファイルではメモリを大量に消費します。ストリーミングモードもありますが、APIがやや複雑です。
// csvtojson: ストリーミングモード
fs.createReadStream('big.csv')
.pipe(csv())
.on('data', (jsonObj) => console.log(jsonObj))
.on('end', () => console.log('done'));
fast-csv はストリームとPromiseの両方をサポートしており、必要に応じて選べます。ストリームを使えばメモリ効率が良いです。
papaparse はブラウザ向けに最適化されており、worker: trueオプションでWeb Workerを使って重い処理をバックグラウンドで実行できます。これによりUIが固まることを防げます。
// papaparse: Web Workerで非同期処理
Papa.parse(file, {
worker: true,
header: true,
complete: (results) => console.log(results.data)
});
データを単にパースするだけでなく、加工や検証が必要な場合があります。
csv-parser は.map()チェーンで変換できますが、機能は限定的です。
// csv-parser: 値の変換
fs.createReadStream('data.csv')
.pipe(csv())
.pipe(map((row) => ({ ...row, age: Number(row.age) })))
.on('data', console.log);
csvtojson はpreRawDataやpreFileLineなどのフックで柔軟な前処理が可能です。
// csvtojson: 行ごとの前処理
const jsonArray = await csv({
preFileLine: (line, idx) => idx === 0 ? line : line.toUpperCase()
}).fromString(csvString);
fast-csv はtransformオプションで各行を加工できます。
// fast-csv: transformで型変換
const rows = await parse(csvString, {
headers: true,
transform: (row) => ({ ...row, age: Number(row.age) })
}).promise();
papaparse はtransform関数で各セルの値を変更できます。
// papaparse: セル単位の変換
Papa.parse(csvString, {
header: true,
transform: (value, field) => field === 'age' ? Number(value) : value,
complete: (results) => console.log(results.data)
});
これは最も重要な判断基準の一つです。
csv-parser: Node.js専用。ブラウザでは動作しない。csvtojson: Node.js専用。ブラウザ非対応。fast-csv: Node.js専用。ブラウザ非対応。papaparse: ブラウザとNode.jsの両方で動作。フロントエンド開発者にとって唯一の選択肢。| ライブラリ | 環境 | 主な用途 | 特徴 |
|---|---|---|---|
csv-parser | Node.js | ストリーム処理、シンプルな変換 | 軽量、ストリーム専用 |
csvtojson | Node.js | 文字列/ファイル→JSON、柔軟な前処理 | 多機能、メモリ消費大 |
fast-csv | Node.js | 高性能ストリーム、型安全な処理 | モダンAPI、双方向対応 |
papaparse | ブラウザ/Node.js | フロントエンドCSV処理、Worker対応 | ユニバーサル、高機能 |
papaparse を選んでください。他の3つはブラウザで動作しません。csv-parser(シンプルさ重視)か fast-csv(モダンAPI・型安全性重視)が適しています。csvtojson の豊富なオプションが便利です。正しいツールを選ぶことで、コードの可読性、パフォーマンス、保守性が大きく変わります。プロジェクトの要件に合わせて最適なライブラリを選びましょう。
csv-parserはNode.js環境でシンプルかつ軽量なストリーム処理が必要な場合に最適です。巨大なCSVファイルを低メモリで逐次処理したいときや、他の変換ロジックを自前で実装したい場合に向いています。ただし、ブラウザ環境では使用できず、高度な変換機能は備えていないため、複雑な前処理が必要なケースには不向きです。
csvtojsonはNode.js上でCSVをJSONに変換する必要があり、かつ柔軟な前処理やカスタマイズが必要な場合に適しています。文字列やファイルからの変換が簡単で、preFileLineやpreRawDataといったフックによる細かい制御が可能です。ただし、大規模データではメモリを大量に消費するため、ストリーム処理が必須のシナリオでは注意が必要です。
fast-csvはNode.js環境で高性能かつ型安全なCSV処理を求める場合に最適です。ストリームとPromiseの両方をサポートしており、transformオプションによる各行の加工も容易です。TypeScriptとの相性も良く、大規模プロジェクトでの堅牢性を重視する場合に推奨されます。ただし、ブラウザ環境では使用できない点に注意してください。
papaparseはフロントエンドでCSVを処理する必要がある場合に唯一の選択肢です。Fileオブジェクト、Blob、文字列などブラウザ特有の入力に対応しており、Web Workerによるバックグラウンド処理も可能でUIのフリーズを防げます。Node.jsでも動作するため、ユニバーサルなアプリケーション開発にも適しています。ただし、Node.js専用の高度なストリーム機能は他ライブラリほど充実していません。
Streaming CSV parser that aims for maximum speed as well as compatibility with the csv-spectrum CSV acid test suite.
csv-parser can convert CSV into JSON at at rate of around 90,000 rows per
second. Performance varies with the data used; try bin/bench.js <your file>
to benchmark your data.
csv-parser can be used in the browser with browserify.
neat-csv can be used if a Promise
based interface to csv-parser is needed.
Note: This module requires Node v8.16.0 or higher.
⚡️ csv-parser is greased-lightning fast
→ npm run bench
Filename Rows Parsed Duration
backtick.csv 2 3.5ms
bad-data.csv 3 0.55ms
basic.csv 1 0.26ms
comma-in-quote.csv 1 0.29ms
comment.csv 2 0.40ms
empty-columns.csv 1 0.40ms
escape-quotes.csv 3 0.38ms
geojson.csv 3 0.46ms
large-dataset.csv 7268 73ms
newlines.csv 3 0.35ms
no-headers.csv 3 0.26ms
option-comment.csv 2 0.24ms
option-escape.csv 3 0.25ms
option-maxRowBytes.csv 4577 39ms
option-newline.csv 0 0.47ms
option-quote-escape.csv 3 0.33ms
option-quote-many.csv 3 0.38ms
option-quote.csv 2 0.22ms
quotes+newlines.csv 3 0.20ms
strict.csv 3 0.22ms
latin.csv 2 0.38ms
mac-newlines.csv 2 0.28ms
utf16-big.csv 2 0.33ms
utf16.csv 2 0.26ms
utf8.csv 2 0.24ms
Using npm:
$ npm install csv-parser
Using yarn:
$ yarn add csv-parser
To use the module, create a readable stream to a desired CSV file, instantiate
csv, and pipe the stream to csv.
Suppose you have a CSV file data.csv which contains the data:
NAME,AGE
Daffy Duck,24
Bugs Bunny,22
It could then be parsed, and results shown like so:
const csv = require('csv-parser')
const fs = require('fs')
const results = [];
fs.createReadStream('data.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
console.log(results);
// [
// { NAME: 'Daffy Duck', AGE: '24' },
// { NAME: 'Bugs Bunny', AGE: '22' }
// ]
});
To specify options for csv, pass an object argument to the function. For
example:
csv({ separator: '\t' });
Returns: Array[Object]
Type: Object
As an alternative to passing an options object, you may pass an Array[String]
which specifies the headers to use. For example:
csv(['Name', 'Age']);
If you need to specify options and headers, please use the the object notation
with the headers property as shown below.
Type: String
Default: "
A single-character string used to specify the character used to escape strings in a CSV row.
Type: Array[String] | Boolean
Specifies the headers to use. Headers define the property key for each value in
a CSV row. If no headers option is provided, csv-parser will use the first
line in a CSV file as the header specification.
If false, specifies that the first row in a data file does not contain
headers, and instructs the parser to use the column index as the key for each column.
Using headers: false with the same data.csv example from above would yield:
[
{ '0': 'Daffy Duck', '1': 24 },
{ '0': 'Bugs Bunny', '1': 22 }
]
Note: If using the headers for an operation on a file which contains headers on the first line, specify skipLines: 1 to skip over the row, or the headers row will appear as normal row data. Alternatively, use the mapHeaders option to manipulate existing headers in that scenario.
Type: Function
A function that can be used to modify the values of each header. Return a String to modify the header. Return null to remove the header, and it's column, from the results.
csv({
mapHeaders: ({ header, index }) => header.toLowerCase()
})
header String The current column header.
index Number The current column index.
Type: Function
A function that can be used to modify the content of each column. The return value will replace the current column content.
csv({
mapValues: ({ header, index, value }) => value.toLowerCase()
})
header String The current column header.
index Number The current column index.
value String The current column value (or content).
Type: String
Default: \n
Specifies a single-character string to denote the end of a line in a CSV file.
Type: String
Default: "
Specifies a single-character string to denote a quoted string.
Type: Boolean
If true, instructs the parser not to decode UTF-8 strings.
Type: String
Default: ,
Specifies a single-character string to use as the column separator for each row.
Type: Boolean | String
Default: false
Instructs the parser to ignore lines which represent comments in a CSV file. Since there is no specification that dictates what a CSV comment looks like, comments should be considered non-standard. The "most common" character used to signify a comment in a CSV file is "#". If this option is set to true, lines which begin with # will be skipped. If a custom character is needed to denote a commented line, this option may be set to a string which represents the leading character(s) signifying a comment line.
Type: Number
Default: 0
Specifies the number of lines at the beginning of a data file that the parser should skip over, prior to parsing headers.
Type: Number
Default: Number.MAX_SAFE_INTEGER
Maximum number of bytes per row. An error is thrown if a line exeeds this value. The default value is on 8 peta byte.
Type: Boolean
Default: false
If true, instructs the parser that the number of columns in each row must match
the number of headers specified or throws an exception.
if false: the headers are mapped to the column index
less columns: any missing column in the middle will result in a wrong property mapping!
more columns: the aditional columns will create a "_"+index properties - eg. "_10":"value"
Type: Boolean
Default: false
If true, instructs the parser to emit each row with a byteOffset property.
The byteOffset represents the offset in bytes of the beginning of the parsed row in the original stream.
Will change the output format of stream to be { byteOffset, row }.
The following events are emitted during parsing:
dataEmitted for each row of data parsed with the notable exception of the header row. Please see Usage for an example.
headersEmitted after the header row is parsed. The first parameter of the event
callback is an Array[String] containing the header names.
fs.createReadStream('data.csv')
.pipe(csv())
.on('headers', (headers) => {
console.log(`First header: ${headers[0]}`)
})
Events available on Node built-in
Readable Streams
are also emitted. The end event should be used to detect the end of parsing.
This module also provides a CLI which will convert CSV to newline-delimited JSON. The following CLI flags can be used to control how input is parsed:
Usage: csv-parser [filename?] [options]
--escape,-e Set the escape character (defaults to quote value)
--headers,-h Explicitly specify csv headers as a comma separated list
--help Show this help
--output,-o Set output file. Defaults to stdout
--quote,-q Set the quote character ('"' by default)
--remove Remove columns from output by header name
--separator,-s Set the separator character ("," by default)
--skipComments,-c Skip CSV comments that begin with '#'. Set a value to change the comment character.
--skipLines,-l Set the number of lines to skip to before parsing headers
--strict Require column length match headers length
--version,-v Print out the installed version
For example; to parse a TSV file:
cat data.tsv | csv-parser -s $'\t'
Users may encounter issues with the encoding of a CSV file. Transcoding the source stream can be done neatly with a modules such as:
Or native iconv if part
of a pipeline.
Some CSV files may be generated with, or contain a leading Byte Order Mark. This may cause issues parsing headers and/or data from your file. From Wikipedia:
The Unicode Standard permits the BOM in UTF-8, but does not require nor recommend its use. Byte order has no meaning in UTF-8.
To use this module with a file containing a BOM, please use a module like strip-bom-stream in your pipeline:
const fs = require('fs');
const csv = require('csv-parser');
const stripBom = require('strip-bom-stream');
fs.createReadStream('data.csv')
.pipe(stripBom())
.pipe(csv())
...
When using the CLI, the BOM can be removed by first running:
$ sed $'s/\xEF\xBB\xBF//g' data.csv