{"id":11382,"library":"nest-csv-parser","title":"NestJS CSV Parser","description":"nest-csv-parser is a utility module designed for the NestJS framework, simplifying the process of parsing CSV files within server-side applications. It acts as a lightweight wrapper around the popular `csv-parser` library, exposing its functionality in a NestJS-idiomatic way through an injectable service. The current stable version is 2.0.4. While a strict release cadence isn't explicitly stated, the project appears actively maintained given its version history and documentation. Its primary differentiator is seamless integration into NestJS applications using `@Module` imports and dependency injection, allowing developers to consume CSV data streams and map them directly to TypeScript entities with optional control over line counts, offsets, and underlying `csv-parser` configurations. It handles streaming large files efficiently without loading the entire content into memory.","status":"active","version":"2.0.4","language":"javascript","source_language":"en","source_url":null,"tags":["javascript"],"install":[{"cmd":"npm install nest-csv-parser","lang":"bash","label":"npm"},{"cmd":"yarn add nest-csv-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add nest-csv-parser","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core parsing engine, nest-csv-parser is a wrapper around it.","package":"csv-parser","optional":false},{"reason":"Peer dependency for NestJS module functionality and decorators.","package":"@nestjs/common","optional":false},{"reason":"Peer dependency for core NestJS functionalities.","package":"@nestjs/core","optional":false}],"imports":[{"note":"Required for `imports` array in your NestJS root or feature module to make the parser available.","wrong":"import CsvModule from 'nest-csv-parser'","symbol":"CsvModule","correct":"import { CsvModule } from 'nest-csv-parser'"},{"note":"This injectable service is used to perform the actual CSV parsing. Inject it into your NestJS services or controllers.","wrong":"const { CsvParser } = require('nest-csv-parser')","symbol":"CsvParser","correct":"import { CsvParser } from 'nest-csv-parser'"},{"note":"While used in examples, `Injectable` is a core NestJS decorator and not exported by `nest-csv-parser` itself.","wrong":"import { Injectable } from 'nest-csv-parser'","symbol":"Injectable","correct":"import { Injectable } from '@nestjs/common'"}],"quickstart":{"code":"import { Module, Injectable } from '@nestjs/common';\nimport { CsvModule, CsvParser } from 'nest-csv-parser';\nimport * as fs from 'fs';\n\n// 1. Define your entity structure matching CSV headers\nclass MyDataEntity {\n  id: number;\n  name: string;\n  value: string;\n}\n\n@Injectable()\nexport class MyCsvService {\n  constructor(\n    private readonly csvParser: CsvParser\n  ) {}\n\n  async parseMyCsvFile(): Promise<MyDataEntity[]> {\n    // Create a dummy CSV file for demonstration\n    const csvContent = 'id,name,value\\n1,Alice,Alpha\\n2,Bob,Beta\\n3,Charlie,Gamma';\n    const filePath = './temp.csv';\n    fs.writeFileSync(filePath, csvContent);\n\n    // Create a readable stream from the file\n    const stream = fs.createReadStream(filePath);\n\n    try {\n      // Parse the stream into an array of MyDataEntity objects\n      const entities: MyDataEntity[] = await this.csvParser.parse(stream, MyDataEntity);\n      console.log('Parsed Entities:', entities);\n      return entities;\n    } finally {\n      // Clean up the dummy file\n      fs.unlinkSync(filePath);\n    }\n  }\n}\n\n@Module({\n  imports: [CsvModule],\n  providers: [MyCsvService],\n  exports: [MyCsvService]\n})\nexport class MyCsvParsingModule {}\n\n// Example of how to use it (e.g., in your main.ts or another module)\nasync function bootstrap() {\n  // In a real NestJS app, this would be handled by the framework\n  // For quickstart, we'll manually instantiate\n  const moduleRef = await import('@nestjs/core').then(m => m.NestFactory.createApplicationContext(MyCsvParsingModule));\n  const myCsvService = moduleRef.get(MyCsvService);\n  await myCsvService.parseMyCsvFile();\n  await moduleRef.close();\n}\n\nbootstrap();","lang":"typescript","description":"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."},"warnings":[{"fix":"Ensure your CSV header names (case-sensitive) directly correspond to the property names in your TypeScript entity class, or preprocess the CSV stream to rename headers.","message":"The `csv-parser` library (which `nest-csv-parser` wraps) expects CSV headers to exactly match the property names of your TypeScript `Entity` class. Mismatches will result in `undefined` values for those properties.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Pass an explicit `csvConfig` object to the `parse` method, for example: `csvParser.parse(stream, Entity, null, null, { separator: ',', strict: false })`.","message":"By default, `nest-csv-parser` configures the underlying `csv-parser` with `{ strict: true, separator: ';' }`. If your CSV uses a different separator (e.g., comma `,`) or has inconsistent row lengths, you must override `csvConfig` in the `parse` method.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use `await` when calling `this.csvParser.parse(...)` within an `async` function.","message":"The `parse` method returns a `Promise<Entity[]>`. It's crucial to `await` this promise, especially when dealing with potentially large files, to ensure all data is processed before attempting to use the results. Forgetting `await` will result in a promise object instead of the parsed data.","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":"Add `CsvModule` to the `imports` array of the NestJS module where you are using `CsvParser`. For example: `@Module({ imports: [CsvModule], providers: [...] })`.","cause":"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.","error":"Nest can't resolve dependencies of the CsvParser (?). Please make sure that the argument at index [0] is available in the CsvModule context."},{"fix":"Ensure `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.","cause":"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.","error":"Property 'parse' does not exist on type 'CsvParser'."}],"ecosystem":"npm"}