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
*`-r, --require [path]` Require a node module before execution
322
324
*`--cwd` Behave as if invoked in this working directory <br/>*Default:*`process.cwd()`<br/>*Environment:*`TS_NODE_CWD`
323
325
*`--emit` Emit output files into `.ts-node` directory <br/>*Default:*`false` <br/>*Environment:*`TS_NODE_EMIT`
326
+
*`--scope` Scope compiler to files within `scopeDir`. Anything outside this directory is ignored. <br/>\*Default: `false` <br/>*Environment:*`TS_NODE_SCOPE`
327
+
*`--scopeDir` Directory within which compiler is limited when `scope` is enabled. <br/>*Default:* First of: `tsconfig.json` "rootDir" if specified, directory containing `tsconfig.json`, or cwd if no `tsconfig.json` is loaded.<br/>*Environment:*`TS_NODE_SCOPE_DIR`
328
+
*`moduleType` Override the module type of certain files, ignoring the `package.json``"type"` field. See [Module type overrides](#module-type-overrides) for details.<br/>*Default:* obeys `package.json``"type"` and `tsconfig.json``"module"` <br/>*Can only be specified via `tsconfig.json` or API.*
324
329
*`TS_NODE_HISTORY` Path to history file for REPL <br/>*Default:*`~/.ts_node_repl_history`<br/>
325
330
326
331
## API
@@ -671,6 +676,53 @@ To write your own transpiler integration, check our [API docs](https://typestron
671
676
Integrations are `require()`d, so they can be published to npm. The module must export a `create` function matching the
When deciding between CommonJS and native ECMAScript modules, `ts-node` defaults to matching vanilla `node` and `tsc`
682
+
behavior. This means TypeScript files are transformed according to your `tsconfig.json``"module"` option and executed
683
+
according to node's rules for the `package.json``"type"` field.
684
+
685
+
In some projects you may need to override this behavior for some files. For example, in a webpack
686
+
project, you may have `package.json` configured with `"type":"module"` and `tsconfig.json` with
687
+
`"module":"esnext"`. However, webpack uses our CommonJS hook to execute your `webpack.config.ts`,
688
+
so you need to force your webpack config and any supporting scripts to execute as CommonJS.
689
+
690
+
In these situations, our `moduleTypes` option lets you override certain files, forcing execution as
691
+
CommonJS or ESM. Node supports similar overriding via `.cjs` and `.mjs` file extensions, but `.ts` files cannot use them.
692
+
`moduleTypes` achieves the same effect, and *also* overrides your `tsconfig.json``"module"` config appropriately.
693
+
694
+
The following example tells `ts-node` to execute a webpack config as CommonJS:
695
+
696
+
```jsonc title=tsconfig.json
697
+
{
698
+
"ts-node": {
699
+
"transpileOnly":true,
700
+
"moduleTypes": {
701
+
"webpack.config.ts":"cjs",
702
+
// Globs are also supported with the same behavior as tsconfig "include"
703
+
"webpack-config-scripts/**/*":"cjs"
704
+
}
705
+
},
706
+
"compilerOptions": {
707
+
"module":"es2020",
708
+
"target":"es2020"
709
+
}
710
+
}
711
+
```
712
+
713
+
Each key is a glob pattern with the same syntax as tsconfig's `"include"` array.
714
+
When multiple patterns match the same file, the last pattern takes precedence.
715
+
716
+
*`cjs` overrides matches files to compile and execute as CommonJS.
717
+
*`esm` overrides matches files to compile and execute as native ECMAScript modules.
718
+
*`package` resets either of the above to default behavior, which obeys `package.json``"type"` and `tsconfig.json``"module"` options.
719
+
720
+
### Caveats
721
+
722
+
Files with an overridden module type are transformed with the same limitations as [`isolatedModules`](https://www.typescriptlang.org/tsconfig#isolatedModules). This will only affect rare cases such as using `const enum`s with [`preserveConstEnums`](https://www.typescriptlang.org/tsconfig#preserveConstEnums) disabled.
723
+
724
+
This feature is meant to faciliate scenarios where normal `compilerOptions` and `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 `package.json` and `tsconfig.json` configurations.
0 commit comments