-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Search Terms
- cjs
- cjs extension
- cjs files
Suggestion
Treat files with the .cjs
file extension the same as the .js
file extension, but disallowing ES Module syntax within them.
Use Cases
In packages with:
"type": "module",
in package.json
, Node loads all .js
files as ES modules.
To load a file as a CommonJS module, one has to use the .cjs
file extension.
Examples
// foo.cjs
// $ExpectType typeof Bar
const Bar = require("./bar.cjs");
📝 Note: In order to currently have typed
.cjs
imports, this file has to be namedbar.cjs.d.ts
:
// bar.d.ts
declare class Bar {
constructor();
}
export = Bar;
// bar.cjs
module.exports = class Bar {
constructor() {
// do stuff
}
};
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Related Issues
Raynos, a-ejs, natemoo-re, glen-84, privatenumber and 7 more
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript