When deciding between CommonJS and native ECMAScript modules,
ts-node defaults to matching vanilla
behavior. This means TypeScript files are transformed according to your
"module" option and executed
according to node's rules for the
In some projects you may need to override this behavior for some files. For example, in a webpack
project, you may have
package.json configured with
"type": "module" and
"module": "esnext". However, webpack uses our CommonJS hook to execute your
so you need to force your webpack config and any supporting scripts to execute as CommonJS.
In these situations, our
moduleTypes option lets you override certain files, forcing execution as
CommonJS or ESM. Node supports similar overriding via
.mjs file extensions, but
.ts files cannot use them.
moduleTypes achieves the same effect, and also overrides your
"module" config appropriately.
The following example tells
ts-node to execute a webpack config as CommonJS:
Each key is a glob pattern with the same syntax as tsconfig's
When multiple patterns match the same file, the last pattern takes precedence.
cjsoverrides matches files to compile and execute as CommonJS.
esmoverrides matches files to compile and execute as native ECMAScript modules.
packageresets either of the above to default behavior, which obeys
This feature is meant to facilitate scenarios where normal
package.json configuration is not possible. For example, a
webpack.config.ts cannot be given its own
package.json to override
"type". Wherever possible you should favor using traditional