<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: cloudi africa</title>
    <description>The latest articles on DEV Community by cloudi africa (@cloudi_africa).</description>
    <link>https://dev.to/cloudi_africa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2020236%2Fdf03d7d6-ec2c-4a14-aab0-9751dc8d9e8d.png</url>
      <title>DEV Community: cloudi africa</title>
      <link>https://dev.to/cloudi_africa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cloudi_africa"/>
    <language>en</language>
    <item>
      <title>Setting Up VSCode Debugger for NodeJS + TypeScript Projects</title>
      <dc:creator>cloudi africa</dc:creator>
      <pubDate>Fri, 06 Sep 2024 12:14:17 +0000</pubDate>
      <link>https://dev.to/cloudi_africa/setting-up-vscode-debugger-for-nodejs-typescript-projects-4g00</link>
      <guid>https://dev.to/cloudi_africa/setting-up-vscode-debugger-for-nodejs-typescript-projects-4g00</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;As developers, we know that a smooth debugging process can save hours of frustration and boost productivity. In this blog post, we’ll walk through the process of setting up the VSCode debugger for a NodeJS project using TypeScript. We’ll cover everything from project initialization to creating a launch.json file for debugging and a tasks.json file to automate the build process.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What We’ll Cover&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating a new project&lt;/li&gt;
&lt;li&gt;Setting up TypeScript&lt;/li&gt;
&lt;li&gt;Configuring the debugger&lt;/li&gt;
&lt;li&gt;Automating the build process&lt;/li&gt;
&lt;li&gt;Troubleshooting common issues&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Creating a New Project
&lt;/h2&gt;

&lt;p&gt;First, we need to set up a new project. Don’t worry if you’re not familiar with all the terms — we’ll explain as we go along.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open VSCode and create a new folder for your project.&lt;/li&gt;
&lt;li&gt;Open the terminal in VSCode (View &amp;gt; Terminal or Ctrl+` ).&lt;/li&gt;
&lt;li&gt;In the terminal, type the following command and press Enter:
&lt;code&gt;npm init -y&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This creates a package.json file, which is like a recipe book for your project. It lists all the ingredients (dependencies) your project needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Setting Up TypeScript
&lt;/h2&gt;

&lt;p&gt;Now, let’s add TypeScript to our project. TypeScript is a language that adds extra features to JavaScript, making it easier to write and maintain large projects.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the terminal, type:
&lt;code&gt;npm install typescript - save-dev&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This installs TypeScript as a development tool for your project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, let’s create a TypeScript configuration file. In the terminal, type:
&lt;code&gt;npx tsc - init&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This creates a tsconfig.json file, which tells TypeScript how to behave in your project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open the tsconfig.json file and replace its contents with:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;{&lt;br&gt;
  "compilerOptions": {&lt;br&gt;
    "target": "es2016",&lt;br&gt;
    "module": "commonjs",&lt;br&gt;
    "sourceMap": true,&lt;br&gt;
    "outDir": "./build",&lt;br&gt;
    "esModuleInterop": true,&lt;br&gt;
    "forceConsistentCasingInFileNames": true,&lt;br&gt;
    "strict": true,&lt;br&gt;
    "skipLibCheck": true&lt;br&gt;
  }&lt;br&gt;
}&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Don’t worry about understanding every option right now. The important ones are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“sourceMap”: true: This creates map files that help with debugging.&lt;/li&gt;
&lt;li&gt;“outDir”: “./build”: This tells TypeScript where to put the compiled JavaScript files.&lt;/li&gt;
&lt;li&gt;Create a new folder called src in your project. This is where we’ll put our TypeScript files.&lt;/li&gt;
&lt;li&gt;In the src folder, create a file called index.ts and add this simple code:
&lt;code&gt;console.log("Hello, TypeScript!");&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Configuring the Debugger
&lt;/h2&gt;

&lt;p&gt;Now, let’s set up the debugger in VSCode:&lt;/p&gt;

&lt;p&gt;Click on the “Run and Debug” icon in the left sidebar (it looks like a play button with a bug).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on “create a launch.json file” and select “Node.js”.&lt;/li&gt;
&lt;li&gt;Replace the contents of the launch.json file with:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
{&lt;br&gt;
    "version": "0.2.0",&lt;br&gt;
    "configurations": [&lt;br&gt;
        {&lt;br&gt;
            "type": "node",&lt;br&gt;
            "request": "launch",&lt;br&gt;
            "name": "Debug TypeScript",&lt;br&gt;
            "skipFiles": [&lt;br&gt;
                "&amp;lt;node_internals&amp;gt;/**"&lt;br&gt;
            ],&lt;br&gt;
            "program": "${workspaceFolder}/build/index.js",&lt;br&gt;
            "preLaunchTask": "tsc: build - tsconfig.json",&lt;br&gt;
            "outFiles": ["${workspaceFolder}/build/**/*.js"]&lt;br&gt;
        }&lt;br&gt;
    ]&lt;br&gt;
}&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This tells VSCode how to run and debug our TypeScript project.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Automating the Build Process
&lt;/h2&gt;

&lt;p&gt;To make our lives easier, we’ll set up VSCode to automatically compile our TypeScript files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Press Ctrl + Shift + P (or Cmd + Shift + P on Mac) to open the command palette.&lt;/li&gt;
&lt;li&gt;Type “Tasks: Configure Task” and select it.&lt;/li&gt;
&lt;li&gt;Choose “tsc: build — tsconfig.json”.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This creates a tasks.json file that tells VSCode how to compile our TypeScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Debugging Your Code
&lt;/h2&gt;

&lt;p&gt;Now you’re ready to debug!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In your index.ts file, add a breakpoint by clicking to the left of a line number. A red dot should appear.&lt;/li&gt;
&lt;li&gt;Click the “Run and Debug” icon in the left sidebar.&lt;/li&gt;
&lt;li&gt;Click the green play button next to “Debug TypeScript”.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;VSCode will compile your TypeScript and pause at the breakpoint you set. You can now step through your code, inspect variables, and find bugs more easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  Troubleshooting
&lt;/h2&gt;

&lt;p&gt;If you’re using Windows and encounter issues with the build task not completing:&lt;/p&gt;

&lt;p&gt;Open the command palette (Ctrl + Shift + P).&lt;br&gt;
Search for “Terminal: Select Default Profile” and choose PowerShell.&lt;br&gt;
This can resolve some common issues with task execution on Windows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You’ve set up a NodeJS + TypeScript project with debugging in VSCode. This might seem like a lot at first, but with practice, it’ll become second nature. Remember, debugging is a powerful tool that can save you hours of frustration when trying to find and fix bugs in your code.&lt;/p&gt;

&lt;p&gt;Keep experimenting, and don’t be afraid to use the debugger often. It’s there to help you understand what’s happening in your code.&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>typescript</category>
      <category>node</category>
      <category>debug</category>
    </item>
  </channel>
</rss>
