json2csv icon indicating copy to clipboard operation
json2csv copied to clipboard

Error module stream in ESM build

Open Nisgrak opened this issue 3 years ago • 11 comments

Hi!

I'm using this lib in 5.0.6 version with Vite (using the ESM build) and have an error when load the Parser:

browser-external:stream:3 Uncaught Error: Module "stream" has been externalized for browser compatibility and cannot be accessed in client code.
    at Object.get (browser-external:stream:3)
    at json2csv.esm.js:1870

I'm not using the Streaming API, only the synchronous so don't know if it's possible to disabled.

Thanks!

Nisgrak avatar Sep 09 '21 21:09 Nisgrak

Maybe you can set this option in vite? https://vitejs.dev/config/#optimizedeps-exclude

Or the rollup options here https://vitejs.dev/config/#build-rollupoptions

knownasilya avatar Sep 10 '21 00:09 knownasilya

I'm trying with this, but happend the same error

optimizeDeps: {
    exclude: ["stream"]
  },

Nisgrak avatar Sep 10 '21 07:09 Nisgrak

Any disclosure for that? I'm facing the same issue.

juliolmuller avatar Nov 14 '21 01:11 juliolmuller

Can you reproduce the issue and upload it as a repo for us to look at?

knownasilya avatar Nov 14 '21 15:11 knownasilya

I found a temporary workaround using umd instead

import { parse } from 'json2csv/dist/json2csv.umd'

filipw01 avatar Nov 14 '21 17:11 filipw01

Using a resolve.alias configuration of { "json2csv": "json2csv/dist/json2csv.umd.js" } works as well and does not break the TypeScript definitions.

oschwede avatar Jan 19 '22 10:01 oschwede

The problem happens when packaging the library for frontend since it uses some Node libraries. Like the stream library.

In v6 this, will be changed to minimize the node-specific parts and we should reconsider how we expose the different APIs to clearly separate node APIs from standard JS APIs

juanjoDiaz avatar Jan 19 '22 19:01 juanjoDiaz

I found a temporary workaround using umd instead

import { parse } from 'json2csv/dist/json2csv.umd'

works for me

daxiaraoming avatar Mar 15 '22 07:03 daxiaraoming

Another solution could be to import directly the module that you want to use. Something like

import parser from 'json2csv/JSON2CSVParser'

Closing since a workaround has been given.

juanjoDiaz avatar Apr 25 '22 18:04 juanjoDiaz

took long enough....

v6, which has moved to a new repo and broken down into smaller packages is in esm.

Full docs here: https://juanjodiaz.github.io/json2csv/

juanjoDiaz avatar Sep 27 '22 16:09 juanjoDiaz

Still same issue with vite and json2csv@v6 (latest). But the UMD workaround still works.

TheJaredWilcurt avatar Jan 20 '23 03:01 TheJaredWilcurt