Rodrigo Brickman
Rodrigo Brickman

Reputation: 173

Can't debug expo project on vscode

I have an expo project, which we can run and build and it works correctly in android and iOS. What I want is to debug said project using my Visual Studio Code.

I followed some guides and tried the following:

  1. Adding React Native Tools extension in vscode.
  2. Adding the "Attach to packager" configuration in the vscode debugger.
  3. Changing the "react-native.packager.port" in settings.json to match the expo packager port (19001)
  4. Running expo (expo start)
  5. And tried to start the debugger with "Debug JS remotely" both enabled and disabled and also with the chrome debugger open or closed

The result I get is the small window with the debugger controls appears for a second and then dissapears, without any logs or evidence that it did something. I checked the terminal tab, the output tab and the debug console tab in vscode

By the way, when I enable "Debug JS remotely" the chrome debugger does launch and works perfectly.

My launch.json was autogenerated by the react native tools extension. I also tried adding "sourceMaps":true to the attach configuration and the end result was the same. Here is my code:

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "Debug Android",
        "cwd": "${workspaceFolder}",
        "type": "reactnative",
        "request": "launch",
        "platform": "android"
    },
    {
        "name": "Debug iOS",
        "cwd": "${workspaceFolder}",
        "type": "reactnative",
        "request": "launch",
        "platform": "ios"
    },
    {
        "name": "Attach to packager",
        "cwd": "${workspaceFolder}",
        "type": "reactnative",
        "request": "attach"
    },
    {
        "name": "Debug in Exponent",
        "cwd": "${workspaceFolder}",
        "type": "reactnative",
        "request": "launch",
        "platform": "exponent"
    }
]

}

Just in case you need it, the OS is Ubuntu 16.04

Thanks in advance!

Upvotes: 14

Views: 15161

Answers (4)

airtonix
airtonix

Reputation: 5154

In 2024, the answer is https://docs.expo.dev/debugging/tools/#debugging-with-vs-code

In short you need to install the Expo Tools extension and create a launch config like:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "expo",
            "request": "attach",
            "name": "Inspect Expo app",
            "projectRoot": "${workspaceFolder}",
            "bundlerHost": "127.0.0.1"
        }
    ]
}

Then:

yarn expo start --clear
# wait for help display
# press a
# wait for app to appear on emulator

Now in VScode, you can run the debug task Inspect Expo app

I accept claps from adoring fans (also money).

Upvotes: 1

Mahmoud Farahat
Mahmoud Farahat

Reputation: 5475

Thanks Loupi & Bharat Lalwani your answers really helped me, and I want to post a more updated and detailed answer.

  1. Install React Native Tools

  2. Add Attach to packager config to .vscode/launch.json (Create the file if not exist)

    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to packager",
            "cwd": "${workspaceFolder}",
            "type": "reactnative",
            "request": "attach",
            "port": "19000", //Port number may be different in your machine
            "sourceMaps": true
        }
    ]
    
  3. Edit vscode settings file to add "react-native-packger.port": 19000 //same port in the previous step

vscode settings files locations:

  • Windows %APPDATA%\Code\User\settings.json
  • macOS $HOME/Library/Application Support/Code/User/settings.json
  • Linux $HOME/.config/Code/User/settings.json
  1. run expo start and find the correct port in the terminal (in my case it's 19000 update the port in steps 2&3 if yours is different, step the app and re-run expo start ) enter image description here

  2. open the debug menu and click attach to packager enter image description here

  3. go back to terminal and press a to start the app in android emulator (make sure that the emulator is already running from AVD manager), if the emulator stuck on a white screen go to terminal press r to reload the app

  4. if no breakpoints where hit, make sure that Debug remote JS is enabled in your emulator, while the app is running in the emulator press CTRL+M and select Debug remote JS enter image description here

Note: to start a new debugging session, first make sure to stop expo server using CTRL+C in the terminal and disconnect the debugger in vs code as in the following screenshot, you may also need to close the running app in emulator firstenter image description here enter image description here

Remember to close debugger-ui tab in the browser before attaching the debugger in vscode

Upvotes: 23

Bharat Lalwani
Bharat Lalwani

Reputation: 1520

I have done all changes as Loupi mentioned. But for me worked for Port no. 19000. I have to set both settings.json & launch.json port as "port" : "19000".

Here is the code snippet for the below images:-

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Direct iOS - Experimental",
            "request": "launch",
            "type": "reactnativedirect",
            "cwd": "${workspaceFolder}",
            "platform": "ios"
        },
        {
            "name": "Debug iOS",
            "cwd": "${workspaceFolder}",
            "type": "reactnative",
            "request": "launch",
            "platform": "ios"
        },
        {
            "name": "Attach to packager",
            "cwd": "${workspaceFolder}",
            "type": "reactnative",
            "request": "attach",
            "port" : "19000",
            "sourceMaps": true
        }
    ]
}

enter image description here

settings.json

Upvotes: 2

Loupi
Loupi

Reputation: 701

Here is a .vscode/launch.json file with a single Attach to packager config.
Notice that the port property is set to 19001.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to packager",
            "cwd": "${workspaceFolder}",
            "type": "reactnative",
            "request": "attach",
            "port": "19001",
            "sourceMaps": true
        }
    ]
}

To debug your app, first start the expo packager, using the vscode console: npm run start

Then start the "Attach to packager" debugger profile. In the Debug Console window, you should see that the debugger is now attached to the packager.

Finally go back to the console and launch your app on the desired target. i.e: 'a' for android.

Instead of seeing a new react-native debug tab opening in your browser, you should now see that the debugger is connected in vscode.

Upvotes: 26

Related Questions