Skip to content

Visual Studio Code express app #641

Open

Description

@shanekwheeler

I am trying to run an express app through Visual Studio Code. I have a launch.json file with DEBUG defined like so:

        "env": {
             "DEBUG": "*"
        }

Here is a trimmed down version of my app.js file where you can see the bolded debug line that doesn't output to the debug console (Test 2). Test 1 before it outputs as expected. If I run this from the command line passing DEBUG=* npm start both lines show as expected.

var debug = require('debug')('app');
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();
const v1 = require('./routes/v1');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

**debug("Test 1 that outputs!");**

app.use(function(req, res, next) {
    console.error("THIS OUTPUTS");
    **debug("Test 2 that doesn't output!!");**
    console.log(`${req.method} request for '${req.url}' - ${JSON.stringify(req.body)}`);
    next();
});

app.use(cors());

app.listen(3000);

console.log("Service running on port 3000");

module.exports = app;

Activity

  1. Qix- commented on Dec 3, 2018

    @Qix-
    Member

    Above that line, could you please add console.log('debug', process.env.DEBUG); and re-run and show the output here?

  2. added
    questionThis issue asks a question or requests usage support
    on Dec 3, 2018
  3. shanekwheeler commented on Dec 3, 2018

    @shanekwheeler
    Author

    It outputs: debug *

    Mon, 03 Dec 2018 16:17:23 GMT app Test 1 that outputs!
    Mon, 03 Dec 2018 16:17:23 GMT express:router use '/' <anonymous>
    Mon, 03 Dec 2018 16:17:23 GMT express:router:layer new '/'
    Mon, 03 Dec 2018 16:17:23 GMT express:router use '/' corsMiddleware
    Mon, 03 Dec 2018 16:17:23 GMT express:router:layer new '/'
    Service running on port 3000       app.js:49
    THIS OUTPUTS                              app.js:15
    debug *                                           app.js:16
    POST request for '/accessory-api/v1/accessory/32bdaa89-e791-42a3-a472-8f5f732be1ea' - {"reachable":false}     app.js:18
    
  4. added
    needs-documentationThis issue or change requires additional documentation
    help-wantedThis issue has an actionable item
    and removed
    questionThis issue asks a question or requests usage support
    on Dec 3, 2018
  5. Qix- commented on Dec 3, 2018

    @Qix-
    Member

    Thanks for the report and the detailed information. It's refreshing to be able to reproduce something so quickly 😄

    You're absolutely right. For some bizarre reason, VSCode patches console when they should really be consuming stdout/stderr streams - or both, I suppose.

    See microsoft/vscode#19750 (comment) - it doesn't appear to be something they want to fix concretely.

    Add this to your launch.json:

    {
        "outputCapture": "std"
    }

    (by default, it's "console", but debug uses process.stderr.write() - appropriately, I might add).

    Thanks again for reporting.


    I would happily accept a PR for adding this piece of information into the README :)

  6. added
    pr-welcomeThis issue has an approved change; a pull request would be appreciated
    on Dec 3, 2018
  7. added this to the 5.x milestone on Dec 18, 2018
  8. Qix- commented on Dec 19, 2018

    @Qix-
    Member

    Blocked by #649 - please let that issue be resolved first before tackling this issue!

  9. michielbdejong commented on Apr 11, 2019

    @michielbdejong

    Related to microsoft/vscode#41600 (comment).

    The solution of adding "outputCapture": "std" in VSCode is not ideal, because the output is in a single colour, which makes it less readable than when running DEBUG=* npm start from a bash terminal window.

    Interestingly, they mention "Getting output through the debug socket" as an alternative (seemingly) to getting output from stdout? How does one log a debug statement "through the debug socket"?

  10. DimitarNestorov commented on May 9, 2019

    @DimitarNestorov

    Or you can keep "outputCapture": "console" and add the following (before requiring debug) to trigger the browser logger:

    process.browser = true
    global.window = { process: { type: 'renderer' } }

    image

    Would love a toggle switch like debug.forceConsoleLogging = true. Or if anyone wants to automate it I would suggest this implementation from Node's Cluster module:
    https://github.com/nodejs/node/blob/422e8f762873aef4a37185f3237c0d666c929d8e/lib/internal/cluster/master.js#L103-L109


    @michielbdejong the debug socket is the ws://127.0.0.1:45341/f8704798-75c0-4c1c-9086-7a3c1d483cf3 which you can see in the beginning of my debug console above. VS Code and Node communicate through this socket using Chrome Debugging Protocol to transfer console.logs, console.errors... (no stdout/stderr), and also stack trace info (browser.js:183 on the right), breakpoint control and so on. I believe that by attaching to it you won't be able to solve the issue, because you would be a client and the server (Node) is what emits the console, not sure.


    In case anyone is using TypeScript, here's the type declarations:

    declare global {
    	namespace NodeJS {
    		interface Process {
    			browser: boolean
    		}
    
    		interface Global {
    			window: object
    		}
    	}
    }

    Just found out that there is an inspector module built in to node which allows you to transfer information to the debug console. So I guess it could actually be easily implemented by adding inspector.console.log call after the process.stdout.write call, the only issue would be coloring.

  11. GioLogist commented on May 30, 2019

    @GioLogist

    +1 For this issues, happy to see this discussion happening : )

    @DimitarNestorov Not sure how that solution for coloring worked for you.

    With the following solution, I receive debug info (w/out color):

    launch.json

    "outputCapture": "std",
    

    With this solution, I don't receive anything

    launch.json

    "outputCapture": "console", // or even if i leave as std
    

    test.js

    process.browser = true
    global.window = { process: { type: 'renderer' } }
    // blah blah debug
    
  12. DimitarNestorov commented on Jun 1, 2019

    @DimitarNestorov

    @GioLogist Where is your require/import? It must be after the two lines. I'll setup an example project if you want?

  13. GioLogist commented on Jun 4, 2019

    @GioLogist

    @DimitarNestorov just tried re-ordering now w/no luck

  14. DimitarNestorov commented on Jun 5, 2019

    @DimitarNestorov
  15. zwneng commented on Jul 4, 2019

    @zwneng

    try this:

    in lanuch.json
    "console": "integratedTerminal"

  16. michaeljohnbennett commented on Feb 1, 2022

    @michaeljohnbennett

    could we get this added into the docs? 3 years later I only stumbled on this after searching for ages for a solution to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help-wantedThis issue has an actionable itemneeds-documentationThis issue or change requires additional documentationpr-welcomeThis issue has an approved change; a pull request would be appreciated

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions