{"id":11837,"date":"2025-01-23T05:53:29","date_gmt":"2025-01-23T05:53:29","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11837"},"modified":"2025-01-23T06:13:51","modified_gmt":"2025-01-23T06:13:51","slug":"watch-and-reload-ts-node-when-typescript-files-change","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/node\/watch-and-reload-ts-node-when-typescript-files-change","title":{"rendered":"How to Watch and Reload ts-node When TypeScript Files Change"},"content":{"rendered":"<h2>Solution 1 : Using Nodemon with ts-node<\/h2>\n<h3>Step 1: Install Required Packages<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"lighttpd\">npm install ts-node nodemon typescript --save-dev\r\n<\/pre>\n<h3>Step 2: Create a <strong>nodemon.json<\/strong> Configuration File<\/h3>\n<p>Add a <strong>nodemon.json<\/strong> file to your project root with the following content:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"lighttpd\">{\r\n  \"watch\": [\"src\"],  \/\/ Directory or files to watch\r\n  \"ext\": \"ts\",       \/\/ File extensions to watch\r\n  \"ignore\": [\"dist\"], \/\/ Directories to ignore\r\n  \"exec\": \"ts-node .\/src\/index.ts\" \/\/ Command to execute\r\n}\r\n<\/pre>\n<h3>Step 3: Run nodemon<\/h3>\n<p>Use nodemon to start your application:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"lighttpd\">npx nodemon\r\nOr\r\n\"scripts\": {\r\n  \"start\": \"npx nodemon\"\r\n}\r\n<\/pre>\n<p>Alternative if we dont want to create a nodemon.json file then we can execute following command directly or we can add it to package. Json.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"lighttpd\">npx nodemon --watch src --ext ts --exec \"ts-node .\/src\/index.ts\"\r\n<\/pre>\n<p><strong>Pros:-<\/strong><\/p>\n<ol>\n<li><b>Mature and Flexible<\/b><span style=\"font-weight: 400;\">:<\/span>\n<ul>\n<li><span style=\"font-weight: 400;\">nodemon<\/span><span style=\"font-weight: 400;\"> offers a wide range of configuration options, such as ignoring files, specifying extensions, and handling custom scripts.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">You can fine-tune what to watch or ignore, making it suitable for larger projects.<\/span><\/li>\n<\/ul>\n<\/li>\n<li><b>Compatible with Complex Workflows<\/b><span style=\"font-weight: 400;\">:<\/span>\n<ul>\n<li><span style=\"font-weight: 400;\">Works well with tools like preprocessors or additional build steps.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Can integrate with custom tasks (e.g., linting or running multiple commands).<\/span><\/li>\n<\/ul>\n<\/li>\n<li><b>Community Support<\/b><span style=\"font-weight: 400;\">:<\/span>\n<ul>\n<li><span style=\"font-weight: 400;\">Widely used, so you\u2019ll find plenty of resources, plugins, and fixes for common problems.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>Cons:-<\/strong><\/p>\n<ol>\n<li><b>Dependency Overhead<\/b><span style=\"font-weight: 400;\">:<\/span>\n<ul>\n<li><span style=\"font-weight: 400;\">Requires installing <\/span><span style=\"font-weight: 400;\">nodemon<\/span><span style=\"font-weight: 400;\"> and configuring it, which adds extra setup and dependencies.<\/span><\/li>\n<\/ul>\n<\/li>\n<li><b>Performance<\/b><span style=\"font-weight: 400;\">:<\/span>\n<ul>\n<li><span style=\"font-weight: 400;\">Slightly slower for very large projects since it monitors a broader range of files.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>Solution 2 : Using Node.js&#8217;s Built-in &#8211;watch Mode<\/h2>\n<p>We can use Node.js&#8217;s built-in &#8211;watch mode which monitors files for changes and restarts your application automatically. This feature is available from <strong>Node.js v18.11.0 <\/strong>and later.<br \/>\nWe can use this command to run ts-node directly using node js built in watch mode.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"lighttpd\">node --watch -r ts-node\/register .\/src\/index.ts\r\n<\/pre>\n<p><strong>Pros:-<\/strong><\/p>\n<ul>\n<li aria-level=\"1\"><b>Lightweight<\/b><b>:<\/b><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"2\"><span style=\"font-weight: 400;\">No need to install additional tools like <\/span><span style=\"font-weight: 400;\">nodemon<\/span><span style=\"font-weight: 400;\">. Ideal for smaller projects or minimal setups.<\/span><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"1\"><b>Built-In Simplicity:<\/b>\n<ul>\n<li aria-level=\"1\"><span style=\"font-weight: 400;\">Reduces dependency management.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li style=\"list-style-type: none;\"><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li aria-level=\"2\"><span style=\"font-weight: 400;\">The <\/span><span style=\"font-weight: 400;\">&#8211;watch<\/span><span style=\"font-weight: 400;\"> mode is easy to enable with no extra configuration.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li aria-level=\"1\"><b>Good for Small-to-Medium Projects:<\/b><\/li>\n<\/ul>\n<ol>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span style=\"font-weight: 400;\">Works well for straightforward applications without a lot of custom requirements.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>Cons:-<\/strong><\/p>\n<ul>\n<li aria-level=\"1\"><b>Less Configurable:<\/b><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"2\"><span style=\"font-weight: 400;\">You can&#8217;t exclude specific file types or directories unless you set environment variables (<\/span><span style=\"font-weight: 400;\">NODE_WATCH_IGNORE<\/span><span style=\"font-weight: 400;\">).<\/span>\n<ul>\n<li aria-level=\"2\"><span style=\"font-weight: 400;\">No support for additional workflows like running custom scripts.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li style=\"list-style-type: none;\"><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>Not Feature-Rich:<\/b><\/li>\n<\/ul>\n<ol>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><span style=\"font-weight: 400;\">Limited to restarting the application when files change. It lacks advanced capabilities like conditional restart logic or task runners.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>Recommendation<\/h2>\n<ol>\n<li><b>Use <\/b><b>nodemon<\/b><b> with <\/b><b>ts-node<\/b><b> if<\/b>:\n<ul>\n<li>You need advanced features like ignoring specific files or directories.<\/li>\n<li>You work on large or complex projects with custom workflows.<\/li>\n<li>You prefer a mature and well-supported solution.<\/li>\n<\/ul>\n<\/li>\n<li><b>Use Node.js Watch Mode if<\/b>:\n<ul>\n<li>You prefer simplicity and fewer dependencies.<\/li>\n<li>You\u2019re working on a smaller project or want a lightweight setup.<\/li>\n<li>You\u2019re exploring Node.js\u2019s built-in capabilities for a minimalist approach.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Solution 1 : Using Nodemon with ts-node Step 1: Install Required Packages npm install ts-node nodemon typescript &#8211;save-dev Step 2: Create a nodemon.json Configuration File Add a nodemon.json file to your project root with the following content: { &#8220;watch&#8221;: [&#8220;src&#8221;], \/\/ Directory or files to watch &#8220;ext&#8221;: &#8220;ts&#8221;, \/\/ File extensions to watch &#8220;ignore&#8221;: [&#8220;dist&#8221;], [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11838,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-11837","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11837"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11837"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11837\/revisions"}],"predecessor-version":[{"id":11841,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11837\/revisions\/11841"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11838"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}