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

4.7-beta nodenext, .cts compiles to CJS, but code can be ES6 module #48809

Closed
robogeek opened this issue Apr 22, 2022 · 6 comments
Closed

4.7-beta nodenext, .cts compiles to CJS, but code can be ES6 module #48809

robogeek opened this issue Apr 22, 2022 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@robogeek
Copy link

Bug Report

I read the 4.7.0-beta announcement on the Microsoft devblogs, and am trying to replicate the new behavior for file extensions. I created a test package, where the source files are cts.cts, index.ts, and mts.mts. I am expecting the first to be transpiled to CJS format in all cases, the last to transpile to ES6 format in all cases, and for index.ts to transpile to either CJS or ES6 format depending on the setting of module in tsconfig.json.

Instead ... the files do get named as expected, as cts.cjs, and index.js, and mts.mjs.

However, the content of these files are in either CJS or ES6 format depending on module in tsconfig.json.

🔎 Search Terms

On DuckDuckGo, I used "typescript nodenext" and found a StackOverflow question that was useful.

On GitHub, I simply put "nodenext".

🕗 Version & Regression Information

$ npx tsc --version
Version 4.7.0-beta

⏯ Playground Link

N/A

💻 Code

The code is not important to the bug report. But, the three files named above all have the same source, which is this wonderful piece of coding:

// These functions implement an API with local state
// This will be important in the hybrid module implementation
var count = 0;
export function next(): number { return count++; }
export function squared(): number { return count ** 2; }
export function reset(): void { count = 0; }

// These are simple functions

export default function(msg: string): string {
    return `Hello ${msg}`;
}
export function api1(msg: string): string { return msg; }

🙁 Actual behavior

As discussed above, the file names become cts.cjs, and index.js, and mts.mjs.

If the module tag in tsconfig.json is set to es2022, all these files transpile to ES6 format.

If the module tag is set to nodenext, all these files transpile to CJS format.

🙂 Expected behavior

I am expecting that with module set to nodenext that cts.cts transpiles to cts.cjs in CJS, index.ts transpiles to index.js in ES6 format, and mts.mts transpiles to mts.mjs in ES6 format.

For other settings of module the .cts and .mts should possibly throw errors since that's what the blog post says.

@The-Code-Monkey
Copy link

I just had this same issue and setting es2022 works whereas nodenext doesnt

@andrewbranch
Copy link
Member

@robogeek please post your full tsconfig.json

@andrewbranch andrewbranch added the Needs More Info The issue still hasn't been fully clarified label Apr 25, 2022
@robogeek
Copy link
Author

This is for a directory which is meant to be rendered as ES6 format. However, I believe the .cts file must be transpiled to CJS format, and the .mts file to MJS format. With this, every file is CJS format.

{
    "compilerOptions": {
        "lib": [ "es6", "es2021", "esnext" ],
        "target": "es2021",
        "module": "NodeNext",
        "moduleResolution": "Node",
        "outDir": "./dist-es",
        "rootDir": "./lib-es",
        "declaration": true,
        "declarationMap": true,
        "inlineSourceMap": true,
        "inlineSources": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
    },
    "include": [ "./lib-es/**/*" ]
}

Same happens if I use nodenext rather than NodeNext. The capitalized form is preferred because VSC tells me the lowercase nodenext is incorrect.

Setting it to es2022 as @The-Code-Monkey suggested results in all three files being in ES6 format.

Remember that, Node.js requires that .cjs files are in CJS format, and .mjs files are in ES6 format. What I'm showing is that depending on this setting, both the generated .cjs and generated .mjs files can be the wrong format.

@andrewbranch
Copy link
Member

Duplicate of #48835

@andrewbranch andrewbranch marked this as a duplicate of #48835 Apr 25, 2022
@andrewbranch andrewbranch added Duplicate An existing issue was already created and removed Needs More Info The issue still hasn't been fully clarified labels Apr 25, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@thetutlage
Copy link

Doesn't seems like a duplicate of #48835

In this issue, we are expecting .cts to be always commonJS and .mts to be allows ESM. Regardless of the module inside tsconfig.json and type inside packages.json file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants