当我不是node.exe的调用者时,如何在Windows的nodejs中调试javascript [英] how to debug javascript in nodejs on Windows when I'm not the caller of node.exe

查看:83
本文介绍了当我不是node.exe的调用者时,如何在Windows的nodejs中调试javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试Node.js脚本(在Windows上)。我发现可以使用 debugger;语句停止执行/在脚本中设置断点。但是,这似乎仅在node.exe调用时起作用:

  node debug myscript.js 

问题是我不是调用node.exe的人,因此我无法传递debug参数。实际上,在myscript.js之前,node.exe被多次调用。而myscript.js又从其他other.js中调用。直接运行myscript.js(在other.js之外)不起作用。



是否有一种方法等待调试器附加?



如果可能,该解决方案应使用某些GUI调试器(例如npm node-inspector)。



更新1:



要在应用程序中等待调试器使用:

  npm install sleep#在本地安装,而不是全局安装

将代码添加到myscript.js:

  var sleep = require('sleep'); 

...
var done = 1;
while(done!= 1){
console.log( PID = + process.pid);
sleep(1);
}
...

运行脚本(即启动任何触发其脚本的脚本)执行):

  PID = 3280 
PID = 3280

启动节点检查器:

  C:\用户\Samo>节点检查器
节点检查器v0.12.7
访问http://127.0.0.1:8080/?port=5858以开始调试。

打开一个新的cmd.exe并运行:

  C:\Users\Samo>任务列表/ FI IMAGENAME eq node.exe 

映像名称PID会话名称会话#内存使用
=============================================== ========================
node.exe 3944控制台1 64,016 K
node.exe 3280控制台1119,276 K

来自同一cmd.exe问题:

  node -e process._debugProcess(3280) 

在调试脚本的输出,您应该看到:

  PID = 3280 
启动调试器代理。
调试器在端口5858上监听

现在转到指定的URL(


  1. 默认情况下,该节点将在端口9229上进行监听

  2. 打开另一个 cmd git-bash 并运行此命令,

    其中 21392 是您的进程的PID 。



  node -e process._debugProcess(21392) 




  1. 您应该看到此

  2. 开始从VSCode 附加到9229


现在一切都应该准备就绪。


I'm trying to debug nodejs script (on Windows). I found that I can stop execution / set a breakpoint withing the script by using the 'debugger;' statement. However this seems to work only if node.exe called as:

node debug myscript.js

Problem is I'm not the one who's calling node.exe, so I can't pass the debug argument. Actually node.exe is being called many times before myscript.js. And myscript.js is in turn being called from some other.js. Running myscript.js directly (outside other.js) does not work.

Is there a way to wait for the debugger to attach?

If possible the solution should make use of some GUI debugger (like npm node-inspector).

Update 1:

To wait for the debugger in your application use:

npm install sleep           # install locally, not global

Add code to myscript.js:

var sleep = require('sleep'); 

...
   var done = 1;
   while ( done != 1 ) {
     console.log("PID=" + process.pid);
     sleep(1);
   }
...

Run your script (i.e. start whatever triggers its execution):

PID=3280
PID=3280

Start node-inspector:

C:\Users\Samo>node-inspector
Node Inspector v0.12.7
Visit http://127.0.0.1:8080/?port=5858 to start debugging.

Open a new cmd.exe and run:

C:\Users\Samo>tasklist /FI "IMAGENAME eq node.exe"

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
node.exe                      3944 Console                    1     64,016 K
node.exe                      3280 Console                    1    119,276 K

From the same cmd.exe issue:

node -e "process._debugProcess(3280)"

In the debugged script's output, you should see:

PID=3280
Starting debugger agent.
Debugger listening on port 5858

Now go to the indicated URL (http://127.0.0.1:8080/?port=5858) using Chrome (not Firefox, but Chrome). You should be able to debug, now.

I said should because in my case it does not work. If I send _debugProcess to 3280 (my myscript.js) nothing happens. However if I send _debugProcess to 3944 I get the "Starting debugger agent." message. Why?

解决方案

If you have the typical windows workspace such as Node, git and VSCode,
you can do it in these simple steps:

  1. In VSCode, open launch.json configuration or create new by clicking on the wheel
    (this is the debug view CtrlShiftD)

  1. The node will listen on port 9229 by default, so add this configuration:

{
  "type": "node",
  "request": "attach",
  "name": "Attach to 9229",
  "port": 9229
},

  1. Open Task Manager and locate the PID of your node process
    I could identify my by the "build" folder where the index.js is.
  2. open another cmd or git-bash and run this command,
    where 21392 is the PID of your process.

node -e "process._debugProcess(21392)"

  1. you should see this
  2. Start debugging from VSCode Attach to 9229

Everything should be ready now.

这篇关于当我不是node.exe的调用者时,如何在Windows的nodejs中调试javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆