Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
huan opened this issue Oct 10, 2016 · 5 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@huan
Copy link

huan commented Oct 10, 2016

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
wechaty@0.4.0 /Users/zixia/git/wechaty
└── @types/ws@0.0.34 

$ ./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

@jonrimmer
Copy link

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

@mhegazy
Copy link
Contributor

mhegazy commented Oct 10, 2016

Thanks @jonrimmer. this is the correct workaround for typescript@2.0.

The issue has been fixed in typescript@next and should be part of typescript@2.1. Please see #11103 for more details.

@mhegazy mhegazy closed this as completed Oct 10, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 10, 2016
@huan
Copy link
Author

huan commented Oct 10, 2016

@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?

@mhegazy
Copy link
Contributor

mhegazy commented Oct 10, 2016

as a mentioned, it is a bug, reproted in #11103, and fixed. sorry about the confusion.

passing --moduleResolution node would address the issue.

@huan
Copy link
Author

huan commented Oct 10, 2016

@mhegazy got it. thanks!

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants