@d1g1tal/tsnode 0.1.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -1
- package/README.md +661 -38
- package/dist/api/index.d.ts +41 -0
- package/dist/api/index.js +3 -0
- package/dist/chunk-4VQTTHC7.js +1 -0
- package/dist/chunk-CWI5WFAA.js +1 -0
- package/dist/chunk-G3UY36AV.js +1 -0
- package/dist/chunk-PXHSM7AZ.js +1 -0
- package/dist/chunk-QJLUIPZW.js +1 -0
- package/dist/chunk-QZ6GOFQ4.js +1 -0
- package/dist/chunk-UY7ICRUU.js +1 -0
- package/dist/cli.js +5 -18
- package/dist/client-MB4FJ7RP.js +1 -0
- package/dist/loader.d.ts +1 -0
- package/dist/loader.js +1 -0
- package/dist/package-H7Z52REB.js +1 -0
- package/dist/preflight.js +1 -0
- package/dist/remove-argv-flags-YKV2LNF2.js +1 -0
- package/dist/repl.js +1 -0
- package/dist/run-J7WDXCNP.js +1 -0
- package/dist/run-eval-in-process-LFWU3AL6.js +1 -0
- package/dist/run-in-process-5OFAUP24.js +1 -0
- package/dist/server-TWLLBPU7.js +1 -0
- package/dist/suppress-warnings.js +1 -0
- package/dist/watch-KRAOMOO6.js +4 -0
- package/package.json +70 -50
- package/dist/cli.d.ts +0 -2
- package/dist/hooks.d.ts +0 -8
- package/dist/hooks.js +0 -217
package/README.md
CHANGED
|
@@ -1,67 +1,690 @@
|
|
|
1
1
|
# tsnode
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/D1g1talEntr0py/tsnode/actions/workflows/ci.yml)
|
|
4
|
+
[](https://github.com/D1g1talEntr0py/tsnode/actions/workflows/publish.yml)
|
|
5
|
+
[](https://www.npmjs.com/package/@d1g1tal/tsnode)
|
|
6
|
+
[](https://www.npmjs.com/package/@d1g1tal/tsnode)
|
|
7
|
+
[](./LICENSE)
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
ESM-only Node.js TypeScript runner.
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
```sh
|
|
12
|
+
tsnode foo.ts
|
|
13
|
+
```
|
|
8
14
|
|
|
9
|
-
|
|
15
|
+
Forked from [tsx](https://github.com/privatenumber/tsx) by [Hiroki Osame](https://github.com/privatenumber). This fork removes legacy dual-mode branching and focuses on a fast ESM-only path.
|
|
16
|
+
|
|
17
|
+
## Table of contents
|
|
18
|
+
|
|
19
|
+
- [What tsnode is](#what-tsnode-is)
|
|
20
|
+
- [How tsnode works](#how-tsnode-works)
|
|
21
|
+
- [Install](#install)
|
|
22
|
+
- [Quick start](#quick-start)
|
|
23
|
+
- [How to choose between tsnode and node --import](#how-to-choose-between-tsnode-and-node---import)
|
|
24
|
+
- [CLI argument rules](#cli-argument-rules)
|
|
25
|
+
- [Run a TypeScript file](#run-a-typescript-file)
|
|
26
|
+
- [Pass script arguments](#pass-script-arguments)
|
|
27
|
+
- [Eval code (-e)](#eval-code--e)
|
|
28
|
+
- [Print expression result (-p)](#print-expression-result--p)
|
|
29
|
+
- [REPL](#repl)
|
|
30
|
+
- [Watch mode](#watch-mode)
|
|
31
|
+
- [Node test runner with TypeScript](#node-test-runner-with-typescript)
|
|
32
|
+
- [Custom tsconfig path](#custom-tsconfig-path)
|
|
33
|
+
- [Disable transform cache](#disable-transform-cache)
|
|
34
|
+
- [Shell scripts](#shell-scripts)
|
|
35
|
+
- [Fast path: node --import](#fast-path-node---import)
|
|
36
|
+
- [Programmatic API](#programmatic-api)
|
|
37
|
+
- [register()](#register)
|
|
38
|
+
- [tsImport()](#tsimport)
|
|
39
|
+
- [Source maps and debugging](#source-maps-and-debugging)
|
|
40
|
+
- [Type-checking and compiler behavior](#type-checking-and-compiler-behavior)
|
|
41
|
+
- [Performance and cache behavior](#performance-and-cache-behavior)
|
|
42
|
+
- [ESM-only expectations](#esm-only-expectations)
|
|
43
|
+
- [CLI and environment reference](#cli-and-environment-reference)
|
|
44
|
+
- [Troubleshooting](#troubleshooting)
|
|
45
|
+
- [FAQ](#faq)
|
|
46
|
+
- [What was removed from tsx](#what-was-removed-from-tsx)
|
|
47
|
+
- [Requirements](#requirements)
|
|
48
|
+
- [License and attribution](#license-and-attribution)
|
|
49
|
+
|
|
50
|
+
## What tsnode is
|
|
51
|
+
|
|
52
|
+
`tsnode` runs TypeScript files directly in Node.js without requiring a separate build step first.
|
|
53
|
+
|
|
54
|
+
If you normally do this:
|
|
55
|
+
|
|
56
|
+
1. write `.ts`
|
|
57
|
+
2. compile to `.js`
|
|
58
|
+
3. run Node on compiled output
|
|
59
|
+
|
|
60
|
+
`tsnode` simplifies that into one step for runtime execution.
|
|
61
|
+
|
|
62
|
+
This project is intentionally ESM-only. That focus removes old compatibility branches and keeps runtime behavior and performance easier to reason about.
|
|
63
|
+
|
|
64
|
+
## How tsnode works
|
|
65
|
+
|
|
66
|
+
`tsnode` registers a Node loader hook. When Node requests a TypeScript module, that module is transformed on demand and executed.
|
|
67
|
+
|
|
68
|
+
Important mental model:
|
|
69
|
+
|
|
70
|
+
- It is runtime transpilation.
|
|
71
|
+
- It is not a bundler.
|
|
72
|
+
- It is not a type-checker.
|
|
73
|
+
- It is built for modern ESM workflows.
|
|
74
|
+
|
|
75
|
+
Most confusion around this library comes from expecting one of these:
|
|
76
|
+
|
|
77
|
+
- CommonJS runtime compatibility (`require` workflows)
|
|
78
|
+
- Type-checking while executing
|
|
79
|
+
- One single "best" command for every scenario
|
|
10
80
|
|
|
11
|
-
|
|
81
|
+
This README is organized to make each scenario explicit.
|
|
12
82
|
|
|
13
83
|
## Install
|
|
14
84
|
|
|
15
|
-
|
|
16
|
-
|
|
85
|
+
### Local project dependency (recommended)
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
pnpm add -D @d1g1tal/tsnode
|
|
89
|
+
# or
|
|
90
|
+
npm i -D @d1g1tal/tsnode
|
|
91
|
+
# or
|
|
92
|
+
yarn add -D @d1g1tal/tsnode
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Run from your project:
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
pnpm tsnode ./src/main.ts
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Global install
|
|
102
|
+
|
|
103
|
+
```sh
|
|
104
|
+
pnpm add -g @d1g1tal/tsnode
|
|
105
|
+
# or
|
|
106
|
+
npm i -g @d1g1tal/tsnode
|
|
107
|
+
# or
|
|
108
|
+
yarn global add @d1g1tal/tsnode
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Then:
|
|
112
|
+
|
|
113
|
+
```sh
|
|
114
|
+
tsnode ./main.ts
|
|
17
115
|
```
|
|
18
116
|
|
|
19
|
-
|
|
117
|
+
## Quick start
|
|
118
|
+
|
|
119
|
+
Use these commands as your baseline:
|
|
120
|
+
|
|
121
|
+
1. Run a file
|
|
20
122
|
|
|
21
|
-
```
|
|
22
|
-
|
|
123
|
+
```sh
|
|
124
|
+
tsnode ./script.ts
|
|
23
125
|
```
|
|
24
126
|
|
|
25
|
-
|
|
127
|
+
2. Watch and rerun on change
|
|
26
128
|
|
|
27
|
-
|
|
129
|
+
```sh
|
|
130
|
+
tsnode watch ./script.ts
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
3. Fastest one-off execution path
|
|
28
134
|
|
|
29
|
-
```
|
|
30
|
-
tsnode ./
|
|
135
|
+
```sh
|
|
136
|
+
node --import @d1g1tal/tsnode ./script.ts
|
|
31
137
|
```
|
|
32
138
|
|
|
33
|
-
|
|
139
|
+
4. Type-check in a separate step
|
|
34
140
|
|
|
35
|
-
```
|
|
36
|
-
|
|
141
|
+
```sh
|
|
142
|
+
tsc --noEmit
|
|
37
143
|
```
|
|
38
144
|
|
|
39
|
-
##
|
|
145
|
+
## How to choose between tsnode and node --import
|
|
146
|
+
|
|
147
|
+
This is the decision that matters most in day-to-day use.
|
|
148
|
+
|
|
149
|
+
Use `tsnode` when you want CLI features:
|
|
150
|
+
|
|
151
|
+
- `watch`
|
|
152
|
+
- `--test`
|
|
153
|
+
- REPL
|
|
154
|
+
- `-e` / `-p`
|
|
155
|
+
|
|
156
|
+
Use `node --import @d1g1tal/tsnode` when startup overhead is the main concern for plain file execution.
|
|
157
|
+
|
|
158
|
+
Rule of thumb:
|
|
159
|
+
|
|
160
|
+
- Feature-rich workflow: use `tsnode`
|
|
161
|
+
- Lowest startup overhead for direct script execution: use `node --import`
|
|
162
|
+
|
|
163
|
+
## CLI argument rules
|
|
164
|
+
|
|
165
|
+
Argument placement is the most common source of mistakes.
|
|
166
|
+
|
|
167
|
+
General pattern:
|
|
168
|
+
|
|
169
|
+
```sh
|
|
170
|
+
tsnode [tsnode/node flags] ./entry.ts [script args]
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Example:
|
|
174
|
+
|
|
175
|
+
```sh
|
|
176
|
+
tsnode --tsconfig ./tsconfig.scripts.json ./scripts/sync.ts --dry-run --verbose
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
How it is interpreted:
|
|
180
|
+
|
|
181
|
+
- `--tsconfig` configures runtime behavior
|
|
182
|
+
- `./scripts/sync.ts` is your script entrypoint
|
|
183
|
+
- `--dry-run --verbose` are received by your script via `process.argv`
|
|
184
|
+
|
|
185
|
+
## Run a TypeScript file
|
|
186
|
+
|
|
187
|
+
This is the core use case.
|
|
188
|
+
|
|
189
|
+
```sh
|
|
190
|
+
tsnode ./src/main.ts
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Where this is useful in real projects:
|
|
194
|
+
|
|
195
|
+
- migration scripts
|
|
196
|
+
- release scripts
|
|
197
|
+
- data utilities
|
|
198
|
+
- internal tooling commands
|
|
199
|
+
|
|
200
|
+
Real-world example:
|
|
201
|
+
|
|
202
|
+
```sh
|
|
203
|
+
tsnode ./scripts/migrate.ts --environment=staging
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
If your team keeps ops scripts in TypeScript, this is usually your default command.
|
|
207
|
+
|
|
208
|
+
## Pass script arguments
|
|
209
|
+
|
|
210
|
+
Arguments after the script path are passed directly to your script.
|
|
211
|
+
|
|
212
|
+
```sh
|
|
213
|
+
tsnode ./scripts/report.ts --since=2026-01-01 --format=json
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Script example:
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
console.log(process.argv.slice(2));
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Output:
|
|
223
|
+
|
|
224
|
+
```txt
|
|
225
|
+
[ '--since=2026-01-01', '--format=json' ]
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Where this helps:
|
|
229
|
+
|
|
230
|
+
- CI pipelines with parameterized scripts
|
|
231
|
+
- scheduled jobs with date windows
|
|
232
|
+
- safety controls like `--dry-run`
|
|
233
|
+
|
|
234
|
+
## Eval code (-e)
|
|
235
|
+
|
|
236
|
+
`-e` runs a TypeScript snippet directly from the command line.
|
|
237
|
+
|
|
238
|
+
```sh
|
|
239
|
+
tsnode -e 'const n: number = 42; console.log(n * 2)'
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
Use this when you need a quick experiment without creating a file.
|
|
243
|
+
|
|
244
|
+
Real-world examples:
|
|
245
|
+
|
|
246
|
+
- validating parser behavior against a sample
|
|
247
|
+
- quickly reproducing part of a bug
|
|
248
|
+
- trying a tiny data transform
|
|
249
|
+
|
|
250
|
+
## Print expression result (-p)
|
|
251
|
+
|
|
252
|
+
`-p` evaluates an expression and prints its result.
|
|
253
|
+
|
|
254
|
+
```sh
|
|
255
|
+
tsnode -p 'new Date(0).toISOString()'
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
This is ideal for one-liner checks, shell workflows, and quick normalization logic.
|
|
259
|
+
|
|
260
|
+
Real-world examples:
|
|
261
|
+
|
|
262
|
+
- date formatting checks
|
|
263
|
+
- quick path/string transformations
|
|
264
|
+
- tiny utility evaluations in terminal workflows
|
|
265
|
+
|
|
266
|
+
## REPL
|
|
267
|
+
|
|
268
|
+
Running `tsnode` with no arguments starts an interactive TypeScript REPL.
|
|
269
|
+
|
|
270
|
+
```sh
|
|
271
|
+
tsnode
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
This is great for trying ideas before writing files.
|
|
275
|
+
|
|
276
|
+
You still get normal Node REPL behavior:
|
|
277
|
+
|
|
278
|
+
- `.help`
|
|
279
|
+
- `.exit`
|
|
280
|
+
- tab completion
|
|
281
|
+
- `_` for last result
|
|
282
|
+
|
|
283
|
+
## Watch mode
|
|
284
|
+
|
|
285
|
+
Watch mode reruns your script whenever relevant files change.
|
|
286
|
+
|
|
287
|
+
```sh
|
|
288
|
+
tsnode watch ./src/main.ts
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Press `Return` to manually rerun.
|
|
292
|
+
|
|
293
|
+
This is useful when you are in a fast edit/run/debug loop.
|
|
294
|
+
|
|
295
|
+
Useful watch options:
|
|
296
|
+
|
|
297
|
+
| Flag | What it helps with |
|
|
298
|
+
|---|---|
|
|
299
|
+
| `--include <path>` | Add files outside import graph (for example config files) |
|
|
300
|
+
| `--exclude <path>` | Ignore generated or noisy files |
|
|
301
|
+
| `--clear-screen=false` | Keep previous output visible |
|
|
302
|
+
| `--no-cache` | Debug cache-sensitive behavior |
|
|
303
|
+
| `--tsconfig <path>` | Use a specific tsconfig for watch session |
|
|
304
|
+
|
|
305
|
+
Example:
|
|
306
|
+
|
|
307
|
+
```sh
|
|
308
|
+
tsnode watch \
|
|
309
|
+
--include ./config/runtime.json \
|
|
310
|
+
--exclude './generated/*' \
|
|
311
|
+
--clear-screen=false \
|
|
312
|
+
./src/server.ts
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## Node test runner with TypeScript
|
|
316
|
+
|
|
317
|
+
`tsnode --test` enables TypeScript execution for Node's built-in test runner.
|
|
318
|
+
|
|
319
|
+
```sh
|
|
320
|
+
tsnode --test
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Use this if you already prefer `node:test` and want to keep your test files in TypeScript.
|
|
324
|
+
|
|
325
|
+
Pattern-based example:
|
|
326
|
+
|
|
327
|
+
```sh
|
|
328
|
+
tsnode --test ./tests/**/*.test.ts
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
This keeps your test runtime simple without adding a separate compile phase just for tests.
|
|
332
|
+
|
|
333
|
+
## Custom tsconfig path
|
|
334
|
+
|
|
335
|
+
By default, tsnode finds `tsconfig.json` from the working directory. Use `--tsconfig` when that is not the config you want.
|
|
336
|
+
|
|
337
|
+
```sh
|
|
338
|
+
tsnode --tsconfig ./configs/tsconfig.scripts.json ./scripts/sync.ts
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
Typical cases:
|
|
342
|
+
|
|
343
|
+
- monorepos
|
|
344
|
+
- separate app/tooling/test tsconfig files
|
|
345
|
+
- dedicated script/runtime tsconfig
|
|
346
|
+
|
|
347
|
+
With `node --import`, use:
|
|
348
|
+
|
|
349
|
+
```sh
|
|
350
|
+
TSNODE_TSCONFIG_PATH=./configs/tsconfig.scripts.json node --import @d1g1tal/tsnode ./scripts/sync.ts
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
## Disable transform cache
|
|
354
|
+
|
|
355
|
+
In normal use, cache improves repeated execution. During debugging, a cache-free run can be useful.
|
|
356
|
+
|
|
357
|
+
```sh
|
|
358
|
+
tsnode --no-cache ./src/main.ts
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Use this when:
|
|
362
|
+
|
|
363
|
+
- validating cache invalidation behavior
|
|
364
|
+
- investigating stale cache suspicions
|
|
365
|
+
- collecting deterministic no-cache timing data
|
|
366
|
+
|
|
367
|
+
## Shell scripts
|
|
40
368
|
|
|
41
|
-
|
|
369
|
+
You can execute TypeScript files directly as shell scripts using a shebang.
|
|
370
|
+
|
|
371
|
+
```ts
|
|
372
|
+
#!/usr/bin/env tsnode
|
|
373
|
+
|
|
374
|
+
console.log('argv:', process.argv.slice(2));
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
Make it executable:
|
|
378
|
+
|
|
379
|
+
```sh
|
|
380
|
+
chmod +x ./script.ts
|
|
381
|
+
./script.ts hello world
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
This is especially useful for team automation scripts where TypeScript readability is preferable to complex shell script logic.
|
|
385
|
+
|
|
386
|
+
## Fast path: node --import
|
|
387
|
+
|
|
388
|
+
For low-overhead direct file execution, use:
|
|
389
|
+
|
|
390
|
+
```sh
|
|
391
|
+
node --import @d1g1tal/tsnode ./src/main.ts
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
Why it is often faster:
|
|
395
|
+
|
|
396
|
+
- tsnode CLI may spawn a child process depending on mode
|
|
397
|
+
- `node --import` runs in a single process
|
|
398
|
+
|
|
399
|
+
Good use cases:
|
|
400
|
+
|
|
401
|
+
- short-lived scripts called frequently
|
|
402
|
+
- Makefile / Docker commands that already run `node`
|
|
403
|
+
- startup-focused benchmarks
|
|
404
|
+
|
|
405
|
+
Set custom tsconfig for this mode:
|
|
406
|
+
|
|
407
|
+
```sh
|
|
408
|
+
TSNODE_TSCONFIG_PATH=./path/to/tsconfig.custom.json node --import @d1g1tal/tsnode ./main.ts
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
Inject through `NODE_OPTIONS` when another tool launches Node internally:
|
|
412
|
+
|
|
413
|
+
```sh
|
|
414
|
+
NODE_OPTIONS='--import @d1g1tal/tsnode' npx some-binary
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
Caveat: child Node processes inherit `NODE_OPTIONS`, which can add overhead in process-heavy workflows.
|
|
418
|
+
|
|
419
|
+
Optional helper function:
|
|
420
|
+
|
|
421
|
+
```sh
|
|
422
|
+
# ~/.bashrc or ~/.zshrc
|
|
423
|
+
tsnode() {
|
|
424
|
+
case "$1" in
|
|
425
|
+
""|watch|-e|--eval|-p|--print|--test)
|
|
426
|
+
command tsnode "$@"
|
|
427
|
+
;;
|
|
428
|
+
*)
|
|
429
|
+
node --import @d1g1tal/tsnode "$@"
|
|
430
|
+
;;
|
|
431
|
+
esac
|
|
432
|
+
}
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
## Programmatic API
|
|
436
|
+
|
|
437
|
+
```ts
|
|
438
|
+
import { register, tsImport } from '@d1g1tal/tsnode/api';
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
Use the API when you are embedding TypeScript loading into another runtime process.
|
|
442
|
+
|
|
443
|
+
### register()
|
|
444
|
+
|
|
445
|
+
`register()` installs loader hooks in the current process.
|
|
446
|
+
|
|
447
|
+
```ts
|
|
448
|
+
import { register } from '@d1g1tal/tsnode/api';
|
|
449
|
+
|
|
450
|
+
const { unregister } = register();
|
|
451
|
+
|
|
452
|
+
await import('./task.ts');
|
|
453
|
+
|
|
454
|
+
await unregister();
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
Where this is useful:
|
|
458
|
+
|
|
459
|
+
- worker processes loading TypeScript jobs
|
|
460
|
+
- plugin hosts
|
|
461
|
+
- runtime utilities that need temporary TS loading support
|
|
462
|
+
|
|
463
|
+
Scoped namespace example:
|
|
464
|
+
|
|
465
|
+
```ts
|
|
466
|
+
import { register } from '@d1g1tal/tsnode/api';
|
|
467
|
+
|
|
468
|
+
const api = register({ namespace: `tenant-${Date.now()}` });
|
|
469
|
+
|
|
470
|
+
const moduleA = await api.import('./plugin.ts', import.meta.url);
|
|
471
|
+
|
|
472
|
+
await api.unregister();
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
This helps isolate module loading between independent plugin/task runs.
|
|
476
|
+
|
|
477
|
+
Track imports with `onImport`:
|
|
478
|
+
|
|
479
|
+
```ts
|
|
480
|
+
import { register } from '@d1g1tal/tsnode/api';
|
|
481
|
+
|
|
482
|
+
register({
|
|
483
|
+
onImport(url) {
|
|
484
|
+
console.log('Loaded:', url);
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
### tsImport()
|
|
490
|
+
|
|
491
|
+
`tsImport()` dynamically imports a TypeScript module with targeted registration behavior.
|
|
492
|
+
|
|
493
|
+
Basic use:
|
|
494
|
+
|
|
495
|
+
```ts
|
|
496
|
+
import { tsImport } from '@d1g1tal/tsnode/api';
|
|
497
|
+
|
|
498
|
+
const loaded = await tsImport('./task.ts', import.meta.url);
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
Object form:
|
|
502
|
+
|
|
503
|
+
```ts
|
|
504
|
+
import { tsImport } from '@d1g1tal/tsnode/api';
|
|
505
|
+
|
|
506
|
+
const loaded = await tsImport('./task.ts', {
|
|
507
|
+
parentURL: import.meta.url,
|
|
508
|
+
tsconfig: './tsconfig.tools.json',
|
|
509
|
+
onImport(url) {
|
|
510
|
+
console.log(url);
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
Disable tsconfig lookup for a specific import:
|
|
516
|
+
|
|
517
|
+
```ts
|
|
518
|
+
await tsImport('./task.ts', {
|
|
519
|
+
parentURL: import.meta.url,
|
|
520
|
+
tsconfig: false
|
|
521
|
+
});
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
Use this when you want dynamic TS imports without permanently changing unrelated runtime imports.
|
|
525
|
+
|
|
526
|
+
## Source maps and debugging
|
|
527
|
+
|
|
528
|
+
Source maps turn stack traces and debugger locations back into TypeScript lines.
|
|
529
|
+
|
|
530
|
+
Source maps are enabled automatically when Node starts with:
|
|
531
|
+
|
|
532
|
+
- `--enable-source-maps`
|
|
533
|
+
- any `--inspect*` flag
|
|
534
|
+
- `NODE_V8_COVERAGE`
|
|
535
|
+
|
|
536
|
+
Force source maps on for non-debug runs:
|
|
537
|
+
|
|
538
|
+
```sh
|
|
539
|
+
TSNODE_SOURCE_MAPS=1 tsnode ./src/main.ts
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
## Type-checking and compiler behavior
|
|
543
|
+
|
|
544
|
+
tsnode runs code; it does not replace static type-checking.
|
|
545
|
+
|
|
546
|
+
Run type-checking separately:
|
|
547
|
+
|
|
548
|
+
```sh
|
|
549
|
+
tsc --noEmit
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
Recommended workflow:
|
|
553
|
+
|
|
554
|
+
- run with tsnode for execution speed
|
|
555
|
+
- validate with `tsc --noEmit` in CI/pre-commit
|
|
556
|
+
|
|
557
|
+
Compiler caveats inherited from esbuild:
|
|
558
|
+
|
|
559
|
+
- `eval()` compatibility semantics are not preserved
|
|
560
|
+
- only a subset of tsconfig options affect transforms
|
|
561
|
+
- `emitDecoratorMetadata` is not supported
|
|
562
|
+
|
|
563
|
+
References:
|
|
564
|
+
|
|
565
|
+
- [esbuild tsconfig support](https://esbuild.github.io/content-types/#tsconfig-json)
|
|
566
|
+
- [esbuild TypeScript caveats](https://esbuild.github.io/content-types/#typescript-caveats)
|
|
567
|
+
|
|
568
|
+
## Performance and cache behavior
|
|
569
|
+
|
|
570
|
+
This fork is optimized for fast ESM execution.
|
|
571
|
+
|
|
572
|
+
Practical guidance:
|
|
573
|
+
|
|
574
|
+
- warm runs usually benefit from cache reuse
|
|
575
|
+
- `--no-cache` is for diagnostics, not normal use
|
|
576
|
+
- `node --import` is usually best for startup-sensitive one-off runs
|
|
577
|
+
|
|
578
|
+
Benchmark whichever path matches your real workload:
|
|
579
|
+
|
|
580
|
+
- `tsnode ./file.ts`
|
|
581
|
+
- `node --import @d1g1tal/tsnode ./file.ts`
|
|
582
|
+
|
|
583
|
+
## ESM-only expectations
|
|
584
|
+
|
|
585
|
+
This runtime expects modern ESM usage.
|
|
586
|
+
|
|
587
|
+
In practice:
|
|
588
|
+
|
|
589
|
+
- use `import` / `export`
|
|
590
|
+
- do not rely on legacy CommonJS runtime patching
|
|
591
|
+
- align project scripts and tooling around ESM behavior
|
|
592
|
+
|
|
593
|
+
If migrating from mixed CJS/ESM code, convert runtime scripts to ESM first, then switch execution to tsnode.
|
|
594
|
+
|
|
595
|
+
## CLI and environment reference
|
|
596
|
+
|
|
597
|
+
### CLI flags
|
|
598
|
+
|
|
599
|
+
| Flag | Meaning |
|
|
600
|
+
|---|---|
|
|
601
|
+
| `--help`, `-h` | Show CLI help |
|
|
602
|
+
| `--version`, `-v` | Show tsnode version |
|
|
603
|
+
| `--tsconfig <path>` | Use a specific tsconfig |
|
|
604
|
+
| `--no-cache` | Disable transform cache |
|
|
605
|
+
| `--test` | Run Node test runner with TS support |
|
|
606
|
+
| `--eval`, `-e <code>` | Evaluate code |
|
|
607
|
+
| `--print`, `-p <expr>` | Evaluate and print expression |
|
|
608
|
+
|
|
609
|
+
Watch subcommand options:
|
|
610
|
+
|
|
611
|
+
| Flag | Meaning |
|
|
612
|
+
|---|---|
|
|
613
|
+
| `watch --include <path>` | Add extra watch targets |
|
|
614
|
+
| `watch --exclude <path>` | Exclude paths from watch |
|
|
615
|
+
| `watch --clear-screen=false` | Keep output between reruns |
|
|
616
|
+
|
|
617
|
+
### Environment variables
|
|
618
|
+
|
|
619
|
+
| Variable | Effect |
|
|
620
|
+
|---|---|
|
|
621
|
+
| `TSNODE_TSCONFIG_PATH` | Set tsconfig path (especially with `node --import`) |
|
|
622
|
+
| `TSNODE_SOURCE_MAPS=1` | Force source maps on |
|
|
623
|
+
| `NODE_OPTIONS=--import @d1g1tal/tsnode` | Inject loader into tools that launch Node internally |
|
|
624
|
+
|
|
625
|
+
## Troubleshooting
|
|
626
|
+
|
|
627
|
+
### Why are my types not being checked?
|
|
628
|
+
|
|
629
|
+
Because tsnode executes TypeScript but does not perform static type-checking. Run `tsc --noEmit` separately.
|
|
630
|
+
|
|
631
|
+
### Why is node --import faster than tsnode for my quick script?
|
|
632
|
+
|
|
633
|
+
`node --import` avoids some CLI/process overhead and is often faster for short-lived runs.
|
|
634
|
+
|
|
635
|
+
### Why does my CommonJS script not work?
|
|
636
|
+
|
|
637
|
+
This project is ESM-only. Convert runtime scripts to ESM module patterns.
|
|
638
|
+
|
|
639
|
+
### Why are my stack traces not mapped to .ts lines?
|
|
640
|
+
|
|
641
|
+
Enable source maps explicitly:
|
|
642
|
+
|
|
643
|
+
```sh
|
|
644
|
+
TSNODE_SOURCE_MAPS=1 tsnode ./src/main.ts
|
|
645
|
+
```
|
|
646
|
+
|
|
647
|
+
## FAQ
|
|
648
|
+
|
|
649
|
+
### Is this a drop-in replacement for every Node + TS setup?
|
|
650
|
+
|
|
651
|
+
It is a strong drop-in for ESM-first runtime workflows. It is not a drop-in for legacy CommonJS runtime setups.
|
|
652
|
+
|
|
653
|
+
### Do I need typescript installed at runtime?
|
|
654
|
+
|
|
655
|
+
No. Runtime transforms are handled by esbuild.
|
|
656
|
+
|
|
657
|
+
### Is watch mode available through node --import?
|
|
658
|
+
|
|
659
|
+
No. Watch mode is a tsnode CLI feature.
|
|
660
|
+
|
|
661
|
+
### Should I always use node --import?
|
|
662
|
+
|
|
663
|
+
Use it when startup overhead is your bottleneck. For watch/test/repl/eval/print workflows, use tsnode CLI.
|
|
664
|
+
|
|
665
|
+
## What was removed from tsx
|
|
666
|
+
|
|
667
|
+
- CommonJS runtime support (`require` patching, `tsx/cjs` entry)
|
|
668
|
+
- Legacy dual-mode API/extension compatibility layers
|
|
669
|
+
- Legacy export pre-parsing and old interop shims
|
|
670
|
+
- `package.json` type walking for module mode detection
|
|
671
|
+
|
|
672
|
+
This fork assumes TypeScript files execute as ESM.
|
|
673
|
+
|
|
674
|
+
## Requirements
|
|
42
675
|
|
|
43
|
-
-
|
|
44
|
-
- Relative paths without an extension resolving to `.ts`
|
|
45
|
-
- Directory imports resolving to `index.ts`
|
|
46
|
-
- `src/` aliases resolving from the nearest project root containing `tsconfig.json` or `package.json`
|
|
676
|
+
- Node.js >= 24.11.1
|
|
47
677
|
|
|
48
|
-
|
|
678
|
+
TypeScript runtime details:
|
|
49
679
|
|
|
50
|
-
-
|
|
51
|
-
-
|
|
52
|
-
-
|
|
680
|
+
- no local `typescript` package is required at runtime
|
|
681
|
+
- transforms are handled by esbuild (with native stripping where available)
|
|
682
|
+
- contributors to this repository use TypeScript 6.x for type-checking
|
|
53
683
|
|
|
54
|
-
##
|
|
684
|
+
## License and attribution
|
|
55
685
|
|
|
56
|
-
|
|
57
|
-
- The transpiler targets modern ESM output for Node.js rather than older runtimes
|
|
58
|
-
- Stage 3 decorators are downleveled during transpilation because current Node.js releases still do not execute that syntax directly
|
|
686
|
+
MIT.
|
|
59
687
|
|
|
60
|
-
|
|
688
|
+
Forked from [privatenumber/tsx](https://github.com/privatenumber/tsx), original work Copyright (c) Hiroki Osame.
|
|
61
689
|
|
|
62
|
-
|
|
63
|
-
pnpm run build
|
|
64
|
-
pnpm run test
|
|
65
|
-
pnpm run type-check
|
|
66
|
-
pnpm run release:check
|
|
67
|
-
```
|
|
690
|
+
See [LICENSE](./LICENSE).
|