键概念
NODE_ENV=development
>使用--inspect
或第三方模块(如温斯顿)实施战略记录,以捕获详细的,特定于上下文的日志以进行详细分析。
util.debuglog
>用于熟悉的调试环境,利用chrome devtools进行node.js应用程序(通过--inspect
理解调试调试术语
Term | Explanation |
---|---|
Breakpoint | A point in the code where the debugger pauses execution, allowing inspection of the program's state. |
Debugger | A tool providing debugging functionalities, such as stepping through code line by line and inspecting variables. |
Feature (not bug) | A common developer phrase used to jokingly dismiss a reported bug. |
Frequency | How often a bug occurs under specific conditions. |
"It doesn't work" | A vague and unhelpful bug report. |
Log Point | An instruction to the debugger to display a variable's value at a specific point during execution. |
Logging | Outputting runtime information to the console or a file. |
Logic Error | The program runs without crashing, but produces incorrect results. |
Priority | The ranking of a bug's importance in the list of planned updates. |
Race Condition | A hard-to-trace bug caused by the unpredictable sequence or timing of events. |
Refactoring | Rewriting code to improve readability and maintainability. |
Regression | The re-emergence of a previously fixed bug, often due to subsequent code changes. |
Related Bug | A bug similar to or connected to another bug. |
Reproduce | The steps needed to trigger the error. |
RTFM Error | User error disguised as a bug report (Read The Flipping Manual). |
Step Into | In a debugger, execute a function call line by line. |
Step Out | In a debugger, complete the current function's execution and return to the calling code. |
Step Over | In a debugger, execute a command without stepping into any functions it calls. |
Severity | The impact of a bug on the system (e.g., data loss is more severe than a minor UI issue). |
Stack Trace | A historical list of all functions called before an error occurred. |
Syntax Error | Errors caused by typos or incorrect code structure (e.g., console.lug() ). |
User Error | An error caused by user actions, but may still require a fix depending on the user's role. |
Watch | A variable monitored during debugger execution. |
Watchpoint | Similar to a breakpoint, but the program pauses only when a specific variable reaches a particular value. |
防止错误
积极的措施可以显着减少错误的发生。
使用强大的代码编辑器>
好的代码编辑器提供诸如线路编号,自动完成,语法突出显示,括号匹配,格式化,格式化等功能,以提高代码质量和减少错误。 流行的选择包括VS代码,原子和括号。
使用代码linter
Linters在测试之前识别潜在的代码问题(语法错误,缩进问题,未确定的变量)。 ESLINT,JSLINT和JSHINT是JavaScript和Node.js的流行选择。 它们可以从命令行()运行或集成到代码编辑中。
eslint myfile.js
>源控制系统(例如,git)跟踪代码更改,从而更容易识别何时何地引入错误。 诸如GitHub和Bitbucket之类的在线存储库提供了方便的工具和存储。
>问题跟踪系统有助于管理错误报告,跟踪重复项,文档复制步骤,分配优先级和监视进度。 许多在线存储库都包含基本问题跟踪,但是专门的解决方案对于大型项目都更好。
> 采用测试驱动的开发(TDD)
>TDD涉及在
之前编写测试代码,确保功能和尽早捕获问题。>
休息
>远离调试一段时间通常会导致新的见解和解决方案。node.js调试:环境变量
环境变量控制节点。JS应用程序设置。 在调试期间,通常设置为
>。 可以在Linux/MacOS(>
NODE_ENV
development
node.js调试:命令行选项NODE_ENV=development
set NODE_ENV=development
$env:NODE_ENV="development"
>命令行选项修改node.js运行时行为。 .env
>输出警告的堆栈跟踪(包括折旧)。 其他选项包括dotenv
,
。 控制台调试
console.log()
是一种基本但必不可少的调试工具。 但是,探索其他console
的方法:.dir()
,.table()
,.error()
,.count()
,.group()
,.time()
,.trace()
和.clear()
>。 ES6破坏性简化了记录复杂对象。
> node.js util.debuglog
util.debuglog
>有条件地将消息写给stderr,只有在适当设置NODE_DEBUG
>第三方记录模块(机舱,Loglevel,Morgan,Pino,Signale等)提供高级功能,例如记录级别,详细控制,文件输出等。
> node.js v8 Inspector
> V8 Inspector是一个强大的调试工具。 用启动应用程序。 命令包括
(继续),(next命令),node inspect ./index.js
(步入),cont
(逐步淘汰),next
,step
,out
pause
watch
node.js用chromesetBreakpoint()
.exit
调试
启动检查器,在端口9229上聆听。打开Chrome's,然后单击“检查”以附加DevTools。 设置断点,观察变量,然后检查呼叫堆栈。 对于远程调试,请使用。
node --inspect ./index.js
chrome://inspect
node --inspect=0.0.0.0:9229 ./index.js
>
vs代码提供集成的node.js调试。 通过单击排水沟或使用条件断点和日志点来设置断点。 对于远程调试或高级配置,请使用
>
launch.json
其他node.js调试工具
>探索其他IDE(Visual Studio,Jetbrains,Webstorm),扩展(Atom's
结论
Node.js提供了丰富的调试工具。 掌握这些工具可显着提高开发速度和应用可靠性。仍然有用,但利用更高级的选项进行有效的调试。node-debug
>
常见问题(FAQS)>
node inspect your-script.js
> node inspect-brk your-script.js
的区别在启动后附加; inspect
>如何设置断点?inspect-brk
使用语句,调试器的命令,或单击编辑器的沟渠(以IDES)。inspect
inspect-brk
的目的debugger;
>调试性能问题?
console.log()
以上是如何调试node.js应用程序:提示,技巧和工具的详细内容。更多信息请关注PHP中文网其他相关文章!