alexcaza / export-to-csv

Export a JS collection to CSV; written in TypeScript.
Other
207 stars 48 forks source link

Examples don't work in TypeScript for Node.js #87

Closed wheelerlaw closed 8 months ago

wheelerlaw commented 8 months ago

Describe the bug The examples in the readme do not work in Typescript for nodejs.

To Reproduce Steps to reproduce the behavior:

  1. Run touch example.ts
  2. Put the contents of the Node.js example in said file
  3. Create a package.json with the following in it:
    {
      "dependencies": {
        "@types/node": "^20.11.20",
        "export-to-csv": "^1.2.3",
        "typescript": "^5.3.3"
      }
    }
  4. Run yarn.
  5. Run node_modules/.bin/ts-node ./example.ts

Expected behavior The example would run.

Screenshots This is the output:

$ node_modules/.bin/ts-node ./example.ts
/home/wheeler/Repositories/Other/sample/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/wheeler/Repositories/Other/sample/node_modules/export-to-csv/output/index.js from /home/wheeler/Repositories/Other/sample/example.ts not supported.
Instead change the require of index.js in /home/wheeler/Repositories/Other/sample/example.ts to a dynamic import() which is available in all CommonJS modules.
    at require.extensions.<computed> [as .js] (/home/wheeler/Repositories/Other/sample/node_modules/ts-node/dist/index.js:851:20)
    at Object.<anonymous> (/home/wheeler/Repositories/Other/sample/example.ts:3:25)
    at m._compile (/home/wheeler/Repositories/Other/sample/node_modules/ts-node/dist/index.js:857:29)
    at require.extensions.<computed> [as .ts] (/home/wheeler/Repositories/Other/sample/node_modules/ts-node/dist/index.js:859:16)
    at phase4 (/home/wheeler/Repositories/Other/sample/node_modules/ts-node/dist/bin.js:466:20)
    at bootstrap (/home/wheeler/Repositories/Other/sample/node_modules/ts-node/dist/bin.js:54:12)
    at main (/home/wheeler/Repositories/Other/sample/node_modules/ts-node/dist/bin.js:33:12)
    at Object.<anonymous> (/home/wheeler/Repositories/Other/sample/node_modules/ts-node/dist/bin.js:579:5) {
  code: 'ERR_REQUIRE_ESM'
}

Desktop (please complete the following information):

Node.js v18.19.0, ts-node v10.9.2

alexcaza commented 8 months ago

Seems like this might be related to this issue with ts-node: https://github.com/TypeStrong/ts-node/issues/1997

I ran your example (with the necessary modifications below) with node --loader=ts-node/esm example.ts and it works as expected.

A few other things:

  1. Ensure you have "type": "module" in your package.json file.
  2. Ensure your moduleResolution is set to node16, nodenext or bundler in your tsconfig.json file.
dstorfer-cn commented 8 months ago

I wonder if this is related to the issue I'm having in React - my project outputs in Common JS. I just implemented this package in a project a few days ago. It builds and compiles completely fine and runs in Storybook too. But when I run a Jest test on it, I get this strange error:

  SyntaxError: Unexpected token 'export'

      1 | import React, { useEffect, useState } from 'react';
      2 | import i18next from 'i18next';
    > 3 | import { mkConfig, generateCsv, download } from 'export-to-csv';

It seems to be saying that it doesn't like the term "export" at the beginning of the index.d.ts file. These are the imports in my TSX file, not in my Jest test file. I have other imports in various TSX files that start with the word export, so I don't understand why it doesn't like this one.
I'm not using ts-node anywhere in my project, but I am using ts-jest, so maybe it's a similar issue? My tsconfig has "moduleResolution": "node" because I'm using CJS, and also my package.json is not type:module, again due to using CJS, but that hasn't been a problem for any of the other packages that have a d.ts starting with export.

If I change moduleResolution to anything else it tells me it's invalid because I'm using CJS.

dstorfer-cn commented 8 months ago

A-ha, and now I see why it doesn't work for me in CommonJS - right at the top of this package's description:

You can easily use this library in JavaScript as well. The bundle uses ES modules, which all modern browsers support.

alexcaza commented 8 months ago

I'll close this for now since this is largely out of my hands, and I haven't heard back from the OP. If the issue persists and more information comes to light, feel free to open another issue 😄

bradley329 commented 3 months ago

hi @dstorfer-cn, i hit the same issue as you, how do you eventually resolve the issue? im also using commonjs

dstorfer-cn commented 3 months ago

hi @dstorfer-cn, i hit the same issue as you, how do you eventually resolve the issue? im also using commonjs

You may have missed my post right after that. Turns out this library uses ES modules, which I think are counter to functioning in commonJS.