如何使 nodemon 与 WSL 2 一起使用? [英] How can nodemon be made to work with WSL 2?
问题描述
自从使用 2020 年 4 月 10 日的 Windows 更新从 WSL 1 更新到 WSL 2(然后将 Ubuntu 18 更新到 Ubuntu 20)以来,我一直无法让 nodemon
热重载是项目目录中的文件更改.当我对 .js
文件进行任何更改时,不会重新启动服务器或在终端输出:
Ever since updating from WSL 1 to WSL 2 with the Windows 10 April 2020 update (and thereafter updating Ubuntu 18 to Ubuntu 20), I have not been able to get nodemon
to hot reload when there are file changes in the project's directory. When I make any changes to .js
files, there's no restarting of the server or output at the terminal:
我使用 nodemon
启动我的 Node.js 服务器,如下所示:
I start my Node.js server with nodemon
like this:
NODE_ENV=development DEBUG='knex:*' nodemon --verbose --inspect ./server.js"
如果它有用,这是我的server.js:
And in case its useful, here is my server.js:
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server started and listening on port ${PORT}`);
});
我什至不确定如何进一步解决此问题以获取有关正在发生的事情的更多有用信息.
I am not even sure how to troubleshoot this further to get more useful information about what's going on.
推荐答案
根本原因:
WSL2 上的 9P 文件系统协议不完全支持 inotify.
Root cause:
inotify is not fully supported in the 9P filesystem protocol on WSL2.
WSL 项目上有几个与此相关的 github 问题,但也许最相关的是 #4739.
There are several github issues on the WSL project related to this, but perhaps the most relevant is #4739.
按照 Simperfy 的建议尝试
nodemon -L
(又名--legacy-watch
).
Try
nodemon -L
(a.k.a.--legacy-watch
) as Simperfy suggested.
尝试从默认的 ext4 文件系统(例如 mkdir -p $HOME/Projects/testserver
)运行.请注意,指向 Windows 文件系统的符号链接仍然无效.作为奖励,WSL ext4 文件系统对于 git 等文件密集型操作将快得多.
您仍然可以通过 \\wsl$\
从 Windows 编辑器和工具访问源代码.
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$\
.
将 Visual Studio Code 与 Remote-WSL 一起使用扩展以在 Windows 文件系统上编辑您的源代码.最简单的方法是在 WSL 中导航到您的项目目录并运行 code .
.
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 的 WSL 集成确实会触发 inotify.
Visual Studio Code's WSL integration does trigger inotify for some reason.
如果您不需要任何 WSL2 功能,请将会话降级到 WSL1.我同时保留 WSL1 和 WSL2 会话.最好的方法是使用 wsl --export
和 wsl --import
创建会话的备份.您可以随时使用 wsl --set-version
切换 WSL 发行版的版本.
我确实在 WSL1 上使用 Windows 文件系统下的示例项目对此进行了测试,并且在 Windows 下通过诸如 notepad.exe 之类的基本内容进行编辑仍会触发 nodemon 重新启动.
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.
更长的答案:
nodemon 开箱即用"对于我在根 (/
) ext4 挂载上的 WSL2(例如 $HOME/src/testserver
).
Longer answer:
nodemon worked "out of the box" for me on WSL2 on the root (/
) ext4 mount (e.g. $HOME/src/testserver
).
当我在 WSL/WSL2 创建的默认 编辑 - 事实证明,我在尝试此操作时使用的是 Visual Studio Code.从 Windows 文件系统上的其他 Windows 应用程序进行编辑不会触发 nodemon 重新启动./mnt/c
挂载下尝试它时,它也能正常工作.当然,/mnt/c
在 WSL2 下慢很多.
It also worked correctly when I tried it under the default 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./mnt/c
mount that WSL/WSL2 creates. Of course, /mnt/c
is much slower under WSL2.
但是查看屏幕截图的第一行,我看到您正在从 /c/Users/
运行它.... 我想也许您创建了这个(可能是 CIFS)挂载尝试解决 WSL2 性能问题 - 这是一种常见的解决方法.
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.
我没有设置 CIFS 挂载,但我能够通过挂载(替换您的 Windows 用户名)来重现您的问题:
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
从该挂载运行 nodemon
以您描述的相同方式失败 -- 对源的更改未触发重新启动.
Running nodemon
from this mount failed in the same manner that you describe -- Changes to the source did not trigger a restart.
但是,在此挂载上运行 nodemon -L
did 会在更改源文件时触发重新启动.
However, running with nodemon -L
on this mount did trigger a restart when source files were changed.
也可以通过安装不同的选项来解决这个问题,但我现在不确定. 编辑 - 似乎不太可能,因为在 Github 上对此进行了错误报告.
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.
此外,您可能希望为 WSL 会话创建一些导出/备份.现在为时已晚(对于您之前的安装),但您可以运行 wsl.exe --export
在升级之前创建 Ubuntu 18.04/WSL1 文件系统的备份.您还可以使用 wsl.exe --set-version
更改特定发行版的版本.这可以给你一些更好的之前/之后"测试比较.
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.
这篇关于如何使 nodemon 与 WSL 2 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!