node-csv: csv-parse@5 is not usable with webpack@5 with esm syntax

Describe the bug

csv-parse specifies

{
  "exports": {
    ".": {
      "import": "./lib/index.js",
      "require": "./dist/cjs/index.cjs"
    },
    "./sync": {
      "import": "./lib/sync.js",
      "require": "./dist/cjs/sync.cjs"
    }
  }
}

which is problematic. If I import csv-parse with

import { parse as csvParse } from 'csv-parse/sync';

results in

Module not found: Error: Can't resolve 'stream' in '/Users/carter/log-analysis/node_modules/.pnpm/csv-parse@5.0.1/node_modules/csv-parse/lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
	- install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "stream": false }

which forces me installing the stream polyfill

If I import csv-parse with

import { parse as csvParse } from 'csv-parse/dist/esm/sync';

results in

Module not found: Error: Package path ./dist/esm/sync is not exported from package /Users/carter/log-analysis/node_modules/csv-parse (see exports field in /Users/carter/log-analysis/node_modules/csv-parse/package.json)

because /dist/esm/sync is not exported

To Reproduce

import { parse as csvParse } from 'csv-parse/sync';

Additional context

csv-parse@5.0.1
webpack@5.64.1

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 16 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I’m having this exact problem, but in a nodejs app.

My node version is 16.13.0. I’m using require syntax instead of import above, but I’m getting similar error messages.

const parse = require('csv-parse/lib/sync')
// Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/sync' is not defined by "exports" in package.json
const parse = require('csv-parse/sync')
parse( ... )
// TypeError: parse is not a function

Does anyone still has issue with webpack and JavaScript or is that part now working (despite the ./browser/esm/* deprecation warning)?

This thread is not TypeScript related. Please open another issue of TS.

Thanks for your help. but the latest change doesn’t work well with TypeScript

Compiled with problems:

ERROR in src/views/data-manage/dictionary-manage/create-edit-modal/create-edit-modal.ts:1:35

TS2307: Cannot find module 'csv-parse/browser/esm/sync' or its corresponding type declarations.
  > 1 | import { parse as csvParse } from 'csv-parse/browser/esm/sync';
      |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2 | import { Form } from 'element-ui';
    3 |
    4 | import { DictionaryApi } from '@/apis';

Can be work around with:

declare module 'csv-parse/browser/esm/sync' {
  export * from 'csv-parse/dist/esm/sync';
}

Why not export csv-parse/dist/* directly?