Registry / data / csv-generate

csv-generate

JSON →
library4.5.1jsnpmunverified

The `csv-generate` package, currently at version 4.5.1, is a flexible JavaScript library for generating random CSV strings and JavaScript objects. It implements the Node.js `stream.Readable` API, making it suitable for scalable data generation in both Node.js and browser environments. As a component of the broader `node-csv` project, it benefits from a stable API and active maintenance, with new releases typically aligned with the entire CSV ecosystem. Key differentiators include its adherence to the stream API for efficient memory usage with large datasets, the ability to produce random or pseudo-random data based on a seed for idempotent generation, and support for user-defined value generation across various data types (integers, booleans, dates, etc.). It can output either raw CSV strings or structured JavaScript objects (arrays) depending on configuration.

npm install csv-generate
INSTALL
IMPORT
SIG · CSV-GENERATE
C
csv-generate
datajavascriptv4.5.1
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.

generate
✓ import { generate } from 'csv-generate';
✗ const generate = require('csv-generate');
The primary named export for creating a CSV generator stream. Prefer ESM import style for modern Node.js and browser environments. The CommonJS equivalent is `const { generate } = require('csv-generate');`.
Options
✓ import type { Options } from 'csv-generate';
TypeScript type import for configuring the `generate` function. Provides type safety for the configuration object passed to `generate`.
* as csvGenerate
✓ import * as csvGenerate from 'csv-generate';
✗ const csvGenerate = require('csv-generate');
Imports all named exports into a namespace object. Useful when accessing multiple utilities from the package or for compatibility with older module patterns.

This example demonstrates how to use `csv-generate` to create a stream that outputs a fixed number of pseudo-random CSV records as JavaScript arrays, handling events for data consumption and errors.

import { generate } from 'csv-generate'; import assert from 'assert'; const records = []; // Initialize the generator with specific options generate({ seed: 1, objectMode: true, // Crucial for getting arrays/objects instead of raw CSV strings columns: 2, length: 2, }) // Use the readable stream API to consume generated records .on('readable', function () { let record; while ((record = this.read()) !== null) { records.push(record); } }) // Catch any errors during generation .on('error', function (err) { console.error('CSV Generation Error:', err); }) // Once generation is complete, assert the output .on('end', function () { assert.deepStrictEqual(records, [ ['OMH', 'ONKCHhJmjadoA'], ['D', 'GeACHiN'], ]); console.log('CSV generation successful. Records:', records); });
Debug
Known issues
gotchaThe `objectMode: true` option is critical if you intend to receive parsed JavaScript objects (arrays by default) from the stream. If omitted, the stream will emit raw CSV string chunks (buffers in binary mode), which then require a separate parsing step.
fix
Always include `{ objectMode: true }` in the options object passed to `generate()` if you expect structured data records.
affects: >=3.0
gotchaNode.js streams, including those created by `csv-generate`, require explicit error handling. Failing to attach an `.on('error', ...)` handler to the stream can lead to unhandled 'error' events, causing the Node.js process to crash.
fix
Always register an error listener on the stream, for example: `.on('error', (err) => console.error(err));`.
affects: >=1.0
gotchaWhen consuming `csv-generate` (a `Readable` stream) with a `Writable` stream (e.g., writing to a file), implement proper backpressure management. If the `Writable` stream cannot keep up, it will signal `false` from `write()`, and the `Readable` stream should be paused to prevent excessive memory usage.
fix
When piping streams, Node.js handles backpressure automatically. If consuming manually with `.on('data')` or `.on('readable')`, check `writableStream.write(chunk)` return value and call `readableStream.pause()` when `false`, resuming with `writableStream.once('drain', () => readableStream.resume())`.
affects: >=1.0
Errors
Common errors & fixes
TypeError: (0 , csv_generate__WEBPACK_IMPORTED_MODULE_0__.generate) is not a function
The `generate` function is a named export, not a default export or a class constructor. This error typically occurs when attempting to import it incorrectly (e.g., `import generate from 'csv-generate';`) or calling it with `new`.
fix
Ensure you are using named import syntax: `import { generate } from 'csv-generate';` and call it directly as a function: `generate({...});`.
Unhandled 'error' event
An error occurred within the stream pipeline, but no `.on('error')` event listener was attached to catch it, leading to a process crash.
fix
Add an error handler to your stream: `generate(...).on('error', (err) => console.error('Stream error:', err));`.
Output is a single CSV string or buffer instead of an array of records.
The `objectMode` option was not set to `true` when initializing the generator. By default, `csv-generate` operates in binary mode, emitting raw string/buffer chunks.
fix
Pass `{ objectMode: true }` in the options object when calling `generate()`: `generate({ objectMode: true, ... });`.
Upgrade
Version history
4.5.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
csv-generate — npm install csv-generate · libregistry