.mts is a cool file extension (TypeScript ES modules)
What's that, a file extension named after me?! Or a file extension for TypeScript files that are always ES modules?
I was reading the
Prettier 2.5 announcement post
the other day
and noticed that
"Prettier will now format files with
.cts extensions as TypeScript."
.mts is not named after my first name (Matias).
It's for TypeScript files that are always ES modules.
that use the
that use the
- TypeScript might require special syntax for CommonJS modules (I'm not sure).
and TypeScript files use the
Such files can be either ES modules or CommonJS modules.
The TypeScript 4.5 Beta announcement post explains when the more precise file extensions can be useful (emphasis added):
Node.js supports a new setting in
"type"can be set to either
This setting controls whether
.jsfiles are interpreted as ES modules or CommonJS modules, and defaults to CommonJS when not set.
package.jsonis nice because it allows us to continue using the
.jsfile extensions which can be convenient; however, you will occasionally need to write a file that differs from what
typespecifies. You might also just prefer to always be explicit.
Node.js supports two extensions to help with this:
.mjsfiles are always ES modules, and
.cjsfiles are always CommonJS modules, and there's no way to override these.
In turn, TypeScript supports two new source file extensions:
There are more details in the first section of the TypeScript 4.5 Beta announcement post.
Some tools may also require or encourage the more precise file extensions.
Vite (a build tool)
supports JSX by default only in
(but not in
for performance reasons.
Can you think of a file extension that's named after you?