题意:如何在 WebStorm 中运行 nodemon?
问题背景:
I would like to use nodemon from within the WebStorm IDE (version 7). Nodemon watches one or more files in my source folder and restarts the node process (an Express server in this case), when one of the source files changes.
我想在 WebStorm IDE(版本 7)中使用 nodemon。Nodemon 监视我源文件夹中的一个或多个文件,并在其中一个源文件更改时重启 Node 进程(在这种情况下是一个 Express 服务器)。
How do I configure WebStorm to use nodemon in a Run Configuration, so that the node process is automatically restarted?
我该如何配置 WebStorm 以在运行配置中使用 nodemon,使 Node 进程能够自动重启?
Without nodemon, I use the following configuration in WebStorm, but have to restart the node process whenever I change something in the source file:
没有 nodemon 的情况下,我在 WebStorm 中使用以下配置,但每当我更改源文件时都必须重启 Node 进程:
- Node interpreter:
/usr/local/bin/node Node 解释器:/usr/local/bin/node
- Working directory:
/Users/foo/test 工作目录
- JavaScript file:
server.js javascript 文件
This results in a Run Configuration that runs node server.js
in the specified directory.
这会生成一个在指定目录中运行 `node server.js` 的运行配置。
From command line, I can use the following command to use nodemon to watch for file changes: nodemon server.js
in the project directory.
从命令行,我可以使用以下命令来使用 nodemon 监视文件更改:`nodemon server.js` 在项目目录中。
How do I need to change the WebStorm configuration so that it also uses nodemon?
我需要如何更改 WebStorm 配置,以便它也使用 nodemon?
问题解决:
It looks like the workaround with --exec
isn't necessary anymore, at least when using the newest version of nodemon and Webstorm 7 or 8.
看起来使用 `--exec` 的变通方法不再必要了,至少在使用最新版本的 nodemon 和 WebStorm 7 或 8 时是这样。
All you have to do is specify your path to nodemon by obtaining its path with running which nodemon
in your console (e.g. /usr/local/bin/nodemon
) under "Node parameters":
您只需通过在控制台中运行 `which nodemon` 来获取 nodemon 的路径(例如 `/usr/local/bin/nodemon`),然后在“Node 参数”中指定该路径即可:
@Bela Clark, thanks for confirming.
@Bela Clark,谢谢你的确认。
You may NOT have nodemon
exists from which nodemon
command, then you should have it in your package.json ie nodemon
be installed at :project_dir/node_modules/.bin/nodemon
您可能没有通过 `which nodemon` 命令找到 nodemon,那么您应该在您的 `package.json` 中安装它,也就是说,nodemon 应该位于:`项目目录/node_modules/.bin/nodemon`。
Then from Webstorm 's run/debug config, set Node parameters
to be
然后在 WebStorm 的运行/调试配置中,将 Node 参数设置为:
:path_to_project_dir/node_modules/.bin/nodemon
You should save the debug/run config to file so your teammates can also easily debug/run your nodejs app like you
您应该将调试/运行配置保存到文件中,以便您的团队成员也能像您一样轻松调试/运行您的 Node.js 应用。
This will save the config into some .xml file, sample as below
这将把配置保存到一个 .xml 文件中,示例如下:
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="index.js" type="NodeJSConfigurationType" path-to-node="$USER_HOME$/.nvm/versions/node/v19.4.0/bin/node" nameIsGenerated="true" node-parameters="../node_modules/.bin/nodemon" path-to-js-file="index.js" working-dir="$PROJECT_DIR$/nodejs27/node27_sequelize_apiapp/src">
<method v="2" />
</configuration>
</component>