octet-stream / form-data

Spec-compliant FormData implementation for Node.js
https://www.npmjs.com/package/formdata-node
MIT License
142 stars 17 forks source link

v5 breaks tsx #68

Closed leebenmorris closed 1 year ago

leebenmorris commented 1 year ago

Hi, I am trying to use v5.0.1 in my project.

When I run the script with TSX I get the following error:

➜  ~/github-itv/ctc-telligence-ordering-service/test git:(CBTC-110-jira-xray-integration) ✗ tsx xray/index.ts       
node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/leemor1/github-itv/ctc-telligence-ordering-service/node_modules/formdata-node/package.json
    at __node_internal_captureLargerStackTrace (node:internal/errors:496:5)
    at new NodeError (node:internal/errors:405:5)
    at exportsNotFound (node:internal/modules/esm/resolve:359:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:639:13)
    at resolveExports (node:internal/modules/cjs/loader:567:36)
    at Module._findPath (node:internal/modules/cjs/loader:636:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1063:27)
    at u.default._resolveFilename (/Users/leemor1/Library/pnpm/global/5/.pnpm/@esbuild-kit+cjs-loader@2.4.2/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:1519)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18)
    at <anonymous> (/Users/leemor1/github-itv/ctc-telligence-ordering-service/test/xray/index.ts:5:26)
    at Object.<anonymous> (/Users/leemor1/github-itv/ctc-telligence-ordering-service/test/xray/index.ts:151:44)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Object.F (/Users/leemor1/Library/pnpm/global/5/.pnpm/@esbuild-kit+cjs-loader@2.4.2/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:941)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

v4 does not have this problem, and runs as expected.

octet-stream commented 1 year ago

Hi. Thanks for your feedback. I'll gonna look at your problem as soon as I can.

octet-stream commented 1 year ago

Can confirm this bug exists in minimal example.

octet-stream commented 1 year ago

Setting "type": "module" in package.json fixes the problem for me. Do you have one in your package.json?

octet-stream commented 1 year ago

This probably related issue: https://github.com/esbuild-kit/tsx/issues/239

Your module is compiled to cjs fi you don't have "type": "module" and then when tsx runs your script - this error is thrown. formdata-node does not support commonjs, so you need to either add "type": "module" to your package.json, or stay on the v4. I'm gonna close this as wontfix, because I have no plan to add cjs back any time soon.

Also, Node.js 16 reached its EOL, so you can as well use Node.js' own FormData implementation if you're targeting Node >= 18. Note that File class is available since v20: https://nodejs.org/api/globals.html#class-file

octet-stream commented 1 year ago

v6 brings back CJS support and it's available now, check out release notes for more information: https://github.com/octet-stream/form-data/releases/tag/v6.0.0