Registry / storage / csv-formatter

csv-formatter

JSON →
library1.1.0jsnpmunverified

A streaming CSV formatter for Node.js that transforms arrays of objects into CSV strings. Current stable version is 1.1.0. It aims for compatibility with the csv-spectrum CSV acid test suite. Key differentiators include streaming support (ideal for large datasets), customizable separator and newline, and the ability to specify headers explicitly or derive them from the first object. It can be used in the browser with browserify. The package is lightweight with no dependencies.

npm install csv-formatter
INSTALL
IMPORT
SIG · CSV-FORMATTER
C
csv-formatter
storagejavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

default
✓ import csv from 'csv-formatter'
✗ const { csv } = require('csv-formatter')
The package exports a single function. In CommonJS, use require('csv-formatter') which returns the function directly.
require('csv-formatter')
✓ const csv = require('csv-formatter')
✗ const { default: csv } = require('csv-formatter')
CommonJS import: require returns the function itself, not an object.
default (TypeScript)
✓ import csv from 'csv-formatter'
✗ import * as csv from 'csv-formatter'
TypeScript users should use default import; the module has esModuleInterop behavior or use `import csv = require('csv-formatter')`.

Demonstrates streaming CSV formatting from an array of objects to a CSV string.

import csv from 'csv-formatter'; import { Readable } from 'stream'; const data = [ { name: 'Alice', age: 30 }, { name: 'Bob', age: 25 } ]; const readable = Readable.from(data); const csvStream = readable.pipe(csv()); csvStream.on('data', chunk => { console.log(chunk.toString()); // Output: "name,age\nAlice,30\nBob,25\n" });
Debug
Known issues
gotchaHeaders are derived from the first object's keys if not explicitly provided. If the first object lacks some keys that later objects have, those keys will be missing from output.
fix
Always specify headers explicitly via options or array to ensure all fields are included.
affects: >=0.0.0
gotchaThe package expects object streams; passing non-objects may cause unexpected behavior.
fix
Ensure piped data consists of plain JavaScript objects.
affects: >=0.0.0
gotchaThe output does not handle special characters (quotes, commas, newlines) according to CSV standards automatically; the package relies on csv-spectrum, but basic escaping may be incomplete.
fix
Consider using a more robust CSV library if you need full RFC 4180 compliance.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: csv is not a function
Default import from CommonJS module without proper interop.
fix
Use `import csv from 'csv-formatter'` with ESM or `const csv = require('csv-formatter')` with CommonJS.
Cannot read property 'pipe' of undefined
csv() returns a transform stream; forgetting to call it as a function.
fix
Ensure you call csv() (e.g., .pipe(csv())) not just csv.
ReferenceError: Readable is not defined
Missing import of Readable from 'stream' in Node.js.
fix
Add import { Readable } from 'stream' or const { Readable } = require('stream').
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
21 hits · last 30 days
node
20
Resources
csv-formatter — npm install csv-formatter · libregistry