I'm using TypeScript in JS files (with JSDoc) and I'm using node-fetch. My problem is that when I require node-fetch I get an error (TS2349) when using the function that is exported from node-fetch.
I've attachted a screenshot so you get an overview on how I'm using TypeScript and node-fetch.

I will also send my tsconfig.js file in case that it will help.
{
"compilerOptions": {
"noEmit": true,
"allowJs": true,
"checkJs": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true
}
}
I've done some research on what could be the problem. I can fix the problem by using exports = fetch instead but I don't know how that will effect when using it in ts files. Hence the issue. I'm guessing that this error occurs since I'm using CommonJS instead of ES6 modules.
Any help will be appreciated.
Regards
Dennis Skoko
@torstenwerner @nikcorg @vinaybedre @kyranet @AndrewLeedham @JasonLi914 @wilsonianb @southpolesteve
Hi @DennisSkoko you can add "allowSyntheticDefaultImports": true or "esModuleInterop": true to your compilerOptions (see the docs), not 100% if this is the best approach. Perhaps there is something wrong with the type definition for this module, not my strongest topic so I can't advise without doing some more reading.
I believe @AndrewLeedham's approach is correct and this should be closed
Apologies for the late response. I tried adding both allowSyntheticDefaultImports and esModuleInterop to the tsconfig.js file but with no luck. When reading the documentation, it feels like allowSyntheticDefaultImports should fix this while I think the esModuleInterop is only when TypeScript is compiling.
I will write the tsconfig.js file after I've added those two options:
{
"compilerOptions": {
"noEmit": true,
"allowJs": true,
"checkJs": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true
}
}
I had this same problem, and I fixed it by doing
const fetch = require('node-fetch').default
This workaround should work until the problem gets fixed - I don't know the conventions of @types in order to make a PR.
I could be wrong, but I don't think this is a @types issue at all. To me it looks like the root cause is importing from a javascript module into a commonjs context, which makes this a TypeScript issue.
It's been a long while since I've had to sail those kinds of straits, but I do remember pulling a lot of hairs when struggling to make importing default exports from modules work like default exports from commonjs modules.
I wish I could be of more assistance, but alas I have no experience with this problem in a TypeScript context. I can only suggest asking your question again on StackOverflow or in the TypeScript project.
Ok, thanks for the information. I personally don't have this issue anymore since I have removed TypeScript. If anyone still has this issue, look at the comment above by @nikcorg :)
Regards
The index.js file has this:
module.exports = exports = fetch;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = exports;
exports.Headers = Headers;
exports.Request = Request;
exports.Response = Response;
exports.FetchError = FetchError;
While the index.mjs and index.es.js have this:
export default fetch;
export { Headers, Request, Response, FetchError }
I am no typescript expert, it seems the 'regular' node version is not supported by @types/node-fetch
Most helpful comment
I could be wrong, but I don't think this is a
@typesissue at all. To me it looks like the root cause is importing from a javascript module into a commonjs context, which makes this a TypeScript issue.It's been a long while since I've had to sail those kinds of straits, but I do remember pulling a lot of hairs when struggling to make importing default exports from modules work like default exports from commonjs modules.
I wish I could be of more assistance, but alas I have no experience with this problem in a TypeScript context. I can only suggest asking your question again on StackOverflow or in the TypeScript project.