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
- Fix failing tests - close #5 — committed to vue-typed/vue-typed by budiadiono 7 years ago
- Fix "Unable to find type definition file for 'mocha' / 'node'" Fixes the "npm run test" commad error, based on a comment from: https://github.com/TypeStrong/ts-node/issues/216#issuecomment-266466104 — committed to MartinSGill/discovery-writer by MartinSGill 7 years ago
- Fix ts-node builds on Windows Fixes Windows builds using ts-node module, to resolve the following issue: https://github.com/TypeStrong/ts-node/issues/216 — committed to MartinSGill/cspell by MartinSGill 7 years ago
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/
):Running
tsc
works fine.ts-node
seems to be missing the global referencestsc
has.EDIT: Here’s another workaround, which seems a little better: https://github.com/TypeStrong/ts-node/issues/179#issuecomment-249025729
My fix for windows, after trying most suggestions, was to remove
types
and instead usein combination with adding this to my src/typings.d.ts
Tsnode 1.7.0 TypeScript 2.1.1 and 2.1.4
@danielpa9708 add
In your
compilerOptions
and be sure to have@types/node
installedI am getting this with
ts-node
2.1.2 andtypescript
2.4.1 (on node 6.10.3 on macOS). If I taketypescript
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):(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
Be aware that using this solutions, you may introduce some side effects / unexpected behavior: