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
If you only need to add TypeScript & ESM support in a CommonJS context, you can use the CJS loader:
46
+
---
47
+
48
+
# CommonJS
49
+
50
+
If you only need to add TypeScript & ESM support in a CommonJS context.
51
+
52
+
## Command-line API
53
+
54
+
Pass tsx into the `--require` flag:
48
55
49
56
```sh
50
57
node --require tsx/cjs ./file.ts
51
58
```
59
+
60
+
## Node.js API
61
+
62
+
### Globally patching `require`
63
+
64
+
#### Enabling TSX Enhancement
65
+
66
+
To enhance the global `require()` function with TypeScript support, prepend your script with the following line:
67
+
68
+
```js
69
+
require('tsx/cjs')
70
+
```
71
+
72
+
#### Manual Registration & Unregistration
73
+
74
+
To manually register and unregister the TypeScript enhancement on the global `require()`:
75
+
76
+
```js
77
+
consttsx=require('tsx/cjs/api')
78
+
79
+
// Register tsx enhancement for all global require() calls
80
+
constunregister=tsx.register()
81
+
82
+
// Optionally, unregister the enhancement when needed
83
+
unregister()
84
+
```
85
+
86
+
### Isolated `require()`
87
+
88
+
In situations where TypeScript support is needed only for loading a specific file (e.g. within third-party packages) without affecting the global environment, you can utilize tsx's custom `require` function.
89
+
90
+
Note the current file path must be passed in as the second argument so it knows how to resolve relative paths.
0 commit comments