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

430
Views
TypeScript does not infer the type of CommonJS import (through require())
  • My code runs in Node.js environment. I have installed @types/node. When I write import http = require('http') or import * as http from 'http', the type of the object I'm importing is inferred: enter image description here
  • However, when I write const fs = require('fs');, the type is any. Why is that? Can I do anything about it?

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

TL;DR

A possible fix for this is to import the types for Node.js.


Full Solution

The solution for this issue is to import the types for Node.js.

This is available as an npm package. It contains all of the types for Node.js.

To install it, you need to run the following command.

$ npm install --save-dev @types/node

The reason the --save-dev flag is on the install command is because it isn't supposed to be a dependency, but instead a dev (development) dependency. This will not install this package if, for example, your project is installed as an npm module.

After this, IntelliSense will automatically pick up the type definitions, and will add auto-correct your Node.js code, including doing some other handy things.


If the type definitions don't get picked up, then the best solution is to restart Visual Studio Code. If that doesn't work, make sure that this is in your package.json file.

{
  "devDependencies": {
    "@types/node": "^17.0.31"
  }
}

Note: The version was correct at the time of post.

If it isn't, then add that code in your package.json file, save it, and run the following command.

$ npm install

This should install it successfully.

about 4 years ago · Juan Pablo Isaza 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!