You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
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
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.
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"?
@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 {namespaceNodeJS{interfaceProcess{browser: boolean}interfaceGlobal{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.
Reacted by Michiel de Jong, Bryan Chen and Simon Smith
Activity
Qix- commentedon Dec 3, 2018
Above that line, could you please add
console.log('debug', process.env.DEBUG);and re-run and show the output here?shanekwheeler commentedon Dec 3, 2018
It outputs:
debug *Qix- commentedon 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
consolewhen 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", butdebugusesprocess.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- commentedon Dec 19, 2018
Blocked by #649 - please let that issue be resolved first before tackling this issue!
michielbdejong commentedon 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 runningDEBUG=* npm startfrom 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 commentedon May 9, 2019
Or you can keep
"outputCapture": "console"and add the following (before requiring debug) to trigger the browser logger: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-7a3c1d483cf3which 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:183on 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:
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.logcall after theprocess.stdout.writecall, the only issue would be coloring.GioLogist commentedon 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
With this solution, I don't receive anything
launch.json
test.js
DimitarNestorov commentedon 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 commentedon Jun 4, 2019
@DimitarNestorov just tried re-ordering now w/no luck
DimitarNestorov commentedon Jun 5, 2019
@GioLogist Check this example out: https://github.com/dimitarnestorov/vscode-debug-with-colors
zwneng commentedon Jul 4, 2019
try this:
in lanuch.json
"console": "integratedTerminal"michaeljohnbennett commentedon Feb 1, 2022
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.