Typescript - Cannot find module 'http' on Visual Studio Code Typescript - Cannot find module 'http' on Visual Studio Code typescript typescript

Typescript - Cannot find module 'http' on Visual Studio Code


This worked for me:

npm install @types/node --save

I realise it's been a while since the OP, however this is a more updated answer in case someone stumbles across this problem.


Updated solution

This error occurs in Typescript because the http and the other modules of Node.js are written in Javascript. The Typescript compiler doesn't have information about the types and modules of libraries that are written in Javascript. To add this information, you need to include type declarations for the Node.js in your Typescript project.

Execute the following terminal command in your project's root directory:

npm install -D @types/node

That's it! Now the error should disappear.


What happens behind the scenes?

The above command will download type declaration files (.d.ts) for the Node.js. Now you can see the files in the directory ./node_modules/@types/node of your project and http.d.ts is one of them. In this file, you'll find declarations for the http module and all the types such as IncomingMessage, ServerResponse and others that are used in the HTTP server. This is how Typescript compiler and VS code use type declaration information to provide you with the type safety.

@types:

There is a community maintained repository calledDefinitelyTyped which contains type declaration files for a lot of old and new Javascript libraries such as Express, Sequelize, JQuery and many others. When you specify the @types package in your command, it means you are downloading the declaration types from the DefinitelyTyped repository.

-D flag:

The command will also automatically add the types for the Node.js in the devDependencies section of your package.json file as shown in the following code snippet:

{  ...  "devDependencies": {    ...    "@types/node": "^14.0.27"  }}

The -D flag ensures that the types go in the devDependencies section of the package.json file instead of the dependencies section. Because this package is required only in development not in production. Do not use --save flag as mentioned in other answers, because it adds the types dependency in the dependencies section of the package.json and bloats the server installation with unnecessary files.

That's it!


I have the same issues. It's similar to that:https://github.com/TypeStrong/ts-node/issues/216

After installing the typings with:

typings install dt~node --global --save

And then added this to my file:

///<reference path="../typings/globals/node/index.d.ts"/>

And suddenly it works.