How do I watch and run a build?
Something similar to tsx watch src/index.ts, is there an option in esbuild? The --watch option will not run the file.
Node.js has a --watch option. You can do something like this:
# concurrently
esbuild --watch main.ts --bundle --platform=node --outdir=.
node --watch main.js
Thanks for the reply. How performant is that in comparison with let's say tsx or ts-node since both esbuild and Node are listening for file changes?
I didn't run benchmarks. However you may find some clue in their technical details:
-
tsxandts-nodeare using esm loaders andrequire.extensionsto hack into Node.js execution and the former runsesbuild.transform()on each loaded file. Their cold start is maybe faster than "build and run" since the IO payload is smaller than a full build. -
tsxuses a performant FS watcher (e.g. usefseventson macOS) to watch and restart the Node.js process. While esbuild uses its own polling-based watcher. Sotsxis maybe faster in responding to a file change.
However esbuild is quite fast, even a full build often only takes 0.5s. I don't think performance that matters in this situation. Note that the build mode can give you plugins and less dependencies.
node --watch main.js
Looks like it never ends up running node --watch dist/server.js. Here is what I did
"build:watch": "esbuild --watch src/server.ts --bundle --platform=node --outdir=dist",
"node:watch": "doppler run -- node --watch dist/server.js",
"dev": "pnpm build:watch && pnpm node:watch",
and the logs
pnpm dev
> pnpm build:watch && pnpm node:watch
> esbuild --watch src/server.ts --bundle --platform=node --outdir=dist
[watch] build finished, watching for changes...
The shell operator a && b means only when the former command exits successfully then the latter command will run. Obviously neither esbuild or node would exit in watch mode.
By concurrently I mean you can start 2 terminals to execute them individually, or using the concurrently npm package to do similar things.
By concurrently I mean you can start 2 terminals to execute them individually, or using the
concurrentlynpm package to do similar things.
My apologies, I miss understood. Well tbh, that doesn't sound 'developer friendly'. Guess I'll have to stick with tsx for now to watch and run my files. Thanks for the help!
Well tbh, that doesn't sound 'developer friendly'.
There're tons of tools around esbuild to make common tasks have good DX like tsx to execute scripts and vite to develop lib / websites. In fact I also wrote one which wraps esbuild's build mode and takes advantage of esbuild's builtin watch mode to respawn the Node.js process.
Well tbh, that doesn't sound 'developer friendly'.
There're tons of tools around esbuild to make common tasks have good DX like
tsxto execute scripts andviteto develop lib / websites. In fact I also wrote one which wraps esbuild'sbuildmode and takes advantage of esbuild's builtin watch mode to respawn the Node.js process.
Thanks for that