Skip to content

Watch mode ​

Watch mode runs a script and reruns it when watched files change. Use it during development to see the result of edits without restarting the command yourself.

The examples assume tsx is installed in your project. Run them from the project directory.

Start watching ​

Save this file in your project directory:

file.ts

ts
console.log('Watching for changes')

Run:

sh
npx tsx watch ./file.ts

Initial output:

text
Watching for changes

tsx keeps watching after the script finishes or fails. Change the message in file.ts and save it: the next run prints the new message. If the previous run is still active, tsx stops it before starting the next run.

Press Return to rerun manually, or Ctrl+C to stop watching.

tsx watch is a tsx command, separate from Node.js's --watch flag.

Watch behavior ​

tsx watches the entry script and dependencies loaded while the script runs. A file that your program reads with a filesystem API is not automatically a watched module dependency. Use --include to watch those files.

By default, tsx ignores:

  • Hidden files and directories, such as .env and .git
  • Files inside node_modules, bower_components, and vendor

Watch paths are relative to the directory where you start the command. Watch mode does not watch every file in your project by default.

Customizing watched files ​

The script path is a required string. Arguments after it are passed to the script. Watch mode keeps the command running until you stop it.

OptionTypeDefaultBehavior
--include <path or glob>Repeatable stringNo additional pathsWatch additional files or directories, subject to the ignores above.
--exclude <path or glob>Repeatable stringNo additional exclusionsAdd paths to the default ignores.
--ignore <path or glob>Repeatable stringNo additional exclusionsDeprecated alias for --exclude.
--clear-screenBooleantrueClear terminal output before a rerun; set --clear-screen=false to retain output.
--tsconfig <path>StringNot set; detect from the working directoryUse a custom TypeScript configuration.
--no-cacheBooleanfalseDisable caching of transformed code.

Including files to watch ​

Use --include to watch additional files or directories. For a project with config.json and a templates directory alongside the file.ts example above, run:

sh
npx tsx watch --include ./config.json --include "./templates/**/*" ./file.ts

Changes to config.json or files under templates now trigger a rerun, even if the script does not import them. Default ignores and --exclude still apply to included paths.

Excluding files from watch ​

Use --exclude to prevent changes to selected files from triggering a rerun. For a project where file.ts imports generated.ts and writes debug.log, run:

sh
npx tsx watch --exclude ./generated.ts --exclude ./debug.log ./file.ts

Changes to generated.ts and debug.log do not trigger reruns. Excluding a file does not prevent the script from loading it.

Using glob patterns ​

Both --include and --exclude accept glob patterns. For a project where file.ts imports files under data, quote the pattern so your shell passes it to tsx instead of expanding it first:

sh
npx tsx watch --exclude "./data/**/*" ./file.ts

Changes under data do not trigger reruns.

Tips ​

tsx clears the terminal on rerun by default. To keep output from earlier runs visible, disable clearing:

sh
npx tsx watch --clear-screen=false ./file.ts

The file.ts example continues printing its message on each run, and earlier output remains visible. Place watch options before the script path and script arguments after it. To pass arguments to a script, follow the CLI argument syntax.