简体   繁体   English

如何从 WebStorm 中运行 nodemon?

[英]How can I run nodemon from within WebStorm?

I would like to use nodemon from within the WebStorm IDE (version 7).我想在 WebStorm IDE(版本 7)中使用nodemon 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.当其中一个源文件发生更改时,Nodemon 监视我的源文件夹中的一个或多个文件并重新启动节点进程(在本例中为 Express 服务器)。

How do I configure WebStorm to use nodemon in a Run Configuration , so that the node process is automatically restarted?如何配置 WebStorm 在Run Configuration中使用nodemon ,以便节点进程自动重启?

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 interpreter: /usr/local/bin/node节点解释器: /usr/local/bin/node
  • Working directory: /Users/foo/test工作目录: /Users/foo/test
  • JavaScript file: server.js JavaScript 文件: server.js

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 (eg /usr/local/bin/nodemon ) under "Node parameters":您所要做的就是通过在控制台中运行which nodemon (例如/usr/local/bin/nodemon )在“节点参数”下获取其路径来指定nodemon的路径:

使用 nodemon 进行 Webstorm

@Bela Clark, thanks for confirming. @Bela Clark,感谢您的确认。

This is the Windows solution这是 Windows 解决方案

You can just use the nodemon.cmd instead of node directly like :您可以直接使用 nodemon.cmd 而不是 node ,例如:

Node interpreter : C:\MyPath\To\nodemon.cmd
Node parameters : /*Empty for me*/
Node WorkingDirectoy : C:\Users\MyUserName\Desktop\DirectoryContainingMyIndex.js
JavaScriptFile : app\index.js /*or just index.js depending on your config*/

and then :然后:

在此处输入图片说明

Hope it will help you.希望它会帮助你。

To install nodemon , use the following (if required, use sudo to run the installation with root privileges:要安装nodemon ,请使用以下命令(如果需要,请使用sudo以 root 权限运行安装:

npm install -g nodemon

This will install nodemon globally on your machine.这将在您的机器上全局安装nodemon

Then, in your WebStorm Run Configuration , add the following, leaving everything else unchanged:然后,在您的 WebStorm Run Configuration 中,添加以下内容,保持其他所有内容不变:

  • Node parameters: /usr/local/bin/nodemon --exec /usr/local/bin/node节点参数: /usr/local/bin/nodemon --exec /usr/local/bin/node

This will instruct the node interpreter to execute the nodemon script using the following command line: node /usr/local/bin/nodemon --exec /usr/local/bin/node server.js .这将指示节点解释器使用以下命令行执行nodemon脚本: node /usr/local/bin/nodemon --exec /usr/local/bin/node server.js

The --exec part is important, as the execution will fail with the following error: --exec部分很重要,因为执行将失败并显示以下错误:

/usr/local/bin/node /usr/local/bin/nodemon server.js
4 Oct 13:56:50 - [nodemon] v0.7.10
4 Oct 13:56:50 - [nodemon] to restart at any time, enter `rs`
4 Oct 13:56:50 - [nodemon] watching: /Users/foo/test
execvp(): No such file or directory
4 Oct 13:56:50 - [nodemon] starting `node server.js`
4 Oct 13:56:50 - [nodemon] exception in nodemon killing node
Error: spawn ENOENT
    at errnoException (child_process.js:980:11)
    at Process.ChildProcess._handle.onexit (child_process.js:771:34)

The error seems to be caused by WebStorm not seeing the node executable on its path.该错误似乎是由 WebStorm 没有在其路径上看到node可执行文件引起的。

The fix for this is to specify the location to the node executable using the --exec /usr/local/bin/node parameter.对此的修复是使用--exec /usr/local/bin/node参数指定node可执行文件的位置。

Using these settings, nodemon works fine when run from a WebStorm Run Configuration .使用这些设置, nodemon在从 WebStorm运行配置运行时可以正常工作。

The same trick might have to be used with some of the tools similar to nodemon , eg node-supervisor .同样的技巧可能必须与一些类似于nodemon的工具一起使用,例如node-supervisor

I'm on Windows and for me didn't worked with nodemon (no idea why), but someone from Jetbrains suggested to try with supervisor:我在 Windows 上,对我来说没有与 nodemon 一起工作(不知道为什么),但是来自 Jetbrains 的人建议与主管一起尝试:

  1. I installed supervisor: npm install supervisor -g我安装了主管: npm install supervisor -g

  2. Then find where is supervisor installed, for me was in: C:\\Users\\AlinC\\AppData\\Roaming\\npm\\node_modules\\supervisor\\lib\\cli-wrapper.js –no-restart-on error然后找到supervisor安装在哪里,对我来说是在: C:\\Users\\AlinC\\AppData\\Roaming\\npm\\node_modules\\supervisor\\lib\\cli-wrapper.js –no-restart-on error

  3. I went back to Intellij: edit configurations -> node parameters -> and added: C:\\Users\\AlinC\\AppData\\Roaming\\npm\\node_modules\\supervisor\\lib\\cli-wrapper.js –no-restart-on error我回到 Intellij:编辑配置 -> 节点参数 -> 并添加:C:\\Users\\AlinC\\AppData\\Roaming\\npm\\node_modules\\supervisor\\lib\\cli-wrapper.js –no-restart-on error

编辑配置

节点参数

For those interested for the solution in Windows 10, here is my configuration.对于那些对 Windows 10 中的解决方案感兴趣的人,这是我的配置。 It does not show "Terminate Batch" thing and works perfectly.它不显示“终止批处理”的东西并且完美运行。

在此处输入图片说明

You press debug ONCE and than you can save change files whatever and the server will restart in debug mode.您按下调试一次,然后您可以保存更改文件,服务器将在调试模式下重新启动。 All brakepoints are working perfectly所有刹车点都运行良好

For windows users set:对于 Windows 用户设置:

Node Interpreter: Path of the node.exe ie c:\\program files\\node\\node.exe节点解释器:node.exe 的路径,即 c:\\program files\\node\\node.exe

Node parameter: C:\\Users\\YOURUSER\\AppData\\Roaming\\npm\\node_modules\\nodemon\\bin\\nodemon.js节点参数:C:\\Users\\YOURUSER\\AppData\\Roaming\\npm\\node_modules\\nodemon\\bin\\nodemon.js

You can also make it work with nvm and debugging still works.您还可以使其与 nvm 一起使用,并且调试仍然有效。

Tested with Node.js 8.1.0 and Webstorm 2017.2使用 Node.js 8.1.0 和 Webstorm 2017.2 进行测试

First make sure you are on the right version (in my case v8.1.0) and install nodemon globally -首先确保您使用的是正确的版本(在我的情况下为 v8.1.0)并全局安装 nodemon -

nvm use v8.1.0
npm install -g nodemon

Then, open Run/Debug configurations and create a new one with the correct node interpreter.然后,打开运行/调试配置并使用正确的节点解释器创建一个新配置。

Node parameters should be:节点参数应该是:

MAC MAC

/Users/[YOUR_USER]/.nvm/versions/node/v8.1.0/bin/nodemon --inspect=3001

LINUX LINUX

/usr/local/nvm/versions/node/v8.1.0/bin/nodemon --inspect=3001

Save and debug respponsibally :)以响应方式保存和调试 :)

Node.js nodemon webstorm 调试

Here's the configuration that works for me on Windows 7 + WebStorm 8.0.4.这是在 Windows 7 + WebStorm 8.0.4 上适用于我的配置。 If I put nodemon.cmd as the node interpreter I kept getting "Terminate batch job (Y/N)?".如果我将 nodemon.cmd 作为节点解释器,我会不断收到“终止批处理作业(是/否)?”。

Windows 上的 Nodemon + Webstorm

In case you've installed nodemon like a global library, just set in node parameters:如果您像全局库一样安装了 nodemon,只需在节点参数中设置:

C:\\Users\\${yourUser}\\AppData\\Roaming\\npm\\node_modules\\nodemon\\bin\\nodemon.js C:\\Users\\${yourUser}\\AppData\\Roaming\\npm\\node_modules\\nodemon\\bin\\nodemon.js

webstorm中的nodemon

This is the only thing that worked for me:这是唯一对我有用的东西:

  1. Add a new package.json node run script command:添加一个新的 package.json 节点运行脚本命令:

在此处输入图片说明

  1. Create an NPM CONFIG (not a node config)创建NPM CONFIG (不是节点配置)
  2. Select "start-watch" as the command选择“start-watch”作为命令

在此处输入图片说明

For me this worked for debugging / breakpoints without issues or additional headache.对我来说,这适用于调试/断点,没有问题或额外的麻烦。

some of these answers appear to only work for Mac.其中一些答案似乎仅适用于 Mac。 For Windows, this configuration seems to work (my user name on Windows 7 is denman).对于 Windows,此配置似乎有效(我在 Windows 7 上的用户名是 denman)。

在此处输入图片说明

main.js is the starting point file for my Express application. main.js 是我的 Express 应用程序的起点文件。

Just add new script to package.json called nodemon (or choose your own name)只需向package.json添加名为 nodemon 的新脚本(或选择您自己的名称)

"scripts": {
  ...
  "nodemon": "nodemon ./bin/www"
}

Then go to Run/Debug Configuration and add npm configuration.然后转到运行/调试配置并添加npm配置。 Set设置

  • Command to " run "命令运行
  • Script to " nodemon " (name you chose in package.json) nodemon ”的脚本(您在 package.json 中选择的名称)

例子

  1. Do a npm install nodmemon -g执行 npm install nodmemon -g
  2. Only change the Path to Node to the nodemon.cmd, in my case (C:\\Users\\Rohit Taneja\\AppData\\Roaming\\npm\\nodemon.cmd), you'll also get this path after your installion of nodemon finishes.仅将节点的路径更改为 nodemon.cmd,在我的情况下 (C:\\Users\\Rohit Taneja\\AppData\\Roaming\\npm\\nodemon.cmd),在安装完 nodemon 后,您还将获得此路径。

  3. You're good to go你很高兴去

Per @bernhardw comment, as this was the answer for me -每@bernhardw 评论,因为这是我的答案 -

All is needed is /usr/local/bin/nodemon under node parameters Works for run and debug as it restarts upon changes, but debugging with breakpoint does not work.所有需要的是node parameters下的/usr/local/bin/nodemon当它在更改时重新启动时适用于运行和调试,使用断点调试不起作用。

Bonus: add -e for more extension eg /usr/local/bin/nodemon -e js,html,jade奖励:添加 -e 以获得更多扩展,例如/usr/local/bin/nodemon -e js,html,jade

(osx 10.10.5, rubymine 7.1.4) (操作系统 10.10.5,红宝石 7.1.4)

HTH HTH

npm install -g nodemon npm install -g nodemon

1* goto run->Edit Configurations->Press'+' at left corner and choose Node.js

2* Select Node.js and Press '+' 

3* Name as Nodemon, add path in javaScript file: C:\Users\Your_User_Name\AppData\Roaming\npm\node_modules\nodemon\bin\nodemon.js

4* Click Apply and Ok

5* Run the Nodemon

在此处输入图片说明 在此处输入图片说明

Script in package.json: "start": "nodemon --inspect -r babel-register src", package.json 中的脚本: "start": "nodemon --inspect -r babel-register src",

First pic: Run debug and it will start第一张图:运行调试,它将启动

Second pic: attaching to existing running node第二张图片:附加到现有的运行节点设置 在此处输入图片说明

Here is a fix for an error I was getting...这是对我遇到的错误的修复...

If you are using a Windows + NodeJS + nodemon.如果您使用的是 Windows + NodeJS + nodemon。 With an IntelliJ - Run Configuration.使用 IntelliJ - 运行配置。

ERROR: starting inspector on failed: address already in use

When I use nodemon version 1.19.1, I get the error.当我使用 nodemon 1.19.1 版时,出现错误。 When I use nodemon version 1.18.11, it works!当我使用 nodemon 1.18.11 版时,它可以工作!

Good luck...祝你好运...

I have a development in mac and as OdkoPP indicates I made it work我在 mac 上有一个开发项目,正如 OdkoPP 指出的那样,我让它工作了

"scripts": {
"build": "tsc",
"dev": "nodemon src/index.ts --exec ts-node"
},

Run/Debug Configurations npm:运行/调试配置 npm: 运行/调试配置 npm

This is how I am running这就是我跑步的方式

  1. Installed nodemon package安装 nodemon package

    npm install -g nodemon # OR using yarn: yarn global add nodemon npm install -g nodemon # 或者使用 yarn: yarn global add nodemon

  2. From Webstorm terminal, run从 Webstorm 终端,运行

    nodemon index.js nodemon index.js

This is how it will show running in terminal这就是它在终端中显示运行的方式

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM