microsoft / microsoft/TypeScript
Using `"allowJs": true` and `"module": "commonjs"` to transform `.mjs` files should emit `.cjs` files
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
CommonJS, allowJS, ESM, CJS, file extensions
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about output "file extensions for CommonJS modules".
⏯ Playground Link
Sandbox link with relevant code (I couldn't use the playground, since this involves using an .mjs file).
💻 Code
// package.json
{
"name": "tsc-mjs-extension-bug",
"version": "0.1.0",
"type": "commonjs",
"main": "dist/javascript.mjs",
"scripts": {
"build": "tsc",
"test": "node ."
},
"dependencies": {
"typescript": "5.1.3"
}
}
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "nodenext",
"allowJs": true,
"outDir": "dist"
}
"include": ["src"]
}
// src/javascript.mjs
export const where = "javascript";
console.log(`Hello ${where}`);
// src/typescript.ts
export const where = "typescript";
console.log(`Hello ${where}`);
🙁 Actual behavior
The javascript.mjs is transformed to CommonJS (as expected), but the file extension of the emitted file is still .mjs.
This breaks the package, since .mjs is supposed to be use exclusively for JavaScript using ESM and the emitted file now uses CommonJS.
🙂 Expected behavior
I would expect tsc to transform the src/javascript.mjs to CommonJS and either:
- emit it as
dist/javascript.cjsor alternatively - take into account the
"type": "commonjs"in the package.json and emit the file as.jsas it does with thesrc/typescript.tsfile.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with package.json, tsconfig.json, src/javascript.mjs, and src/typescript.ts from the report, then run the build and test scripts. Compare the emitted extensions and module formats in dist; done means the .mjs input transformed to CommonJS is emitted with an extension that is valid for that format, with coverage for the chosen behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100