Closed
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.