Nodejs node-debug 三法三例之node debugger + node inspector

Nodejs node-debug 三法三例之node debugger + node inspector

大家对nodejs调试应该都比较头疼,至少我这个不用IDE写js的人很头疼这个,其实node的生态圈非常好 有非常好的工具和非常潮的开发方式 这里总结了3法3例,希望能对大家有所帮助

文档地址 http://i5ting.github.io/node-debug-tutorial

Screen Shot 2014-11-17 at 1.34.28 PM.png

3种方法

3个例子

  • hello world
  • 继承例子
  • express helloworld

希望能对大家有所帮助

欢迎关注我的公众号【node全栈】 node全栈.png


31 回复

Nodejs node-debug 三法三例之node debugger + node inspector

大家对Node.js调试可能都会感到头疼,至少我这个不使用IDE来编写JavaScript的人确实对调试感到困扰。不过,Node.js生态系统中有很多优秀的工具和现代化的开发方式。这里我总结了三种调试方法和三个实例,希望对大家有所帮助。

文档地址

Node.js 调试教程

方法一:使用 node debugger

node debugger 是一个内置的调试工具,可以让你在命令行中进行简单的断点调试。

示例代码:

// hello.js
const http = require('http');

console.log('Starting server...');
http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(3000);

console.log('Server running at http://localhost:3000/');

启动调试:

在命令行中运行以下命令:

$ node debug hello.js

这将启动调试模式。你可以在代码中的任意位置设置断点,并通过 cont(继续执行)、next(单步执行)、pause(暂停)等命令进行调试。

方法二:使用 node inspector

node inspector 是一个基于 Chrome DevTools 的调试工具,可以提供更强大的调试功能,包括查看变量、调用堆栈等。

安装:

首先需要全局安装 node-inspector

$ npm install -g node-inspector

启动调试:

在另一个终端窗口中启动 node inspector

$ node-inspector

然后在另一个终端窗口中启动你的Node.js应用,并指定使用 --inspect 参数:

$ node --inspect hello.js

这将打开一个新的浏览器窗口,你可以在这个窗口中使用Chrome DevTools进行调试。

示例代码:继承例子

为了演示继承,我们可以创建一个简单的父类和子类:

// person.js
class Person {
    constructor(name) {
        this.name = name;
    }

    sayHello() {
        console.log(`Hello, my name is ${this.name}`);
    }
}

module.exports = Person;

// student.js
const Person = require('./person');

class Student extends Person {
    constructor(name, grade) {
        super(name);
        this.grade = grade;
    }

    study() {
        console.log(`${this.name} is studying in grade ${this.grade}`);
    }
}

module.exports = Student;

// index.js
const Student = require('./student');

const student = new Student('Alice', '10th');
student.sayHello();
student.study();

你可以使用上述两种方法中的任何一种来调试这个程序。

总结

以上介绍了两种常用的Node.js调试方法:node debuggernode inspector。它们各有特点,可以根据实际需求选择合适的方法。希望这些方法和示例代码能帮助你在Node.js开发中更加得心应手。


很有用,感谢!

除了文档还有视频,赞!

很不错,赞

一直在用node webkit调试node.js

不知道lz的文章右边的树状列表是怎么实现的,比较感兴趣……

调试不是用WebStorm吗!?

看你怎么用 IDE,把它当成依赖,还是当成解决每天 repeat yourself 的便利。

好奇害死喵,原来是夫妻店啊,哈哈

话说我一直都是console.log的。。。。。。。。。。。囧

收藏下 自豪地采用 CNodeJS ionic

image.jpg

iPad双击放大后

欢迎关注我的公众号【node全栈】 node全栈.png

视频看不了。

直接用webstorm最好

mark,本来一直用webstorm

这方面习惯不太好,mark

我一直不习惯用断点调试啊,感觉设置个断点让它在某一步停下来,观察情况,还不如直接在那里写个 console.log 把想要看的东西输出来。

TDD好啊TDD好。我比较喜欢用TDD+console。

如何在vscode debug nodejs的时候使用nodemon?

“scripts”: { “dev”: "nodemon -w src --exec “babel-node src --presets es2015,stage-0"” },

然后运行npm run dev,浏览器就可以访问项目了

launch.json 配置如下

{ “name”: “附加”, “type”: “node”, “request”: “attach”, “port”: 8080, “address”: “localhost”, “restart”: false, “sourceMaps”: false, “outDir”: null, “localRoot”: “${workspaceRoot}”, “remoteRoot”: null },

然后点击vscode上的运行按钮,运行起来了,但是这个运行按钮的框在不停的跳动,浏览器访问也走不到断点,运行launch 按钮的时候是可以debug的。这是为啥呢? 官网上说的也不够明确,看了一遍文章nodemon_vscode,也没办法运行起来!

Screen Shot 2016-12-26 at 4.53.33 PM.png

做了一点修改,新增2种模式,更全面一些

地址不变 http://i5ting.github.io/node-debug-tutorial/

node inspector 和 node debugger 已经无法在 node 7.0 下正常编译了,https://github.com/node-inspector/v8-profiler/issues/98 如果你使用新版 electron 也会无法编译。感觉已经无人维护了。 最新版 node 支持 --inspect 选项,可配合 Chrome Canary 直接进行调试。

webstorm飘过 From Noder

刚刚要来看,发现既然已经不支持调试了,看来只能继续 console.log 了

对于 “Nodejs node-debug 三法三例之node debugger + node inspector” 这个主题,我们可以分别介绍如何使用 node debuggernode inspector 来进行调试。

使用 node debugger

node debugger 是一个内置的命令行调试器。它允许开发者通过命令行逐步执行代码、设置断点等。

示例代码

假设我们有一个简单的 hello.js 文件:

// hello.js
console.log('Starting...');

function greet(name) {
    console.log(`Hello, ${name}!`);
}

greet('World');

console.log('Done.');

调试步骤

  1. 在命令行中运行调试器:

    $ node debug hello.js
    
  2. 设置断点(例如在第4行):

    > setBreakpoint(4)
    
  3. 继续执行到断点:

    > cont
    
  4. 使用 next 步进执行:

    > next
    
  5. 使用 repl 进入交互模式查看变量值:

    > repl
    

使用 node inspector

node inspector 提供了一个基于浏览器的界面来调试 Node.js 应用程序。它更直观且功能强大。

示例代码

假设我们仍然使用 hello.js 文件:

// hello.js
console.log('Starting...');

function greet(name) {
    console.log(`Hello, ${name}!`);
}

greet('World');

console.log('Done.');

调试步骤

  1. 在命令行中启动 inspector 模式:

    $ node --inspect-brk hello.js
    
  2. 打开浏览器并访问 chrome://inspect,然后点击 “Open dedicated DevTools for Node”。

  3. 在 DevTools 中设置断点,并进行调试。

以上就是 node debuggernode inspector 的基本使用方法。希望这对您有所帮助!

回到顶部