TypeScript Version:
2.0.3
Code
// A *self-contained* demonstration of the problem follows...
import * as WebSocket from 'ws'
const ws = new WebSocket('https://api.wechaty.io')
console.log('ws')
$ npm install @types/ws
[email protected] /Users/zixia/git/wechaty
└── @types/[email protected]
$ ./node_modules/.bin/tsc --target es6 t.ts
t.ts(1,28): error TS2307: Cannot find module 'ws'.
Expected behavior:
should found the type defination of ws
Actual behavior:
TS2307 error
It's not like the problem from @types/ws, because if i use another module like @types/express, will be the same error.
BTW: if i run without es6 target, it will ok, but I can't use async/await
$ ./node_modules/.bin/tsc t.ts # will be ok
You need to explicitly specify "node" module resolution strategy, as the default for ES6 target is "classic". E.g.
$ ./node_modules/.bin/tsc --target es6 --moduleResolution node t.ts
Thanks @jonrimmer. this is the correct workaround for [email protected].
The issue has been fixed in typescript@next and should be part of [email protected]. Please see https://github.com/Microsoft/TypeScript/issues/11103 for more details.
@jonrimmer thanks! this cost my serval hours to study... and had not found any solution until read from your post.
why tsc not to search @types by default?
as a mentioned, it is a bug, reproted in https://github.com/Microsoft/TypeScript/issues/11103, and fixed. sorry about the confusion.
passing --moduleResolution node would address the issue.
@mhegazy got it. thanks!
Most helpful comment
You need to explicitly specify "node" module resolution strategy, as the default for ES6 target is "classic". E.g.