{"id":13030,"library":"csv-stringify","title":"CSV Stringify","description":"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.","status":"active","version":"6.7.0","language":"javascript","source_language":"en","source_url":"https://github.com/adaltas/node-csv","tags":["javascript","csv","stringify","stringifier","backend","frontend","typescript"],"install":[{"cmd":"npm install csv-stringify","lang":"bash","label":"npm"},{"cmd":"yarn add csv-stringify","lang":"bash","label":"yarn"},{"cmd":"pnpm add csv-stringify","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { stringify } from 'csv-stringify'; // This imports the stream/callback API, not the sync version","symbol":"stringify (sync API)","correct":"import { stringify } from 'csv-stringify/sync';"},{"note":"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).","wrong":"const stringify = require('csv-stringify').stringify; // While functional, prefers ESM imports in modern Node.js","symbol":"stringify (stream/callback API)","correct":"import { stringify } from 'csv-stringify';"},{"note":"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.","wrong":"import Stringifier from 'csv-stringify'; // No default export for the class","symbol":"Stringifier (Class)","correct":"import { Stringifier } from 'csv-stringify';"},{"note":"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.","wrong":"const stringify = require('csv-stringify'); // Incorrect for sync API; may lead to 'stringify is not a function' if you expect the sync version.","symbol":"CommonJS require","correct":"const { stringify } = require('csv-stringify/sync');\nconst { Stringifier } = require('csv-stringify');"}],"quickstart":{"code":"import { stringify } from 'csv-stringify/sync';\nimport assert from 'assert';\n\nconst records = [\n  ['header1', 'header2', 'header3'],\n  ['value1A', 'value1B', 'value1C'],\n  ['value2A', 'value2B', 'value2C']\n];\n\nconst output = stringify(records, {\n  header: true, // Includes the first row as a header\n  delimiter: ';', // Uses semicolon as a delimiter\n  quote: '\"' // Uses double quotes for fields\n});\n\nconst expectedOutput = '\"header1\";\"header2\";\"header3\"\\n\"value1A\";\"value1B\";\"value1C\"\\n\"value2A\";\"value2B\";\"value2C\"\\n';\n\nassert.equal(output, expectedOutput);\nconsole.log(output);\n/*\n\"header1\";\"header2\";\"header3\"\n\"value1A\";\"value1B\";\"value1C\"\n\"value2A\";\"value2B\";\"value2C\"\n*/\n","lang":"typescript","description":"This quickstart demonstrates the synchronous API for `csv-stringify` to convert an array of arrays into a CSV string, including custom delimiters and headers."},"warnings":[{"fix":"Update import statements to use `import { stringify } from 'csv-stringify/sync';` for ESM or `const { stringify } = require('csv-stringify/sync');` for CommonJS.","message":"With 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`).","severity":"breaking","affected_versions":">=6.0.0"},{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=6.0.0"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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). ","message":"The `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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"Attempting to call `stringify` from the main `csv-stringify` module when the synchronous version was intended, or using `require()` without destructuring.","error":"TypeError: stringify is not a function"},{"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.","cause":"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).","error":"Error: stream.push() after EOF"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}