ts-node: "Cannot find module 'http'. (2307)" with "@types/node" under typescript@2.0.3

TypeScript Version:

2.0.3

Code

// A *self-contained* demonstration of the problem follows...
// t.ts
import * as http from 'http'
console.log(typeof http)
$ ./node_modules/.bin/ts-node -v
ts-node v1.4.1
node v6.0.0

$ ./node_modules/.bin/tsc -v     
Version 2.0.3

$ npm install @types/node
wechaty@0.4.0 /Users/zixia/git/wechaty
└── @types/node@6.0.45

$ ./node_modules/.bin/ts-node --no-cache t.ts

D:\cygwin64\home\zixia\git\wechaty\node_modules\ts-node\dist\index.js:166
                    throw new TSError(diagnosticList);
                          ^
TSError: ⨯ Unable to compile TypeScript
t.ts (1,23): Cannot find module 'http'. (2307)

Expected behavior:

should import http as well

Actual behavior:

TS2307 error

Another STRANGE behavior

if I add another @types into t.ts, it will work without any problem.

$ cat t.ts
import * as http from 'http'
console.log(typeof http)

import * as express from 'express'
console.log(typeof express)

zixia@zixia-desktop ~/git/wechaty
$ ./node_modules/.bin/ts-node --no-cache t.ts
object
function

very interesting, what’s wrong with me?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 9
  • Comments: 15 (3 by maintainers)

Commits related to this issue

Most upvoted comments

I have the same problem. It seems I can workaround it using the following directive at the top of my file (which is located in ./src/):

///<reference path="../node_modules/@types/node/index.d.ts"/>

Running tsc works fine. ts-node seems to be missing the global references tsc has.

EDIT: Here’s another workaround, which seems a little better: https://github.com/TypeStrong/ts-node/issues/179#issuecomment-249025729

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

My fix for windows, after trying most suggestions, was to remove types and instead use

"typeRoots": [
      "node_modules/@types"
    ],

in combination with adding this to my src/typings.d.ts

/// <reference types="@types/mocha" />

Tsnode 1.7.0 TypeScript 2.1.1 and 2.1.4

@danielpa9708 add

{
  "typeRoots": [
    "./node_modules/@types"
  ],
  "types": ["node"]
}

In your compilerOptions and be sure to have @types/node installed

I am getting this with ts-node 2.1.2 and typescript 2.4.1 (on node 6.10.3 on macOS). If I take typescript back to 2.3.4 it works.

A combination of ‘typeRoots’ and types worked for me on Windows (TypeScript@2.2.1, ts-node@2.1.0):

"compilerOptions": {
  "typeRoots": [
    "node_modules/@types"
  ],
  "types": [
    "mocha"
  ]
}

(On OS X, neither of these was needed, and I’m sure the same is true for Linux.)

The solution to this problem is simple than you think : 1 npm i -D typscript 2 npx tsc init 3 npm i ts-node 4 in youre package.js>script>start=“ts-node main-file.ts” 5 youre terminal > npm start

💯 🥇

@MrCrimp although that also solved it for me, the bug remains.

As per the documentation of TypeScript: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

By default all visible “@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, …/node_modules/@types/, …/…/node_modules/@types/, and so on.

Be aware that using this solutions, you may introduce some side effects / unexpected behavior:

If typesRoots is specified, only packages under typeRoots will be included.