10

My application is reading from stdin:

var input = process.stdin.read();

Is it possible to configure Visual Studio Code to redirect input on debug?

So it is be equal to this command line:

node app.js < input.txt

This configuration is not working, and debug is not starting.

{
  "name": "Launch",
  "type": "node",
  "program": "app.js",
  "stopOnEntry": false,
  "args": [
    "<",
    "input.txt"
  ]
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dizzy
  • 892
  • 3
  • 12
  • 24
  • 5
    Input redirection with '<' is only supported if the command and the arguments are interpreted by a shell. But the launch config from above uses the internal debug console of VS Code which doesn't use a 'shell'. You can add a 'console' attribute to the launch config with either a value of 'integratedTerminal' or 'externalTerminal'. In this case a shell is used and input or output redirection will work. – Andre Weinand Feb 21 '17 at 10:23
  • Setting `"console": "integratedTerminal"` while redirecting the standard input throws an error: `Error processing 'configurationDone' request. The handle is invalid.` **console** property should be set to something like `internalConsole`. – Persian Brat Dec 18 '21 at 23:06

2 Answers2

4

The args array is generally for Node.js startup and V8 engine runtime flags.

  --no-deprecation
  --throw-deprecation
  --trace-deprecation
  --v8-options
  --max-stack-size=val
  --icu-data-dir=dir

  --enable-ssl2
  --enable-ssl3

Type node --v8-options at the command line to see the full list of V8 runtime flags.

I'd recommend you start your application with the debug flag from the command line so you can direct it to take stdin and then attach the debugger to your running process.

> node --debug app.js
Debugger listening on port 5858

You can have multiple configurations in your launch.json file. Add or modify one to be your "Attach" debug configuration. For attaching, "address" and "port" must be specified (please note that "address" must be set to "localhost" since remote debugging is not yet supported). Port should be the one that the debug startup process returned above.

Enter image description here

Once your application is running on the port specified, you can change the debug target in the dropdown next to the play/run icon.

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ryan Joy
  • 3,021
  • 19
  • 20
1

This doesn't apply to node, but for anyone using GDB to debug a C/C++ application using the integrated console, the < doesn't get interpreted by a shell. So the workaround is to let GDB create the shell:

miDebuggerArgs: "'-ex' 'run < /path/to/input.txt'"

The Bic Pen
  • 773
  • 6
  • 21