zemirco / json2csv

Convert json to csv with column titles
http://zemirco.github.io/json2csv
MIT License
2.72k stars 363 forks source link

Error module stream in ESM build #539

Open Nisgrak opened 3 years ago

Nisgrak commented 3 years ago

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!

knownasilya commented 3 years ago

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

Nisgrak commented 3 years ago

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

optimizeDeps: {
    exclude: ["stream"]
  },
juliolmuller commented 3 years ago

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

knownasilya commented 3 years ago

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

filipw01 commented 3 years ago

I found a temporary workaround using umd instead

import { parse } from 'json2csv/dist/json2csv.umd'
oschwede commented 2 years ago

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

juanjoDiaz commented 2 years ago

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

daxiaraoming commented 2 years ago

I found a temporary workaround using umd instead

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

works for me

juanjoDiaz commented 2 years ago

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 commented 2 years ago

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/

TheJaredWilcurt commented 1 year ago

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