´øÄãÔÚVSCodeÖе÷ÊÔNestjsÏîÄ¿£¨½Ì³Ì£©
ÓÃVscodeд¹ýNodeÕâÀàÏîÄ¿µÄСͬ°éÃǶ¼ÖªµÀ, ÈôÊÇÎÒÃÇÏëÒªÅÅÅÌÎÊÌâµÄʱ¼ä´ó²¿·Ö¶¼ÊÇͨ¹ýconsole.log¾ÙÐдòÓ¡À´¿´ÎÊÌâÔÚÄÄ, ÈôÊÇÉæ¼°µ½µÄÎÊÌâ½ÏÁ¿ÖØ´óµÄʱ¼ä»áÑ¡Ôñͨ¹ýVscodeÖÐdebugÀ´µ÷ÊÔʵÑé½â¾ö, ¿ÉÈôÊÇÊÇNestÏîÄ¿µÄ»°, ³ýÁËÉèÖÃlaunch.jsonµÄ»°, ÔÚµ÷ÊÔµÄʱ¼ä¿ÉÄÜ»áÌáÐÑ ÕÒ²»µ½Ê¹Ãü¡°tsc: build – tsconfig.json¡±¡£
package.json
Ê×ÏÈÔÚpackage.jsonµÄscript¾ç±¾ÖÐÉèÖÃÏÂÁîÀ´ÓÅ»¯, ÕâÑù²»ÐèҪÿ´Î¶¼ÔÚÖÕ¶ËÇÃÄÇô³¤µÄÏÂÁî¡£¡¾ÍƼöѧϰ£ºvscode/” target=”_blank” textvalue=”vscode½Ì³Ì”>vscode½Ì³Ì¡¢±à³Ì½Ìѧ¡¿
{ "scripts": { "start":"nest start", "dev:debug": "pnpm run start:debug", "start:debug": "nest start --debug --watch", }}¸´ÖÆ´úÂë
µÇ¼ºó¸´ÖÆ
ÎÒÃÇͨ¹ýÔËÐÐpnpm run dev:debug ¾Í¿ÉÒÔÔÚÔËÐÐÔÚʹÓÃnestÏÂÁîÔËÐÐÏîÄ¿µÄʱ¼äÌí¼Ó`¡ªdebug“²ÎÊýÀ´¾ÙÐе÷ÊÔ, ¿ÉÊÇÕâ¸öʱ¼äÎÒÃÇÖ»ÊÇ°ÑÏîÄ¿ÔËÐÐÆðÀ´ÁË, ÔÚVSCodeÖдò¶ÏµãµÄʱ¼ä²¢²»»á¶Ïס, DebugÃæ°åÖÐҲûÓÐÔËÐÐ
Attach µ÷ÊÔ
·¿ªÏÂÁîÃæ°å£¨command+shift+p)£¬ËÑË÷Toggle Auto Attach£¬Ñ¡Öкó»Ø³µÆôÓÃ
Ñ¡Ôñ×ÜÊÇ: ÕâÑùÖ»ÒªÔÚÖÕ¶Ëͨ¹ýnodejsÔËÐÐʹÃüµÄʱ¼ä¶¼»áÆô¶¯Ò»¸öwsµÄµ÷ÊԶ˿Ú
Õâ¸öʱ¼äͨ¹ý, Öն˻᷺ÆðÏÂÃæÕâÐÐ,½Ó×Å»á¼ûhttp://localhost:9229/json, ¾Í¿ÉÒÔ¿´µ½VSCodeÔËÐеÄÿ¸öµ÷ÊÔÓõÄwb(wbsocket), VScodeµ÷ÊÔʵÖÊÉϾÍÊÇͨ¹ýË«¶ËͨѶµÄ·½·¨¾ÙÐе÷ÊÔ
Debugger listening on ws://127.0.0.1:9229/8e908307-94a7-4513-a525-82953b2c02c7For help, see: https://nodejs.org/en/docs/inspector¸´ÖÆ´úÂë
µÇ¼ºó¸´ÖÆ
launch.json
ÏÖÔÚÎÒÃÇ¿ÉÒÔͨ¹ý±¾ÉèÖÃlunch.jsonµÄµ÷ÊÔ·½·¨ÎªattachÀ´¾ÙÐе÷ÊÔ¸Õ¸ÕVScodeÌṩµÄµ÷ÊÔÀú³Ì
{ "version": "0.2.0", "configurations": [ { "type": "node", // µ÷ÊÔ·½·¨¸ÄΪ¸½¼Ó "request": "attach", "name": "Attach Nest", "skipFiles": [ "<node_internals>/**" ], // Æô¶¯µ÷ÊÔÆ÷֮ǰÔËÐÐʹÃü¾ÙÐбàÒëTSÏîÄ¿ "preLaunchTask": "npm: dev:debug", "outFiles": [ "${workspaceFolder}/dist/**/*.js" ] } ] }¸´ÖÆ´úÂë</node_internals>
µÇ¼ºó¸´ÖÆ
×¢ÖØpreLaunchTask, ÉÏÃæÎÒÃÇ˵µ½ÔÚÆô¶¯µ÷ÊÔµÄʱ¼ä»áÌáÐÑ»áÌáÐÑ **ÕÒ²»µ½Ê¹Ãü¡°xxx tsconfig.json¡±¡£**ÎÒÃÇÐèÒªÔÚµ÷ÊÔÏÈͨ¹ýtsc ±àÒë
tasks.json
ÔÚVS CodeÖÐͨ¹ý tasks ÉèÖÃÎļþ£¬¿ÉÒÔ½ç˵һ×éʹÃüÒÔ±ãÔÚ±à¼Æ÷ÖÐÖ´ÐС£
{ "version": "2.0.0", "tasks": [ { "type": "typescript", "tsconfig": "tsconfig.json", "problemMatcher": [ "$tsc" ], "group": "build", "label": "tsc: build", "options": { "emit": "true", "pretty": "true", } }, { "type": "npm", "script": "dev:debug", "problemMatcher": [], "label": "npm: dev:debug", "detail": "pnpm run start:debug", "dependsOn": [ "tsc: build" ] } ] }¸´ÖÆ´úÂë
µÇ¼ºó¸´ÖÆ
ÏÂÃæÚ¹ÊÍÏÂÉÏÃæÉèÖõÄÁ½¸öʹÃü
tsc: build: TypeScript ±àÒëʹÃü£¬ËüʹÓà tsconfig.json ÎļþÖеÄÉèÖÃÀ´±àÒë TypeScript ´úÂ롣ʹÃüµÄÀàÐÍΪ typescript£¬ÒÔÊÇËü»áʹÓà tsc ÏÂÁîÀ´Ö´ÐбàÒë¡£ÔÚ±àÒëʱ´ú£¬ÈôÊÇÓÐÈκιýʧ£¬Ôò»áʹÓà $tsc ÎÊÌâÆ¥ÅäÆ÷À´Ê¶±ð¹ýʧÐÅÏ¢¡£
npm: dev:debug: ÔËÐÐ NestJS Ó¦ÓóÌÐòµÄµ÷ÊÔʹÃü¡£ËüµÄÀàÐÍÊÇ npm£¬¿ÉÒÔʹÓà NPM¡¢Yarn »ò PNPM À´ÔËÐо籾¡£¸ÃʹÃüÖ´ÐÐ dev:debug ¾ç±¾£¬¸Ã¾ç±¾ÓÉ NestJS Ó¦ÓóÌÐò¿ª·¢Ö°Ô±½ç˵£¬²¢ÔÚ package.json ÎļþÖÐÖ¸¶¨¡£¸ÃʹÃüµÄÒÀÀµ¹ØϵÊÇ tsc: build£¬ÕâÒâζ×ÅÔÚÔËÐÐ npm: dev:debug ʹÃü֮ǰ£¬ÐèÒªÏÈÖ´ÐÐ tsc: build ʹÃüÀ´±àÒë TypeScript ´úÂë¡£
ÖÁ´ËÎÒÃǾͿÉÒÔͨ¹ýÔÚVScodeÃæ°åµã»÷µ÷ÊÔ°´Å¥, ÔËÐÐnpm: dev:debug ʹÃüʹÓñàÒëºóµÄ JavaScript ´úÂëÀ´Æô¶¯ NestJS Ó¦ÓóÌÐòµÄµ÷ÊÔ°æ±¾¡£Í¨¹ýÕâÖÖ·½·¨£¬¿ÉÒÔÔÚ VS Code ÖÐÇáËɵØÖ´Ðй¹½¨ºÍµ÷ÊÔ²Ù×÷£¬²¢Ê¹ÓÃÒ»¸ö¼òÆÓµÄÏÂÁîÀ´Æô¶¯Õû¸öÀú³Ì¡£
¸ü¶à¹ØÓÚVSCodeµÄÏà¹Ø֪ʶ£¬Çë»á¼û£ºvscode»ù´¡½Ì³Ì£¡
ÒÔÉϾÍÊÇ´øÄãÔÚVSCodeÖе÷ÊÔNestjsÏîÄ¿£¨½Ì³Ì£©µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡