Quick Explanation
- 
.mjsand.cjsare a way to tell Node.js explicitly how to interpret your JavaScript files. - 
If you're using CommonJS modules with
require, you should use.cjs. - 
If you're using ES modules with
import, you should use.mjs. - 
.mtsand.ctsare TypeScript-specific extensions that tell TypeScript what to compile that specific file down to. - 
There are also versions of these extensions that are designed to contain jsx:
.mjsx,.cjsx,.mtsx, and.ctsx. - 
By default,
.jsfiles will be considered as CommonJS modules. This changes if you set"type": "module"in yourpackage.jsonfile. - 
When using TypeScript, you'll need to import code using the JAVASCRIPT version of the file extension.
 
// importing from foo.mts
import { foo } from "./foo.mjs";
// importing from bar.cts
import { bar } from "./bar.cjs";
// importing from baz.js
import { baz } from "./baz.js";
- If you're planning to make use of this, you should set 
module: "NodeNext"andmoduleResolution: "NodeNext"in yourtsconfig.jsonfile. This will make TypeScript respect the.mtsand.ctsextensions. 
{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}
- If you want TypeScript to ensure you use the correct module syntax, you can set 
verbatimModuleSyntax: truein yourtsconfig.jsonfile. This will make TypeScript throw an error if you use the wrong module syntax. 
{
  "compilerOptions": {
    "verbatimModuleSyntax": true
  }
}
- When 
verbatimModuleSyntaxis set totrue, you'll need to use TypeScript's version of CommonJS imports and exports in your.ctsfiles: 
import { foo } = require("./foo.cjs");
const example = 123;
export = example;Share this TypeScript Concept with your friends