{"id":13555015,"url":"https://github.com/ryu1kn/csv-writer","last_synced_at":"2025-05-14T16:12:48.400Z","repository":{"id":41274070,"uuid":"66066495","full_name":"ryu1kn/csv-writer","owner":"ryu1kn","description":"Convert objects/arrays into a CSV string or write them into a CSV file","archived":false,"fork":false,"pushed_at":"2025-02-18T18:54:18.000Z","size":433,"stargazers_count":252,"open_issues_count":32,"forks_count":41,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-01T04:51:19.295Z","etag":null,"topics":["csv","csv-writer","npm-package"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/csv-writer","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryu1kn.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["ryu1kn"]}},"created_at":"2016-08-19T08:30:03.000Z","updated_at":"2025-03-29T21:01:11.000Z","dependencies_parsed_at":"2023-02-05T03:01:18.915Z","dependency_job_id":"b1141914-1645-43ae-af9e-3a8bd9eb2522","html_url":"https://github.com/ryu1kn/csv-writer","commit_stats":{"total_commits":185,"total_committers":8,"mean_commits":23.125,"dds":"0.12972972972972974","last_synced_commit":"4b36c0c42ae2c8b7ca3ee9567aabf61e9dab9bd2"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryu1kn%2Fcsv-writer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryu1kn%2Fcsv-writer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryu1kn%2Fcsv-writer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryu1kn%2Fcsv-writer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryu1kn","download_url":"https://codeload.github.com/ryu1kn/csv-writer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247790510,"owners_count":20996542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csv","csv-writer","npm-package"],"created_at":"2024-08-01T12:02:59.957Z","updated_at":"2025-04-08T06:29:51.836Z","avatar_url":"https://github.com/ryu1kn.png","language":"TypeScript","funding_links":["https://github.com/sponsors/ryu1kn"],"categories":["TypeScript"],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/ryu1kn/csv-writer.svg?branch=master)](https://travis-ci.org/ryu1kn/csv-writer)\n[![Coverage Status](https://coveralls.io/repos/github/ryu1kn/csv-writer/badge.svg?branch=master)](https://coveralls.io/github/ryu1kn/csv-writer?branch=master)\n[![Code Climate](https://codeclimate.com/github/ryu1kn/csv-writer/badges/gpa.svg)](https://codeclimate.com/github/ryu1kn/csv-writer)\n\n# CSV Writer\n\nConvert objects/arrays into a CSV string or write them into a file.\nIt respects [RFC 4180](https://tools.ietf.org/html/rfc4180) for the output CSV format.\n\n## Prerequisite\n\n* Node version 4 or above\n\n## Usage\n\nThe example below shows how you can write records defined as the array of objects into a file.\n\n```js\nconst createCsvWriter = require('csv-writer').createObjectCsvWriter;\nconst csvWriter = createCsvWriter({\n    path: 'path/to/file.csv',\n    header: [\n        {id: 'name', title: 'NAME'},\n        {id: 'lang', title: 'LANGUAGE'}\n    ]\n});\n\nconst records = [\n    {name: 'Bob',  lang: 'French, English'},\n    {name: 'Mary', lang: 'English'}\n];\n\ncsvWriter.writeRecords(records)       // returns a promise\n    .then(() =\u003e {\n        console.log('...Done');\n    });\n\n// This will produce a file path/to/file.csv with following contents:\n//\n//   NAME,LANGUAGE\n//   Bob,\"French, English\"\n//   Mary,English\n```\n\nYou can keep writing records into the same file by calling `writeRecords` multiple times\n(but need to wait for the fulfillment of the `promise` of the previous `writeRecords` call).\n\n```js\n// In an `async` function\nawait csvWriter.writeRecords(records1)\nawait csvWriter.writeRecords(records2)\n...\n```\n\nHowever, if you need to keep writing large data to a certain file, you would want to create\nnode's transform stream and use `CsvStringifier`, which is explained later, inside it\n, and pipe the stream into a file write stream.\n\nIf you don't want to write a header line, don't give `title` to header elements and just give field IDs as a string.\n\n```js\nconst createCsvWriter = require('csv-writer').createObjectCsvWriter;\nconst csvWriter = createCsvWriter({\n    path: 'path/to/file.csv',\n    header: ['name', 'lang']\n});\n```\n\nIf each record is defined as an array, use `createArrayCsvWriter` to get an `csvWriter`.\n\n```js\nconst createCsvWriter = require('csv-writer').createArrayCsvWriter;\nconst csvWriter = createCsvWriter({\n    header: ['NAME', 'LANGUAGE'],\n    path: 'path/to/file.csv'\n});\n\nconst records = [\n    ['Bob',  'French, English'],\n    ['Mary', 'English']\n];\n\ncsvWriter.writeRecords(records)       // returns a promise\n    .then(() =\u003e {\n        console.log('...Done');\n    });\n\n// This will produce a file path/to/file.csv with following contents:\n//\n//   NAME,LANGUAGE\n//   Bob,\"French, English\"\n//   Mary,English\n```\n\nIf you just want to get a CSV string but don't want to write into a file,\nyou can use `createObjectCsvStringifier` (or `createArrayCsvStringifier`)\nto get an `csvStringifier`.\n\n```js\nconst createCsvStringifier = require('csv-writer').createObjectCsvStringifier;\nconst csvStringifier = createCsvStringifier({\n    header: [\n        {id: 'name', title: 'NAME'},\n        {id: 'lang', title: 'LANGUAGE'}\n    ]\n});\n\nconst records = [\n    {name: 'Bob',  lang: 'French, English'},\n    {name: 'Mary', lang: 'English'}\n];\n\nconsole.log(csvStringifier.getHeaderString());\n// =\u003e 'NAME,LANGUAGE\\n'\n\nconsole.log(csvStringifier.stringifyRecords(records));\n// =\u003e 'Bob,\"French, English\"\\nMary,English\\n'\n```\n\n\n## API\n\n### createObjectCsvWriter(params)\n\n##### Parameters:\n\n* params `\u003cObject\u003e`\n  * path `\u003cstring\u003e`\n\n      Path to a write file\n\n  * header `\u003cArray\u003c{id, title}|string\u003e\u003e`\n\n      Array of objects (`id` and `title` properties) or strings (field IDs).\n      A header line will be written to the file only if given as an array of objects.\n\n  * fieldDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `,`. Only either comma `,` or semicolon `;` is allowed.\n\n  * recordDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `\\n`. Only either LF (`\\n`) or CRLF (`\\r\\n`) is allowed.\n\n  * headerIdDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `undefined`. Give this value to specify a path to a value in a nested object.\n\n  * alwaysQuote `\u003cboolean\u003e` (optional)\n\n      Default: `false`. Set it to `true` to double-quote all fields regardless of their values.\n\n  * encoding `\u003cstring\u003e` (optional)\n\n      Default: `utf8`.\n\n  * append `\u003cboolean\u003e` (optional)\n\n      Default: `false`. When `true`, it will append CSV records to the specified file.\n      If the file doesn't exist, it will create one.\n\n      **NOTE:** A header line will not be written to the file if `true` is given.\n\n##### Returns:\n\n* `\u003cCsvWriter\u003e`\n\n\n### createArrayCsvWriter(params)\n\n##### Parameters:\n\n* params `\u003cObject\u003e`\n  * path `\u003cstring\u003e`\n\n      Path to a write file\n\n  * header `\u003cArray\u003cstring\u003e\u003e` (optional)\n\n      Array of field titles\n\n  * fieldDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `,`. Only either comma `,` or semicolon `;` is allowed.\n\n  * recordDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `\\n`. Only either LF (`\\n`) or CRLF (`\\r\\n`) is allowed.\n\n  * alwaysQuote `\u003cboolean\u003e` (optional)\n\n      Default: `false`. Set it to `true` to double-quote all fields regardless of their values.\n\n  * encoding `\u003cstring\u003e` (optional)\n\n      Default: `utf8`.\n\n  * append `\u003cboolean\u003e` (optional)\n\n      Default: `false`. When `true`, it will append CSV records to the specified file.\n      If the file doesn't exist, it will create one.\n\n      **NOTE:** A header line will not be written to the file if `true` is given.\n\n##### Returns:\n\n* `\u003cCsvWriter\u003e`\n\n\n### CsvWriter#writeRecords(records)\n\n##### Parameters:\n\n* records `\u003cIterator\u003cObject|Array\u003e\u003e`\n\n    Depending on which function was used to create a `csvWriter` (i.e. `createObjectCsvWriter` or `createArrayCsvWriter`),\n    records will be either a collection of objects or arrays. As long as the collection is iterable, it doesn't need to be an array.\n\n##### Returns:\n\n* `\u003cPromise\u003e`\n\n\n### createObjectCsvStringifier(params)\n\n##### Parameters:\n\n* params `\u003cObject\u003e`\n  * header `\u003cArray\u003c{id, title}|string\u003e\u003e`\n\n      Array of objects (`id` and `title` properties) or strings (field IDs)\n\n  * fieldDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `,`. Only either comma `,` or semicolon `;` is allowed.\n\n  * recordDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `\\n`. Only either LF (`\\n`) or CRLF (`\\r\\n`) is allowed.\n\n  * headerIdDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `undefined`. Give this value to specify a path to a value in a nested object.\n\n  * alwaysQuote `\u003cboolean\u003e` (optional)\n\n      Default: `false`. Set it to `true` to double-quote all fields regardless of their values.\n\n##### Returns:\n\n* `\u003cObjectCsvStringifier\u003e`\n\n### ObjectCsvStringifier#getHeaderString()\n\n##### Returns:\n\n* `\u003cstring\u003e`\n\n### ObjectCsvStringifier#stringifyRecords(records)\n\n##### Parameters:\n\n* records `\u003cArray\u003cObject\u003e\u003e`\n\n##### Returns:\n\n* `\u003cstring\u003e`\n\n### createArrayCsvStringifier(params)\n\n##### Parameters:\n\n* params `\u003cObject\u003e`\n  * header `\u003cArray\u003cstring\u003e\u003e` (optional)\n\n      Array of field titles\n\n  * fieldDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `,`. Only either comma `,` or semicolon `;` is allowed.\n\n  * recordDelimiter `\u003cstring\u003e` (optional)\n\n      Default: `\\n`. Only either LF (`\\n`) or CRLF (`\\r\\n`) is allowed.\n\n  * alwaysQuote `\u003cboolean\u003e` (optional)\n\n      Default: `false`. Set it to `true` to double-quote all fields regardless of their values.\n\n##### Returns:\n\n* `\u003cArrayCsvStringifier\u003e`\n\n### ArrayCsvStringifier#getHeaderString()\n\n##### Returns:\n\n* `\u003cstring\u003e`\n\n### ArrayCsvStringifier#stringifyRecords(records)\n\n##### Parameters:\n\n* records `\u003cArray\u003cArray\u003cstring\u003e\u003e\u003e`\n\n##### Returns:\n\n* `\u003cstring\u003e`\n\n\n## Request Features or Report Bugs\n\nFeature requests and bug reports are very welcome: https://github.com/ryu1kn/csv-writer/issues\n\nA couple of requests from me when you raise an issue on GitHub.\n\n* **Requesting a feature:** Please try to provide the context of why you want the feature. Such as,\n  in what situation the feature could help you and how, or how the lack of the feature is causing an inconvenience to you.\n  I can't start thinking of introducing it until I understand how it helps you 🙂\n* **Reporting a bug:** If you could provide a runnable code snippet that reproduces the bug, it would be very helpful!\n\n\n## Development\n\n### Prerequisite\n\n* Node version 8 or above\n* Docker\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryu1kn%2Fcsv-writer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryu1kn%2Fcsv-writer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryu1kn%2Fcsv-writer/lists"}