13

I'm trying to debug my strapi project (3.0.0 beta 16.6) in VS Code. My launch.json:

{
  "type": "node",
  "request": "attach",
  "name": "Attach to strapi",
  "port": 9229
} 

My package.json:

"scripts": {
    "debug": "node --inspect=127.0.0.1:9229 ./node_modules/strapi/bin/strapi.js develop"
}

Debugger attaches to the process, but all my breakpoints become unverified (appear black, not red). What's wrong with my configs?

8 Answers 8

11

Add VS configuration "NODE: launch via npm" in .vscode/launch.json "configurations" property and run

    {
      "type": "node",
      "request": "launch",
      "name": "Launch via NPM",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run-script",
        "develop"
      ],
      "port": 9229,
      "skipFiles": [
        "<node_internals>/**"
      ],
      "console": "integratedTerminal"
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Confirmed with Strapi 14.1.6, but I missing console output so I added "console": "integratedTerminal"
10

Just to add to @alxnkt comment. I have encountered the same and solved it by changing the launch.json to port 9230

{
  "type": "node",
  "request": "attach",
  "name": "Attach to strapi",
  "port": 9230
}

while keeping the port on package.json as 9229

"debug": "node --inspect=127.0.0.1:9229 ./node_modules/strapi/bin/strapi.js develop"

There are somehow 2 processes running when calling the Strapi develop command (possibly the admin panel and its core server), the process we have to monitor becomes port 9230 instead.

1 Comment

In my case the port is incrementing by 1 everytime I change some file in Strapi project because then Strapi restarts the server and the second process, I believe. This makes it very annoying to debug it...
10

This worked for me using Strapi v4

{
    "version": "0.2.0",
    "configurations": [
     {
      "name": "STRAPI Debug",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceRoot}",
      "runtimeExecutable": "node",
      "runtimeVersion":"14.19.0",
      "runtimeArgs": ["--lazy"],
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceRoot}/node_modules/@strapi/strapi/bin/strapi.js",
      "args": [
        "develop"
      ],
      "protocol": "inspector",
      "env": {
        "NODE_ENV": "development"
      },
      "autoAttachChildProcesses": true,
      "console": "integratedTerminal"
     },
    ]
}

Comments

10

For strapi v4 you will have to add "sourceMap": true to compilerOptions of tsconfig.json file.

1 Comment

This should be accepted. Shame strapi starter doesnt add this property ! thank you
9

this answer come from the following strapi/strapi issue:

I have come up with next solution: having next script in server.js file (my custom one):

const strapi = require('strapi');
strapi({ dir: process.cwd(), autoReload: true }).start();

I'm using nodemon by next command: nodemon --inspect=0.0.0.0:9228 server.js

Now I can attach to 9228 by debugger.

2 Comments

Thank you! I got even more than I expected. Bonus: autoreload.
This doesn't work anymore in my dockerized v4 environment. I'm getting Cannot find module 'strapi'... Do you know how to fix it @Jim LAURIE?
3

You can also use NODE_OPTIONS:

NODE_OPTIONS='--inspect' yarn strapi develop

You'll get:

$ NODE_OPTIONS="--inspect" yarn strapi develop
Debugger listening on ws://127.0.0.1:9229/8564942a-6476-443a-9f64-87fa6d0055b7
For help, see: https://nodejs.org/en/docs/inspector
yarn run v1.22.5
$ strapi develop
Starting inspector on 127.0.0.1:9229 failed: address already in use
Debugger listening on ws://127.0.0.1:9230/7da840dd-cb89-493f-8ce0-e11530efdfbb
For help, see: https://nodejs.org/en/docs/inspector

Now you can use Debug: Attach to Node Process to attach to port 9230.

1 Comment

Confirmed with Strapi 4.1.6
2

I just launched it via NPM, here is my launch.json (in .vscode folder)

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch via NPM",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run-script",
        "develop"
      ],
      "port": 9229,
      "skipFiles": [
        "<node_internals>/**"
      ],
      "console": "integratedTerminal"
    }
  ]
}

1 Comment

Confirmed with Strapi 14.1.6, but I missing console output so I added "console": "integratedTerminal"
1

The problem has been solved by setting port number to 9203:

{
  "type": "node",
  "request": "attach",
  "name": "Attach to strapi",
  "port": 9229
} 

But I have no idea about HOW it works...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.