Clarification on exports/imports
callmephilip opened this issue ยท 11 comments
Do I understand this correctly? To use the latest version of the library, we should be calling deep imports like so:
import createUploadLink from "apollo-upload-client/createUploadLink.mjs";
Yes, that's correct! It's optimal JavaScript module design.
Thank you for clarification, Jayden! Is there a way to address Could not find a declaration file for module 'apollo-upload-client/createUploadLink.mjs'
TS issue?
module resolution of this package conflicts with everything else in the project, what is a point to moving to this "optimal module design"?
@callmephilip you can see the right TypeScript config to use documented here:
https://github.com/jaydenseric/apollo-upload-client/tree/v18.0.1#requirements
@vorlov you seem to be confusing a few concepts. Optimal module design as a concept is still valid no matter if you are using CJS, ESM, or how you configure TypeScript. You say "module resolution of this package conflicts with everything else in the project", but, this package (apollo-upload-client
) isn't doing anything invalid; it's standard ESM and the recommended TS config to use for resolving, nodenext
, is what TypeScript themselves recommend. It's the only resolution mode that actually reflects how current versions of Node.js resolves things; it takes into account package exports
field, etc. I don't know what you mean by "conflicts", and by "the project", I assume you mean your project (and not apollo-upload-client
), but if you are having problems with using the standard format for JavaScript and the standard way Node.js resolves modules in your project I don't really care. That's your problem to fix; your project is wrong.
module resolution of this package conflicts with everything else in the project, what is a point to moving to this "optimal module design"?
he wrote it himself, that's why
I did as you said in the documentation, getting this issue after. what is your suggestion?
The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@apollo/client")' call instead.
To convert this file to an ECMAScript module, create a local package.json file with `{ "type": "module" }`.ts(1479)
@vorlov It's because you are trying to require an ESM module from inside your CJS module. In Node.js, ESM can import either ESM or CJS modules, but CJS modules can only require other CJS modules (unless you use a dynamic import). Learn more here:
- https://nodejs.org/api/esm.html#require
- https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
This is just how Node.js has worked for years; my package is not doing anything weird.
also I am importing like this import createUploadLink from 'apollo-upload-client/createUploadLink.mjs';
getting this Could not find a declaration file for module 'apollo-upload-client/createUploadLink.mjs'.
my ts config
"module": "nodenext",
"allowJs": true,
"maxNodeModuleJsDepth": 10,
checked documentation twice, maybe I am stupid but can't get it to work with ts
@vorlov Honestly for poor people like us who are still not an expert around different plugins using different module type and resolution strategy ( Vite + Vue in my case ), it's better to simply downgrade apollo-upload-client
to 17.0.0 and use it without any of this mess of following the recommended standard as mentioned in v18. Perhaps a day will come when we will understand the details of these configuration.