8

It seems that using tsc with compilerOptions.module set to None or CommonJS is producing the same transpiled code. How do these two differ? Why are there two options for producing (apparently) the same transpiled code?

1
  • What version of TypeScript are you using? Commented Aug 21, 2017 at 18:30

1 Answer 1

7

With module set to None, we cannot use imports, exports, or module augmentations (except for the way that @artem notes in the below comments.)

Here is the source of the message.

"Cannot use imports, exports, or module augmentations when '--module' is 'none'.": {
    "category": "Error",
    "code": 1148
},

With module set to CommonJS, if we choose not to use imports, exports, or module augmentations, then our output might look the same as it does when we set module to None.

Sign up to request clarification or add additional context in comments.

6 Comments

How strict is the statement "cannot use imports"? because I did not know this, and I used them anyway, the compiler did not error out, and the result was identical to setting module to CommonJS
@aryzing Good point. I tested just now. The ban on export seems strict whereas the use of import seems allowed.
It's actually "can't have any imports or exports at the top level". Imports and exports inside namespaces are fine because they don't export anything from .ts file at runtime, and as long as file remains 'not a module' module=None works fine. Long explanation here: stackoverflow.com/a/38666021/43848
@artem In a test using TypeScript 2.4.1, an import inside a namespace gave the error: "Import declarations in a namespace cannot reference a module." Inside foo.ts was namespace test { import "./bar"; } and inside bar.ts was namespace test { export const x = 10; }. The error occurred in foo.ts even when the bar.ts was empty. Can you provide an example of a working import inside a namespace when module is set to none?
with module=none you can use only import assignment for creating aliases, you can't import from modules. Example: file a.ts: namespace A { import b = B.b; console.log(b); file b.ts: namespace B { export var b = 'b' } compile with tsc --module none --outFile o.js b.ts a.ts . This part of language predates ES modules and is now mostly of historical interest. Also, when doing this, you are supposed to use reference directives to ensure proper initialization order at runtime.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.