CSVParse for Node.js

IssuesGitHub

ECMAScript modules (ESM) for newer browsers

The ESM distribution target the latest browsers with support for ECMAScript modules introduced by ES6 introduced by ES6.

Compared with the Node.js version, this distribution bundles polyfills to run outside of the Node.js environment.

Addtionnal information are available in the project ECMAScript modules documentation.

Usage

The files can be manually imported. They dont require any external dependencies and they are located inside the packages/csv-parse/dist/esm folder.

When using NPM to manage and load your modules, for example within webpack, use:

import {parse} from 'csv-parse/browser/esm/index.js';
// Or
import {parse} from 'csv-parse/browser/esm/sync.js';

Vanilla JavaScript

A working demo is available in the project demo/browser directory.

With Express, expose the files with:

const app = express();
app.use('/lib/parse/',
  express.static(`node_modules/csv-parse/dist/esm/`));
app.listen(3000);

The HTML code looks like:

<script type="module">
  import {parse} from '/lib/parse/index.js';
  parse(records, options, (err, data) => {
    console.info(data)
  });
</script>

If you wish to use the sync API, use:

<script type="module">
  import {parse} from '/lib/parse/sync.js';
  const data = parse(records, options);
</script>

Webpack module bundler

This distribution is compatible with webpack version 5. It comes with the Node.js polyfills. A working demo is shared on the project repository.

In your module, import the appropriate csv-parse module:

import {parse} from 'csv-parse/browser/esm/index.js';
// Or
import {parse} from 'csv-parse/browser/esm/sync.js';

The relevant webpack configuration looks like:

  {
    entry: './src/parse.js',
    mode: 'development',
    output: {
      filename: 'parse.js',
      path: path.resolve(__dirname, 'dist'),
    }
  },
  {
    entry: './src/parse_sync.js',
    mode: 'development',
    output: {
      filename: 'parse_sync.js',
      path: path.resolve(__dirname, 'dist'),
    }
  },