Skip to content

Instantly share code, notes, and snippets.

@hatton
Last active June 28, 2016 21:35
Show Gist options
  • Save hatton/32e7fd3d3b364ea6ad5d to your computer and use it in GitHub Desktop.
Save hatton/32e7fd3d3b364ea6ad5d to your computer and use it in GitHub Desktop.
Typescript Module Error CheatSheet

Relevant documentation: https://www.typescriptlang.org/docs/handbook/module-resolution.html

This was last updated based on Typescript version 1.7.5. While converting a largish project from typescript + es3 javascript to a pure babel/webpack-driven es6 project, we got some errors that were quite unclear:

#TS2036: "Cannot Find Module"

Alas this can mean several things:

##The path is wrong Remember if the file is in the same directory, you want import y from './foo', not import y from 'foo'

Missing Typings file

Typescript really, really wants a d.ts file to go with the modules import of es6 files. It wants it so that if it can't find one, it will say it can't find the module. Note: As you'd expect, d.ts files are not required of other typescript files, it can get the info it needs from the typescript. Note: though there does need to be a d.ts file, if it's sitting there next to the module, then you don't have a ///<reference>, the file can just be there in the same directory, because you won't actually specify the file extension when you do the import (see next item).

##You have included '.js' in the path Don't include the extension.

#TS2306: ____ "is not a module" We get this when the related d.ts file was written before the js was made into a module using "export" declarations. For example,

declare class LanguageData {

has to become

export class LanguageData {

#TS2307: Cannot find module From microsoft/TypeScript#3808:

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.

JQuery addons

These really seem to want to be in global space anyways:

import '../../node_modules/select2/dist/js/select2.js';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment