Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

532
Views
Intellisense types unavailable with CommonJS + node-fetch

Issue

Using the popular node-fetch library, Intellisense doesn't understand the return value of fetch().

Vim with coc:

inspecting the return type of fetch, I see const result: any

Same thing in VSCode:

same thing in VSCode

For most other libraries I have type completion, but this one does not work.

Things I tried

  1. Added jsconfig.json and tsconfig.json files
  2. Investigated the package, which exports a @types/index.d.ts
  3. Tried installing @types/node-fetch, which also falls out-of-date often
  4. Converted to import syntax, which works

Expected result

As I mentioned, changing to import syntax fixes the issue, but I want it to work with require too.

enter image description here

How can I have type completion on this library with CommonJS?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

const fetch = require('node-fetch');

fetch('https://github.com/')
    .then(res => res.text())
    .then(body => console.log(body));

This works no problem, have you checked the documentation?

over 4 years ago · Santiago Trujillo Report

0

This works for me:

import node_fetch = require("node-fetch");
const fetch = node_fetch.default;

fetch('https://github.com/')
    .then(res => res.text())
    .then(body => console.log(body));

(See https://www.typescriptlang.org/docs/handbook/modules.html)

Mind you, I think there must be a better way to get the default import...

over 4 years ago · Santiago Trujillo Report

0

Solution

const fetch = require('node-fetch').default

Explanation

To bridge fundamental differences between CommonJS and ES module syntax, require('node-fetch') returns an object with this shape:

node REPL showing the result of node-fetch as an object with a property called default

Notice the node-fetch type definition does export the fetch() function as the top level, but also exports a default property for compatibility with ES module syntax. Using this property fixes autocomplete issues.

enter image description here

This still seems like a bug to me, but this workaround does resolve the issue.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!