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

Force TypeScript to be compiled in commonjs module when targeting ES6 #5252

Closed
WangJi opened this issue Oct 14, 2015 · 3 comments
Closed

Force TypeScript to be compiled in commonjs module when targeting ES6 #5252

WangJi opened this issue Oct 14, 2015 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@WangJi
Copy link

WangJi commented Oct 14, 2015

Hereis the scenario:

I'm using TS to write Node application. Since the newer Node version(4.x) now supports most ES6 features, I set tsconfig.json to be like this:

{
    "compilerOptions": {
        "target": "ES6"
    }
}

However, after compiled, these js files can not run in Node because import is still a reserved word and not supported by Node yet.
After I alter tsconfig.json to:

{
    "compilerOptions": {
        "target": "ES6",
        "module": "commonjs"
    }
}

then I get a compile error:

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

So I got stuck here: If I want to use es6 features such as generator,for of, I have to set compile target to es6, but if I do so, both ts and js files would have to be import and export in es6 way, which is not currently supported by Node.

For now, I'm using Babel to convert es6 files to es5. However, it seems too heavy, babel does a lot of polyfill work and the final generated codes are hard for human to read.

So I'm asking if there is some better way of doing this: benefit from writing ts in es6 syntax while directly compile code to js that Node.js can run.

@kitsonk
Copy link
Contributor

kitsonk commented Oct 14, 2015

Dupe of #4389 which is in master.

@WangJi
Copy link
Author

WangJi commented Oct 14, 2015

@kitsonk Thank you .
install typescript@next solve my issue.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Oct 14, 2015
@rudijs
Copy link

rudijs commented Oct 30, 2015

@WangJi Thank you too.

Google search for "typescript generators es5" lead me here, the install typescript@next sorts me too.

@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants