Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

432
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda