juanjoDiaz / json2csv

Flexible conversion between JSON and CSV
https://juanjodiaz.github.io/json2csv/
MIT License
296 stars 32 forks source link

Dependencies not found : @json2csv/formatters/default.js in BaseParser.js #21

Closed guillaumeprevost closed 1 year ago

guillaumeprevost commented 1 year ago

I run into an error after migrating from json2csv v5.0.7 (that was working nicely)

Using @json2csv/node v6.1.2 Node version v16.19.1

Here is the code I am using (within a VueJS / Nuxt web app) :

import { AsyncParser } from '@json2csv/node';
...
const parser = new AsyncParser();
const csv = await parser.parse(this.data).promise();

When trying to start the web app (npm run dev), I get this error :

 ERROR  Failed to compile with 5 errors

These dependencies were not found: 

* @json2csv/formatters/default.js in ../node_modules/@json2csv/plainjs/src/BaseParser.js 
* @json2csv/formatters/number.js in ../node_modules/@json2csv/plainjs/src/BaseParser.js
* @json2csv/formatters/object.js in ../node_modules/@json2csv/plainjs/src/BaseParser.js
* @json2csv/formatters/string.js in ../node_modules/@json2csv/plainjs/src/BaseParser.js
* @json2csv/formatters/symbol.js in ../node_modules/@json2csv/plainjs/src/BaseParser.js

And indeed, this is what I see when looking into my node_modules folder to check the json2csv BaseParser :

import defaultFormatter from '@json2csv/formatters/default.js';
import numberFormatterCtor from '@json2csv/formatters/number.js';
import stringFormatterCtor from '@json2csv/formatters/string.js';
import symbolFormatterCtor from '@json2csv/formatters/symbol.js';
import objectFormatterCtor from '@json2csv/formatters/object.js';

...

Looking at the files structure, there doesn't seem to be any JS file at the root of the package, only within dist / src.

image

Expected the sample code to compile and run Actual output : error message mentioned above

Am I doing something wrong here or is there really an import problem within the lib ?

ncoughlin commented 1 year ago

bump

juanjoDiaz commented 1 year ago

Hi guys,

Sorry for the slow response. I'm looking into this.

juanjoDiaz commented 1 year ago

Looking at the files structure, there doesn't seem to be any JS file at the root of the package, only within dist / src.

That is correct. You can see the routing here: https://github.com/juanjoDiaz/json2csv/blob/main/packages/formatters/package.json#L27-L36

I haven't been able to reproduce the issue. It just works for me... πŸ™

Can you create a public sample project that I can use to reproduce the issues?

guillaumeprevost commented 1 year ago

Have you tried it using Nuxt/VueJS ?

Unfortunately I don't have much extra time to create a repro repository, I have reverted to the previous version (unsupported) from package zemirco/json2csv

If I manage to find time for this I'll do it gladly !

iromanro commented 1 year ago

Running into this issue on my end as well. The imports being made from BaseParser.js are pointing to the root of formatters instead of the /src folder. Error when away when I added /src to the import path

jdddog commented 1 year ago

We had to add this to our jest.config.js moduleNameMapper to get it to map properly: "^@json2csv/formatters/(.*)\\.js$": "<rootDir>/node_modules/@json2csv/formatters/src/$1.js",

juanjoDiaz commented 1 year ago

Running into this issue on my end as well. The imports being made from BaseParser.js are pointing to the root of formatters instead of the /src folder. Error when away when I added /src to the import path

The src shouldn't be needed since the mapping is configured in package.json (See code)

We had to add this to our jest.config.js moduleNameMapper to get it to map properly: "^@json2csv/formatters/(.*)\\.js$": "<rootDir>/node_modules/@json2csv/formatters/src/$1.js",

That seems specific to jest (does jest not respect the exports setting?). However, the original question in this issue was about Vue/Nuxt. Does Vue/Nuxt use a resolver that doesn't respect exports?

As said, I can't reproduce. I sample project to reproduce will accelerate the resolution of this issue.

jdddog commented 1 year ago

@juanjoDiaz I could make a reproduction for jest if that's useful? I didn't have an issue importing it in our actual project though.

laakal commented 1 year ago

It has the same errors for Meteor Js. We are using absolute import. it may be reason.

Screenshot 2023-04-05 at 09 59 33
juanjoDiaz commented 1 year ago

@juanjoDiaz I could make a reproduction for jest if that's useful? I didn't have an issue importing it in our actual project though.

Please do.

Sorry guys, but without a repo to reproduce the issue, there isn't much that I can do πŸ˜•.

juanjoDiaz commented 1 year ago

I just released v7.0.0 which entirely rewrites @json2csv to typescript.

Is the issue still present?

upalexgill commented 1 year ago

@juanjoDiaz yes just tried this for a Vue/Jest project.

Shouldn't this be updated to import ts files? image

juanjoDiaz commented 1 year ago

Nope! πŸ™‚

This was a very controversial design choice made by typescript. You can read https://github.com/microsoft/TypeScript/issues/16577#issuecomment-703190339 or any of the other gazillion issues on their repo about this issue.

In short:

People went crazy around this topic for a while and the Typescript maintainers have been absolutely categorical in many occasions on that they will never implement the mapping from .ts to .js and that this is the way.

I know that is weird. But that Typescript πŸ€·β€β™‚οΈ

upalexgill commented 1 year ago

Thanks for the info @juanjoDiaz, good to know! πŸ‘

laakal commented 1 year ago

Same error v7 for Meteorjs v2.12

Screenshot 2023-05-31 at 11 24 30
juanjoDiaz commented 1 year ago

I've released v7.0.1 which imports formatters from the top level package. But really guys... without a repo for me to reproduce... I can't do anything else...

twinone commented 1 year ago

Hi there, same issue here. I don't have enough time to create a PoC but i'll share my tsconfig.json in case that helps or we can find a common pattern:

{
  "compilerOptions": {
    "target": "es2016",
    "experimentalDecorators": true,
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "strictFunctionTypes": false,
    "noImplicitThis": false,
    "useUnknownInCatchVariables": false,
    "noImplicitAny": false,
    "allowJs": true,
    "types": [
      "vuetify"
    ],
    "paths": {
      "@/*": ["src/*"]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ],
    "outDir": "dist",
  },
  "include": [
    "src/**/*",
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx",
  ],
  "exclude": [
    "node_modules",
    ...
  ]
}

I'll see if I can take a deeper dive next week.

Rapol commented 1 year ago

I also stumbled into this issue. My problem was when running test with jest@27.x.x. After updating jest to v29 the issue went away.

I made a reproduction repo before trying out to upgrade jest. Sharing the repo in case anyone is curious and wants to reproduce it:

https://github.com/Rapol/json2csv-issue

juanjoDiaz commented 1 year ago

Is this still an issue for anyone?

bantr-ryan commented 1 year ago

Popping in to say that I was facing similar issues as @Rapol, but upgrading to jest@29 also fixed my issues

juanjoDiaz commented 1 year ago

I'll close then.

If someone is still facing the issue, this can be reopenened.