鍵概念
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中文網其他相關文章!