You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For the last few years, Node.js has been working to support running ECMAScript modules (ESM). This has been a very difficult feature to support, since the foundation of the Node.js ecosystem is built on a different module system called CommonJS (CJS). Interoperating between the two brings large challenges, with many new features to juggle; however, support for ESM in Node.js is now largely implemented in Node.js 12 and later, and the dust has begun to settle.
104
+
105
+
That’s why TypeScript 4.5 brings two new module settings: node12 and nodenext.
106
+
```json
107
+
{
108
+
"compilerOptions": {
109
+
"module": "nodenext",
110
+
}
111
+
}
112
+
```
113
+
114
+
These new modes bring a few high-level features which we’ll explore here.
115
+
116
+
type in package.json and New Extensions
117
+
Node.js supports a new setting in package.json called type. "type" can be set to either "module" or "commonjs".
118
+
```json
119
+
{
120
+
"name": "my-package",
121
+
"type": "module",
122
+
123
+
"//": "...",
124
+
"dependencies": {
125
+
}
126
+
}
127
+
```
128
+
### `.mjs`, `.cjs`, == `.mts`, `.cts` && `.d.mts` and `.d.cts`.
129
+
130
+
Node.js supports two extensions to help with this: `.mjs` and `.cjs`. `.mjs` files are always ES modules, and `.cjs` files are always CommonJS modules, and there’s no way to override these.
131
+
132
+
In turn, TypeScript supports two new source file extensions: .mts and `.cts`. When TypeScript emits these to JavaScript files, it will emit them to `.mjs` and `.cjs` respectively.
133
+
134
+
Furthermore, TypeScript also supports two new declaration file extensions: .d.mts and .d.cts. When TypeScript generates declaration files for `.mts` and `.cts`, their corresponding extensions will be `.d.mts` and `.d.cts`.
80
135
136
+
Using these extensions is entirely optional, but will often be useful even if you choose not to use them as part of your primary workflow.
0 commit comments