Convert objects/arrays into a CSV string or write them into a CSV file
Installations
npm install csv-writer
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
10.17.0
NPM Version
6.11.3
Score
98.4
Supply Chain
100
Quality
75.9
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (93.91%)
JavaScript (4.74%)
Shell (1.35%)
Developer
Download Statistics
Total Downloads
62,959,055
Last Day
67,973
Last Week
472,437
Last Month
2,693,354
Last Year
26,762,652
GitHub Statistics
248 Stars
192 Commits
39 Forks
4 Watching
1 Branches
6 Contributors
Bundle Size
10.60 kB
Minified
2.51 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.6.0
Package Id
csv-writer@1.6.0
Size
23.23 kB
NPM Version
6.11.3
Node Version
10.17.0
Publised On
18 Jan 2020
Total Downloads
Cumulative downloads
Total Downloads
62,959,055
Last day
-43.2%
67,973
Compared to previous day
Last week
-31.1%
472,437
Compared to previous week
Last month
-1.3%
2,693,354
Compared to previous month
Last year
75.6%
26,762,652
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
CSV Writer
Convert objects/arrays into a CSV string or write them into a file. It respects RFC 4180 for the output CSV format.
Prerequisite
- Node version 4 or above
Usage
The example below shows how you can write records defined as the array of objects into a file.
1const createCsvWriter = require('csv-writer').createObjectCsvWriter; 2const csvWriter = createCsvWriter({ 3 path: 'path/to/file.csv', 4 header: [ 5 {id: 'name', title: 'NAME'}, 6 {id: 'lang', title: 'LANGUAGE'} 7 ] 8}); 9 10const records = [ 11 {name: 'Bob', lang: 'French, English'}, 12 {name: 'Mary', lang: 'English'} 13]; 14 15csvWriter.writeRecords(records) // returns a promise 16 .then(() => { 17 console.log('...Done'); 18 }); 19 20// This will produce a file path/to/file.csv with following contents: 21// 22// NAME,LANGUAGE 23// Bob,"French, English" 24// Mary,English
You can keep writing records into the same file by calling writeRecords
multiple times
(but need to wait for the fulfillment of the promise
of the previous writeRecords
call).
1// In an `async` function
2await csvWriter.writeRecords(records1)
3await csvWriter.writeRecords(records2)
4...
However, if you need to keep writing large data to a certain file, you would want to create
node's transform stream and use CsvStringifier
, which is explained later, inside it
, and pipe the stream into a file write stream.
If you don't want to write a header line, don't give title
to header elements and just give field IDs as a string.
1const createCsvWriter = require('csv-writer').createObjectCsvWriter; 2const csvWriter = createCsvWriter({ 3 path: 'path/to/file.csv', 4 header: ['name', 'lang'] 5});
If each record is defined as an array, use createArrayCsvWriter
to get an csvWriter
.
1const createCsvWriter = require('csv-writer').createArrayCsvWriter; 2const csvWriter = createCsvWriter({ 3 header: ['NAME', 'LANGUAGE'], 4 path: 'path/to/file.csv' 5}); 6 7const records = [ 8 ['Bob', 'French, English'], 9 ['Mary', 'English'] 10]; 11 12csvWriter.writeRecords(records) // returns a promise 13 .then(() => { 14 console.log('...Done'); 15 }); 16 17// This will produce a file path/to/file.csv with following contents: 18// 19// NAME,LANGUAGE 20// Bob,"French, English" 21// Mary,English
If you just want to get a CSV string but don't want to write into a file,
you can use createObjectCsvStringifier
(or createArrayCsvStringifier
)
to get an csvStringifier
.
1const createCsvStringifier = require('csv-writer').createObjectCsvStringifier; 2const csvStringifier = createCsvStringifier({ 3 header: [ 4 {id: 'name', title: 'NAME'}, 5 {id: 'lang', title: 'LANGUAGE'} 6 ] 7}); 8 9const records = [ 10 {name: 'Bob', lang: 'French, English'}, 11 {name: 'Mary', lang: 'English'} 12]; 13 14console.log(csvStringifier.getHeaderString()); 15// => 'NAME,LANGUAGE\n' 16 17console.log(csvStringifier.stringifyRecords(records)); 18// => 'Bob,"French, English"\nMary,English\n'
API
createObjectCsvWriter(params)
Parameters:
- params
<Object>
-
path
<string>
Path to a write file
-
header
<Array<{id, title}|string>>
Array of objects (
id
andtitle
properties) or strings (field IDs). A header line will be written to the file only if given as an array of objects. -
fieldDelimiter
<string>
(optional)Default:
,
. Only either comma,
or semicolon;
is allowed. -
recordDelimiter
<string>
(optional)Default:
\n
. Only either LF (\n
) or CRLF (\r\n
) is allowed. -
headerIdDelimiter
<string>
(optional)Default:
undefined
. Give this value to specify a path to a value in a nested object. -
alwaysQuote
<boolean>
(optional)Default:
false
. Set it totrue
to double-quote all fields regardless of their values. -
encoding
<string>
(optional)Default:
utf8
. -
append
<boolean>
(optional)Default:
false
. Whentrue
, it will append CSV records to the specified file. If the file doesn't exist, it will create one.NOTE: A header line will not be written to the file if
true
is given.
-
Returns:
<CsvWriter>
createArrayCsvWriter(params)
Parameters:
- params
<Object>
-
path
<string>
Path to a write file
-
header
<Array<string>>
(optional)Array of field titles
-
fieldDelimiter
<string>
(optional)Default:
,
. Only either comma,
or semicolon;
is allowed. -
recordDelimiter
<string>
(optional)Default:
\n
. Only either LF (\n
) or CRLF (\r\n
) is allowed. -
alwaysQuote
<boolean>
(optional)Default:
false
. Set it totrue
to double-quote all fields regardless of their values. -
encoding
<string>
(optional)Default:
utf8
. -
append
<boolean>
(optional)Default:
false
. Whentrue
, it will append CSV records to the specified file. If the file doesn't exist, it will create one.NOTE: A header line will not be written to the file if
true
is given.
-
Returns:
<CsvWriter>
CsvWriter#writeRecords(records)
Parameters:
-
records
<Iterator<Object|Array>>
Depending on which function was used to create a
csvWriter
(i.e.createObjectCsvWriter
orcreateArrayCsvWriter
), 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.
Returns:
<Promise>
createObjectCsvStringifier(params)
Parameters:
- params
<Object>
-
header
<Array<{id, title}|string>>
Array of objects (
id
andtitle
properties) or strings (field IDs) -
fieldDelimiter
<string>
(optional)Default:
,
. Only either comma,
or semicolon;
is allowed. -
recordDelimiter
<string>
(optional)Default:
\n
. Only either LF (\n
) or CRLF (\r\n
) is allowed. -
headerIdDelimiter
<string>
(optional)Default:
undefined
. Give this value to specify a path to a value in a nested object. -
alwaysQuote
<boolean>
(optional)Default:
false
. Set it totrue
to double-quote all fields regardless of their values.
-
Returns:
<ObjectCsvStringifier>
ObjectCsvStringifier#getHeaderString()
Returns:
<string>
ObjectCsvStringifier#stringifyRecords(records)
Parameters:
- records
<Array<Object>>
Returns:
<string>
createArrayCsvStringifier(params)
Parameters:
- params
<Object>
-
header
<Array<string>>
(optional)Array of field titles
-
fieldDelimiter
<string>
(optional)Default:
,
. Only either comma,
or semicolon;
is allowed. -
recordDelimiter
<string>
(optional)Default:
\n
. Only either LF (\n
) or CRLF (\r\n
) is allowed. -
alwaysQuote
<boolean>
(optional)Default:
false
. Set it totrue
to double-quote all fields regardless of their values.
-
Returns:
<ArrayCsvStringifier>
ArrayCsvStringifier#getHeaderString()
Returns:
<string>
ArrayCsvStringifier#stringifyRecords(records)
Parameters:
- records
<Array<Array<string>>>
Returns:
<string>
Request Features or Report Bugs
Feature requests and bug reports are very welcome: https://github.com/ryu1kn/csv-writer/issues
A couple of requests from me when you raise an issue on GitHub.
- Requesting a feature: Please try to provide the context of why you want the feature. Such as, in what situation the feature could help you and how, or how the lack of the feature is causing an inconvenience to you. I can't start thinking of introducing it until I understand how it helps you ????
- Reporting a bug: If you could provide a runnable code snippet that reproduces the bug, it would be very helpful!
Development
Prerequisite
- Node version 8 or above
- Docker
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
SAST tool detected but not run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Warn: 4 commits out of 20 are checked with a SAST tool
Reason
6 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
Reason
Found 1/12 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:42: update your workflow using https://app.stepsecurity.io/secureworkflow/ryu1kn/csv-writer/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:46: update your workflow using https://app.stepsecurity.io/secureworkflow/ryu1kn/csv-writer/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:57: update your workflow using https://app.stepsecurity.io/secureworkflow/ryu1kn/csv-writer/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:71: update your workflow using https://app.stepsecurity.io/secureworkflow/ryu1kn/csv-writer/codeql-analysis.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/csv-writer.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/ryu1kn/csv-writer/csv-writer.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/csv-writer.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/ryu1kn/csv-writer/csv-writer.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: test-integration/test.sh:15
- Warn: npmCommand not pinned by hash: test-integration/test.sh:16
- Warn: npmCommand not pinned by hash: .github/workflows/csv-writer.yml:19
- Info: 0 out of 6 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 3 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql-analysis.yml:28
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql-analysis.yml:29
- Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1
- Warn: no topLevel permission defined: .github/workflows/csv-writer.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3.3
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More