How to debug using npm run scripts from VSCode?

后端 未结 7 1049
小鲜肉
小鲜肉 2020-12-12 23:55

I was previously using gulp and running gulp to start my application and listeners from the Visual Studio Code debugger but have recently needed to switch to running scripts

相关标签:
7条回答
  • 2020-12-13 00:23

    My situation was a little different. I was testing an api server and also a client (for that api server). this is what worked for me.

    package.json (of the api server)

      "scripts": {
        "start": "DEBUG=users:* PORT=3333 SEQUELIZE_CONNECT=models/sequelize-sqlite.yaml node user-server",
        "debug": "DEBUG=users:* PORT=3333 SEQUELIZE_CONNECT=models/sequelize-sqlite.yaml node --inspect-brk user-server"
      },
    

    launch.json (of the api server)

    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Attach to Remote",
            "address": "localhost",
            "port": 9229,
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "${workspaceFolder}",
            "skipFiles": [
                "<node_internals>/**"
            ]
        },
    

    with that done, I would do the following.

    • npm run debug
    • you should immediately get something like this - Debugger listening on ws://127.0.0.1:9229/8df6503b-00e9-43da-ac53-c54a013ba53f
    • Attach to (in the debug menu) "Attach to Remote" (or whatever name you gave if you have multiple debug configurations) and then hit Run.
    • if successful, you will immediately see something like this - Debugger attached

    That would run the api server, and also have the debugger start debugging for me.

    Finally, to actually test the api client, I would run the api client file like this. note - all files are in the root folder. if your files are distributed elsewhere, you will have to modify the localRoot and remoteRoot accordingly.

    PORT=3333 node users-add-testthisman1.js
    

    Here, its important to note the client configuration I had.

     var client = restify.createJsonClient({
      url: 'http://localhost:'+process.env.PORT,
      version: '*'
    });
    

    If you have a different configuration, then, your command line will have different arguments, of course.

    0 讨论(0)
提交回复
热议问题