-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a dynamic cjs wrapper built in to node-fetch that import(..) #1327
Comments
import(..)
s
import(..)
s
The eval method is an acceptable for me and would solve the friction users have upgrading to 3.x. The only thing a developer using typescript would need to do is to replace import fetch, { RequestInit } from 'node-fetch'; with import fetch, { RequestInit } from 'node-fetch/cjs'; |
@jeremyVignelles how about this instead? // using type only imports (for typescript)
import type { RequestInit, Headers, Request, Response } from 'node-fetch';
import fetch from 'node-fetch/cjs'; |
OK for me, though I'd prefer to keep a single import per dependency. The more dependency you have in a project, the longer the section becomes and it can be a nightmare to read... |
I'm also wondering about the naming if |
It does export the same thing, that can be used the same way. The way it's done internally is an implementation detail.
As a consumer, I'd tend to interpret this as a "I can import this with require()" (We just need to make sure that it actually can 😄 ). As a consumer not knowing the difference between cjs and mjs, I'd just do what I'm told in the README anyway... That said, I'm ok with |
Well, yes but only the |
Indeed, the classes cannot be imported that way synchronously... What about something like: export const RequestPromise: Promise<typeof Request> = fetchPromise.then(module => module.Request); imported like: import fetch, { RequestPromise } from 'node-fetch/cjs';
// Later
const Request = await RequestPromise; The usage differs a lot now. Not sure if there's a better workaround available... |
// Later
const Request = await RequestPromise; cjs can't use top level await... if they do use |
That's what I meant by
Except for typescript where it's translated into a There should be a way to access the classes, but other than the workaround I suggested (using the |
Some trouble. It's a mess. I can't use ES modules in a serverless environment. The typescript compiles a dynamic import to |
This is fixed in the latest TypeScript beta, which should be out in ~1 month 🎉 https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/ |
still wondering about what we should be doing about this issue, would like to hear someones tough if this should be done before working on v4 |
The fact that typescript postponed the feature is disappointing, and we're stuck in the same mess as before. I'm not a fan of needing an await for importing classes, but user-made workarounds will have the same issue, and the classes are used in few use cases, right? I'd embed the workaround with proper warnings and in a separate file. Users must opt-in for the workaround until it is fixed by typescript. What else could we do? |
Originally posted by @jeremyVignelles in #1266 (comment)
I think this is a good solution that dose not involve introducing a bundler and compiling everything into one file and importing all dependencies and basically duplicating everything. or using complex tools such as
esbuild
,noesm
.This dynamic
import()
have been suggested too many already and seems like good temporary easy solution.I'm wondering if we should go ahead and introduce something like it built in to node-fetch. there is also the method such as lazy import only when needed: #1279 (comment) that i think is good solution also. it can have a faster boot up time. basically goes like this:
One take a way from developers who uses a bundler/test framework such as jest, webpack, rollup, ts-node, ts-node-dev etc is that they like to rewrite the
import()
statement torequire()
or something like it. therefore i think aeval/new Function
solution would be a good optionOne thing to note doe is that it won't export
Headers
,Request
andResponse
in a sync manner but most ppl only use the fetch method anyways(?) - would need to be documented that this dose not do the same thingThe text was updated successfully, but these errors were encountered: