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
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
CsvModule
✓ import { CsvModule } from 'nest-csv-parser'
✗ import CsvModule from 'nest-csv-parser'
Required for `imports` array in your NestJS root or feature module to make the parser available.
CsvParser
✓ import { CsvParser } from 'nest-csv-parser'
✗ const { CsvParser } = require('nest-csv-parser')
This injectable service is used to perform the actual CSV parsing. Inject it into your NestJS services or controllers.
Injectable
✓ import { Injectable } from '@nestjs/common'
✗ import { Injectable } from 'nest-csv-parser'
While used in examples, `Injectable` is a core NestJS decorator and not exported by `nest-csv-parser` itself.
This quickstart demonstrates how to set up `CsvModule` in a NestJS application, define a TypeScript entity to map CSV data, and use `CsvParser` to read and parse a CSV file stream into an array of typed objects. It includes creating a temporary CSV file for demonstration.
import { Module, Injectable } from '@nestjs/common';
import { CsvModule, CsvParser } from 'nest-csv-parser';
import * as fs from 'fs';
// 1. Define your entity structure matching CSV headers
class MyDataEntity {
id: number;
name: string;
value: string;
}
@Injectable()
export class MyCsvService {
constructor(
private readonly csvParser: CsvParser
) {}
async parseMyCsvFile(): Promise<MyDataEntity[]> {
// Create a dummy CSV file for demonstration
const csvContent = 'id,name,value\n1,Alice,Alpha\n2,Bob,Beta\n3,Charlie,Gamma';
const filePath = './temp.csv';
fs.writeFileSync(filePath, csvContent);
// Create a readable stream from the file
const stream = fs.createReadStream(filePath);
try {
// Parse the stream into an array of MyDataEntity objects
const entities: MyDataEntity[] = await this.csvParser.parse(stream, MyDataEntity);
console.log('Parsed Entities:', entities);
return entities;
} finally {
// Clean up the dummy file
fs.unlinkSync(filePath);
}
}
}
@Module({
imports: [CsvModule],
providers: [MyCsvService],
exports: [MyCsvService]
})
export class MyCsvParsingModule {}
// Example of how to use it (e.g., in your main.ts or another module)
async function bootstrap() {
// In a real NestJS app, this would be handled by the framework
// For quickstart, we'll manually instantiate
const moduleRef = await import('@nestjs/core').then(m => m.NestFactory.createApplicationContext(MyCsvParsingModule));
const myCsvService = moduleRef.get(MyCsvService);
await myCsvService.parseMyCsvFile();
await moduleRef.close();
}
bootstrap();
Errors
Common errors & fixes
Nest can't resolve dependencies of the CsvParser (?). Please make sure that the argument at index [0] is available in the CsvModule context.
The `CsvModule` has not been correctly imported into the `imports` array of the module where `CsvParser` is being injected, or the module providing `CsvModule` is not imported into the current module.
fixAdd `CsvModule` to the `imports` array of the NestJS module where you are using `CsvParser`. For example: `@Module({ imports: [CsvModule], providers: [...] })`. Property 'parse' does not exist on type 'CsvParser'.
This error typically occurs if TypeScript cannot infer the type of `csvParser` or if an older version of the package is used that might have a different API. More commonly, it means `CsvParser` was not properly injected or imported.
fixEnsure `CsvParser` is correctly injected via the constructor (`private readonly csvParser: CsvParser`), and that `import { CsvParser } from 'nest-csv-parser'` is present at the top of the file. Audit
Dependencies
csv-parserrequiredCore parsing engine, nest-csv-parser is a wrapper around it.
@nestjs/commonrequiredPeer dependency for NestJS module functionality and decorators.
@nestjs/corerequiredPeer dependency for core NestJS functionalities.