Simon Trewhella
Simon Trewhella

Reputation: 2484

es6 modules to commonjs with typescript under node harmony flag

I'm using TypeScript (1.6) with node under the --harmony flag, so I'd like to transpile the es6 module syntax to commonjs.

From what I can tell, I can't do this with TypeScript 1.6. If I set my target to es6, and module to commonjs, I get a TypeScript error -

Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher.

Why won't TypeScript compile to commonjs with an ES6 target? I imagine a lot people want to do this since node doesn't support ES6 modules yet.

I'd thought the new moduleResolution compiler option might solve this issue, but it doesn't appear to do anything.

Currently, I'm having to use babel just to transpile the module syntax to commonjs, but I'd like to remove babel from my builds so I can take advantage of source maps.

Is there a way I can achieve this? NOTE: I do not want to transpile to ES5. I want my JS running as ES6 under the harmony flag. Thanks!

Upvotes: 6

Views: 3107

Answers (1)

Remo H. Jansen
Remo H. Jansen

Reputation: 24941

The TypeScript team will add support for what you are looking for in the next release. You can wait for a few weeks/months. Alternatively, you can use a Polyfill for the ES6 module loader:

There are more libraries like the ones above available online just check which one does the job for you until official support for --module with --target es6 arrives.

UPDATE

tsconfig.json

{
  "compilerOptions": {
    "target":"ES6",
    "moduleResolution": "classic",
  }
}
  • ES6 support with generators
  • No import stuff transpiling due to "moduleResolution": "classic"

Upvotes: 3

Related Questions