The Wayback Machine - https://web.archive.org/web/20200615033052/https://github.com/visionmedia/debug/issues/641
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visual Studio Code express app #641

Open
shanekwheeler opened this issue Dec 3, 2018 · 11 comments
Open

Visual Studio Code express app #641

shanekwheeler opened this issue Dec 3, 2018 · 11 comments

Comments

@shanekwheeler
Copy link

@shanekwheeler shanekwheeler commented Dec 3, 2018

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;
@Qix-
Copy link
Member

@Qix- Qix- commented Dec 3, 2018

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

@Qix- Qix- added the question label Dec 3, 2018
@shanekwheeler
Copy link
Author

@shanekwheeler shanekwheeler commented Dec 3, 2018

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
@Qix-
Copy link
Member

@Qix- Qix- commented Dec 3, 2018

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 :)

@Qix- Qix- added the pr-welcome label Dec 3, 2018
@Qix- Qix- added this to the 5.x milestone Dec 18, 2018
@Qix-
Copy link
Member

@Qix- Qix- commented Dec 19, 2018

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

@michielbdejong
Copy link

@michielbdejong michielbdejong commented Apr 11, 2019

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"?

@dimitarnestorov
Copy link

@dimitarnestorov dimitarnestorov commented May 9, 2019

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.

@GioLogist
Copy link

@GioLogist GioLogist commented May 30, 2019

+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
@dimitarnestorov
Copy link

@dimitarnestorov dimitarnestorov commented Jun 1, 2019

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

@GioLogist
Copy link

@GioLogist GioLogist commented Jun 4, 2019

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

@dimitarnestorov
Copy link

@dimitarnestorov dimitarnestorov commented Jun 5, 2019

@zwneng
Copy link

@zwneng zwneng commented Jul 4, 2019

try this:

in lanuch.json
"console": "integratedTerminal"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.