question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Visual Studio Code express app

See original GitHub issue

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;

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
DimitarNestorovcommented, May 10, 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.

3reactions
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 https://github.com/Microsoft/vscode/issues/19750#issuecomment-344037380 - 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 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Build Node.js Apps with Visual Studio Code
The Visual Studio Code editor has great support for writing and debugging Node.js applications. This tutorial takes you from Hello World to a...
Read more >
Tutorial: Create a Node.js and Express app in Visual Studio
In this tutorial, learn how to create a basic Node.js application by using the Express web application framework in Visual Studio.
Read more >
How to create and run Node.js project in VS code editor
Step 1: Create an empty folder and move it into that folder from your VS Code editor, use the following command. ; Step...
Read more >
A Practical Introduction to Setting Up a NodeJs+Express web ...
In your VSCode, Open the MyNodeJsProject folder . You can open the folder by clicking the file tab located at the upper...
Read more >
Node.js Applications with VS Code - vscode-docs
Express is a very popular application framework for building and running Node applications. You can scaffold a new Express application using the Express...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found