33

i'm using Visual Studio Code for NodeJS and Typescript development. If I'm writing this code:

import * as http from 'http'; 

The compile says error TS2307: Cannot find module 'http'.

How to handle this error?

Greetz

6
  • See typescriptlang.org/docs/handbook/module-resolution.html Commented Jul 18, 2016 at 11:34
  • i did. But i cannot resolve the problem from the doc. Commented Jul 18, 2016 at 12:28
  • You will have a better change of getting a usable answer if you describe what you already tried, and can provide a minimal working example. Commented Jul 18, 2016 at 13:40
  • Alle the way from the document. if I'm using var x = require('http'); function require not found. But i do dont understand this... http is a default module in node. So why typescript cannot handle it? Commented Jul 18, 2016 at 16:39
  • 1
    I have the same problem in WebStorm, if I do it in typescript it does not finds the module, if I do it in pure Javascript works fine. Commented Oct 31, 2016 at 22:53

5 Answers 5

78

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I am new to TypeScript. Can you explain what '@types/node' means. Or point to a good resource will also be helpful.
@IndikaK @types/node is a package containing TypeScript declarations of the Node runtime, it's common to require @types/packageName when developing TypeScript in order for non-TypeScript libraries to function within TypeScript (so you get type-checking/hinting at compile-time). devblogs.microsoft.com/typescript/writing-dts-files-for-types would demonstrate that concept better than I can explain, however feel free to reach out with any questions!
The required types are also included in other type @type/... packages. I was using express and installing @types/express fixed it for me.
It worked, but it seems that I have to repeat this for each new project. Can't VSCode have some sort of "project template" like the full VS, and I just click "Create new NodeJS TypeScript project" and all the necessary tsconfig, package.json, this dependency etc are automatically setup so that I can start writing code immediately without repeating this tedious steps in the console?
Types are only used in in dev and at build time, they should be installed with --save-dev.
|
18

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 called DefinitelyTyped 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!

Comments

5

I know this question is old, but I found out that this error occurs when the tsconfig.json file is missing or you have an empty ("types": [""]) array of "types" property in the compilerOptions.

The bellow configuration will fix your problem. Of course you need to have @types/node, you can add also more types there.

{
  "compilerOptions": {
    "types": ["node"]
  }
}

More information you can vision the link

Comments

1

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.

Comments

0

I had this issue in my Angular 12 project when my IDE's auto-complete inserted this import to a component file without me noticing:

import * as e from 'cors';

Removing this line solved the issue.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.