Search code examples
javascripttypescriptwebstorm

Typescript and webstorm, missing --module flag


Editing a typescript project, that was created on VisualStudio, in WebStorm is replying this error:

Error:(1, 1) TS1148: Cannot compile external modules unless the '--module' flag is provided.

the code is simple like this:

in file Test.ts

class helloWorld{

}

export = helloWorld;

Thanks in advance for your help

EDITED

I forgot to mention that I was using a MAC.

Also after enter the post, on my quest to find the solution, I try to compile the web in VisualStudio Code for Mac, and I have the same problem there, that, make realize that the problem was not the IDE but something in common, that I didn't figure it out.

On fedemp answer this sentence trigger someting:

One is using the flag at compilation: tsc --module commonjs or --module amd.

So I go to Webstorm's Preferences -> Languages & Frameworks -> TypeScript and on the option "Command line options:" I added --module amd and done!


Solution

  • Check this: http://www.typescriptlang.org/Handbook#modules-export- It's the official documentation for Typescript, the section about export =.

    When you use export =, you are creating an external module to be consumed using AMD or CommonJS. That's why the compiler complains.

    You have to two options depending on your needs. One is using the flag at compilation: tsc --module commonjs or --module amd. Use this if you want to use NodeJS or require.js.

    The other is export class HelloWorld {... if you want to use your class in another typescript file.