How can nodemon be made to work with WSL 2?

Root cause:

inotify is not fully supported in the 9P filesystem protocol on WSL2.

There are several github issues on the WSL project related to this, but perhaps the most relevant is #4739.

Possible Workarounds:

  1. Try nodemon -L (a.k.a. --legacy-watch) as Simperfy suggested.

  2. Try running from the default ext4 filesystem (e.g. mkdir -p $HOME/Projects/testserver). Note that a symlink to the Windows filesystem will still not work. As a bonus, the WSL ext4 filesystem will be much faster for file intensive operations like git.

    You can still access the source from Windows editors and tools through \\wsl$\.

  3. Use Visual Studio Code with the Remote-WSL extension to edit your source on the Windows filesystem. The easiest way to do this is by navigating in WSL to your project directory and running code ..

    Visual Studio Code’s WSL integration does trigger inotify for some reason.

  4. Downgrade the session to WSL1 if you don’t need any of the WSL2 features. I keep both WSL1 and WSL2 sessions around. The best way to do this is to create a backup of the session with wsl --export and wsl --import. You can switch the version of a WSL distro at any point with wsl --set-version.

    I did test this on WSL1 with a sample project under the Windows filesystem, and editing via something as basic as notepad.exe under Windows still triggered nodemon to restart.

Longer answer:

nodemon worked “out of the box” for me on WSL2 on the root (/) ext4 mount (e.g. $HOME/src/testserver).

It also worked correctly when I tried it under the default /mnt/c mount that WSL/WSL2 creates. Of course, /mnt/c is much slower under WSL2. Edit – It turns out that I was using Visual Studio Code when I attempted this. Editing from other Windows apps on the Windows filesystem did not trigger nodemon to restart.

But looking at the first line of your screenshot, I see that you are running this from /c/Users/…. I’m thinking maybe you created this (perhaps CIFS) mount to try to work around the WSL2 performance issues – It’s a common workaround.

I didn’t set up a CIFS mount, but I was able to reproduce your problem by mounting with (substituting your Windows username):

mkdir $HOME/mnttest
sudo mount -t drvfs 'C:' $HOME/mnttest
cd $HOME/mnttest/Users/Raj/Projects/testserver

Running nodemon from this mount failed in the same manner that you describe — Changes to the source did not trigger a restart.

However, running with nodemon -L on this mount did trigger a restart when source files were changed.

It also may be possible to fix the problem by mounting with different options, but I’m just not sure at this point. Edit – Seems unlikely, given the bug reports on this on Github.

Also, you may want to create some exports/backups of your WSL sessions. It’s too late at this point (for your previous install), but you could have run wsl.exe --export to create a backup of the Ubuntu 18.04/WSL1 filesystem before upgrading. You can also change the version of a particular distribution with wsl.exe --set-version. This could give you some better “before/after” test comparisons.

Leave a Comment