Typescript: "error TS2307: Cannot find module" when using @types/ws with typescript 2.0

Created on 10 Oct 2016  ·  5Comments  ·  Source: microsoft/TypeScript

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

http://stackoverflow.com/questions/39960436/error-ts2307-cannot-find-module-when-using-types-ws-with-typescript-2-0

Duplicate

Most helpful comment

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

All 5 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

siddjain picture siddjain  ·  3Comments

weswigham picture weswigham  ·  3Comments

zhuravlikjb picture zhuravlikjb  ·  3Comments

bgrieder picture bgrieder  ·  3Comments

kyasbal-1994 picture kyasbal-1994  ·  3Comments