Registry / serialization / csv-stringify

csv-stringify

JSON →
library1.0.0jsnpmunverified

csv-stringify is a JavaScript library designed to convert data records into CSV (Comma Separated Values) formatted text. It primarily implements the Node.js `stream.Transform` API, allowing for efficient processing of large datasets by handling data in chunks rather than loading entire files into memory. The package also provides convenient synchronous and callback-based APIs for simpler use cases. First released in 2010, it boasts a mature codebase, comprehensive test coverage, and a focus on scalability and customizability, supporting various delimiters, quotes, escape characters, and headers. It is currently at version 6.7.0 (as of early 2026), indicating active maintenance and consistent updates. It is often used as part of the broader `node-csv` ecosystem alongside `csv-parse`, `csv-generate`, and `stream-transform` to build complete CSV processing pipelines.

npm install csv-stringify
INSTALL
IMPORT
SIG · CSV-STRINGIFY
C
csv-stringify
serializationjavascriptv1.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

stringify (sync API)
✓ import { stringify } from 'csv-stringify/sync';
✗ import { stringify } from 'csv-stringify'; // This imports the stream/callback API, not the sync version
For the direct synchronous conversion of data to a CSV string, use the `/sync` submodule. This is ideal for smaller datasets or scripting where streaming isn't required.
stringify (stream/callback API)
✓ import { stringify } from 'csv-stringify';
✗ const stringify = require('csv-stringify').stringify; // While functional, prefers ESM imports in modern Node.js
This imports the main `stringify` function, which can be used for both the callback-based API (passing data and a callback) or to create a `stream.Transform` instance (passing options).
Stringifier (Class)
✓ import { Stringifier } from 'csv-stringify';
✗ import Stringifier from 'csv-stringify'; // No default export for the class
Directly access the `Stringifier` class for advanced stream-based usage or when you need to extend its functionality. This is the underlying `stream.Transform` implementation.
CommonJS require
✓ const { stringify } = require('csv-stringify/sync'); const { Stringifier } = require('csv-stringify');
✗ const stringify = require('csv-stringify'); // Incorrect for sync API; may lead to 'stringify is not a function' if you expect the sync version.
CommonJS users should explicitly require from `csv-stringify/sync` for the synchronous function or `csv-stringify` for the stream/callback functions and the `Stringifier` class.

This quickstart demonstrates the synchronous API for `csv-stringify` to convert an array of arrays into a CSV string, including custom delimiters and headers.

import { stringify } from 'csv-stringify/sync'; import assert from 'assert'; const records = [ ['header1', 'header2', 'header3'], ['value1A', 'value1B', 'value1C'], ['value2A', 'value2B', 'value2C'] ]; const output = stringify(records, { header: true, // Includes the first row as a header delimiter: ';', // Uses semicolon as a delimiter quote: '"' // Uses double quotes for fields }); const expectedOutput = '"header1";"header2";"header3"\n"value1A";"value1B";"value1C"\n"value2A";"value2B";"value2C"\n'; assert.equal(output, expectedOutput); console.log(output); /* "header1";"header2";"header3" "value1A";"value1B";"value1C" "value2A";"value2B";"value2C" */
Debug
Known issues
breakingWith the release of `csv-stringify` v6.0.0, the import path for the synchronous API changed. Previously, it was often accessed via `{package_name}/lib/sync` (e.g., `csv-stringify/lib/sync`).
fix
Update import statements to use `import { stringify } from 'csv-stringify/sync';` for ESM or `const { stringify } = require('csv-stringify/sync');` for CommonJS.
affects: >=6.0.0
breakingVersion 6 fully transitioned to ECMAScript Modules (ESM) internally and provides transparent usage between CommonJS and ESM through the `package.json` `exports` property. While older Node.js versions might have fallbacks, modern Node.js and bundlers will respect the ESM exports.
fix
Ensure your project is configured for ESM if you intend to use `import` statements, and be mindful of module resolution in older Node.js environments. If experiencing issues, verify Node.js version compatibility (Node 12+ for `exports` field) or consider `require('csv-stringify/dist/cjs/sync.cjs')` for older CJS setups.
affects: >=6.0.0
gotchaWhen using `csv-stringify` in streaming mode (i.e., `new Stringifier(options)` or piping directly), forgetting to end the writable stream can lead to incomplete output or processes hanging, as the 'end' event won't be emitted.
fix
Always ensure the writable stream is explicitly ended after all data has been written, for example, by calling `stream.end()` or ensuring the upstream readable stream finishes and pipes correctly.
affects: >=1.0.0
gotchaThe `header` option (to include column names as the first row) needs to be explicitly set to `true` for `csv-stringify` to generate a header. If using an array of objects as input, `columns` option also needs to be defined for the header to correctly map object keys to CSV columns.
fix
Set `header: true` in the options for CSV output to include a header row. If using object input, specify the `columns` option as an array of strings (for the order) or objects (for mapping/formatting). 
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: stringify is not a function
Attempting to call `stringify` from the main `csv-stringify` module when the synchronous version was intended, or using `require()` without destructuring.
fix
For synchronous stringification, use `import { stringify } from 'csv-stringify/sync';` or `const { stringify } = require('csv-stringify/sync');`. If using the main module, remember it provides the stream/callback API.
Error: stream.push() after EOF
Attempting to write data to a `csv-stringify` stream after it has been explicitly ended or implicitly closed (e.g., due to an upstream stream ending).
fix
Ensure that data is only written to the stream while it is still open. Check upstream data sources for premature closure or ensure `stream.end()` is not called too early.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
14
OpenAI (training)
1
Resources
csv-stringify — npm install csv-stringify · libregistry