Skip to content

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

@WangJi

Description

@WangJi

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions