dotnet watch (like nodemon for NodeJS)

By July 1, 2018 No Comments

Updated July 1, 2018

With the release of dotnet core 2.1.3, they have built in the following dotnet tools. Watch is one of them. So to get started just create a project or upgrade a project to 2.1.3 and then simply add a ‘watch’ after your dotnet app and before the command.

dotnet watch run

nodemon is used in NodeJS applications to automatically reload the application when the source code changes, which allows the next request or app launch to run the latest code. For .NET developers, we have dotnet-watch. From their github, dotnet-watch is a file watcher for dotnet that restarts the specified application when changes in the source code are detected.

You can start from a new dotnet project with ‘dotnet new web’ or in your existing project. Then execute the following dotnet cli command to add the package and the latest version of the cli will automatically invoke a restore.

dotnet add package Microsoft.DotNet.Watcher.Tools

Next you need to go into the csproj file and edit the <PackageReference> node with Microsoft.DotNet.Watcher.Tools and change the node to a <DotNetCliToolReference>. This will allow you to execute the package from the CLI. As you make changes throughout the source code for your project, the watcher will automatically recompile the assembly and relaunch.

dotnet watch run

If you need to specify file types or folders to include or exclude, simply add a node under an ItemGroup in the .csproj and specify the Watch node’s attributes for Include and/or Exclude.

<ItemGroup> <!-- extends watching group to include *.js files --> 
    <Watch Include="**\*.js" Exclude="node_modules\**\*.js;$(DefaultExcludes)" />