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

tsc shows error 'Cannot find external module', but it works !?! #3808

Closed
bradws opened this issue Jul 10, 2015 · 2 comments
Closed

tsc shows error 'Cannot find external module', but it works !?! #3808

bradws opened this issue Jul 10, 2015 · 2 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@bradws
Copy link

bradws commented Jul 10, 2015

Hi team,

I get an error in the typescript compiler which I cant seem to understand how to fix. The error is:
>ex.ts(3,27): error TS2307: Cannot find external module 'autobahn'.

I'm transpiling like:
>tsc -m commonjs -t es5 "./typings/autobahn/autobahn.d.ts" ex.ts
and i also tried
>tsc -m commonjs -t es5 ex.ts
..but funnily enough, it DOES produce the correct ES5 javascript source, and it runs and works fine ?!?

...and my code is simply:

/// <reference path="./typings/autobahn/autobahn.d.ts" />
import autobahn = require('autobahn');
class C {
    x: number;
}
var myC = new C();`

..and I execute with
>node ex

It's like tsc can't find the module/file 'autobahn' ???

On my Windows 7 box, I have the latest release of node and npm. I have used npm to install autobahnJS (just any standard javascript library https://github.com/tavendo/AutobahnJS), Typescript v1.5.0-beta (https://github.com/Microsoft/TypeScript) and tsd (the typescript definition manager https://github.com/DefinitelyTyped/tsd).

I have used tsd to install the autobahn definitely types (it put it into the default folder ./typings/autobahn) and I reference it in my source code as you can see above.

All seems to be ok. As i mentioned, it seems to transpile just fine, the ES5 javascript output is as expected, and it runs.

It's a bit annoying having these error messages - when there is no error? Whats going on?

Cheers;

@qc00
Copy link

qc00 commented Jul 10, 2015

This is actually quite subtle, the TS2307 error means it tried to look for a file called autobahn.ts and did not find it. It does this because autobahn.d.ts failed to declare the module as an "Ambient External Module".

You can write an additional d.ts file to import the incorrect d.ts and re-export the module. Please also consider submitting a patch to DefinitelyTyped.

@RyanCavanaugh RyanCavanaugh added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Jul 10, 2015
@RyanCavanaugh
Copy link
Member

As usual, @billccn's explanation is correct.

TypeScript will always try to emit code, even if it thinks it's not going to work. In your case the code was fine but the definition file was incomplete, so it turned out to be useful behavior.

@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
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

3 participants