Run configurations - Cannot connect to runtime process - node.js

I'm atempting to run the node command npm run dev from the debugger in vscode.
My run config in launch.json:
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"dev"
],
"cwd": "${workspaceRoot}"
}]
My scripts in package.json:
"scripts": {
"dev": "npm run build:live",
"build:live": "nodemon --exec ./node_modules/.bin/ts-node -- ./app/*.ts"
}
But when i run the config i get this output:
npm --debug-brk=18538 run dev
> discordbot#1.0.0 dev /home/olian04/Documents/Projects/Node/JavaScript/DiscordBot.js
> npm run build:live
> discordbot#1.0.0 build:live /home/olian04/Documents/Projects/Node/JavaScript/DiscordBot.js
> nodemon --exec ./node_modules/.bin/ts-node -- ./app/*.ts
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `./node_modules/.bin/ts-node ./app/index.ts`
And then this error:
Cannot connect to runtime process (timeout after 10000 ms).
To me it looks as if it worked, but it still throws an error, and the code stops running when it does, why?

I ended up adding the "port" tag to the "configurations".
And the --debug-brk argument to the "build:live" node command.
Final configs:
"configurations": [{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"dev"
],
"port": 5858,
"cwd": "${workspaceRoot}"
}]
Final script:
"scripts": {
"dev": "npm run build:live",
"build:live": "nodemon --debug-brk=5858 --exec ./node_modules/.bin/ts-node -- ./app/*.ts"
}

Related

while debugging in vscode changes in nestjs file does not take effect

I have a nestjs project in vscode and in debugging mode the changes in code does not take effect. Breakpoints hit normally, however, but for example a changed variable values are not to be seen - i.e. they stay as the previous ones (the ones before change).
My launch.json is the following:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"runtimeExecutable": "nodemon",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register", "--config", "nodemon-debug.json"],
"program": "${workspaceFolder}/src/main.ts",
"autoAttachChildProcesses": true,
"restart": true
}
]
}
And the nodemon-debug.json file has the following:
{
"watch": ["src"],
"ext": "ts, js",
"ignore": ["src/**/*.spec.ts"]
}
And strangely enough, when I make change in the code, the nodemon really registers it and restart the app, as can seen from the console:
C:\...\AppData\Roaming\npm\nodemon.cmd --nolazy -r ts-node/register --config nodemon-debug.json .\dist\main.js
[nodemon] 2.0.18
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[nodemon] to restart at any time, enter `rs`
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[nodemon] watching path(s): src\**\*
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[nodemon] watching extensions: ts,js
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[nodemon] starting `node --nolazy -r ts-node/register .\dist\main.js`
c:\...\AppData\Roaming\npm\node_modules\nodemon\lib\utils\log.js:34
[WEB] http://localhost:3000
Can anyone figure out why changes are not taking effect?
I finally found solution from this https://stackoverflow.com/a/63325135/7625979
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Nest Framework",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"start:debug",
"--",
"--inspect-brk"
],
"autoAttachChildProcesses": true,
"restart": true,
"sourceMaps": true,
"stopOnEntry": false,
"console": "integratedTerminal",
}
]
}
This solution indeed works for me perfectly and does not need either the vscode's setting: Debug > Javascript: Auto Attach Filter to "Always". Also console: integratedTerminal -option is handy because debug-info comes directly into the same console. Also, nodemon-debug.json is not now needed.
Script command in Package.json file are now:
"scripts": {
...
"start:debug": "nest start --debug --watch",
...
}
It seems that "--watch" is still needed here for the code changes to take effect.
I don't have a launch.json in my NestJS project in VSCode.
However, I did change the debug settings in VSCode to the following:
file -> preferences -> settings -> Debug > Javascript: Auto Attach Filter to "Always"
Then when I set a breakpoint my test stops on the breakpoint. If I make a change and save the file my test will restart automatically and stop on the breakpoint with the updated values.

How to get VSCode to attach to nodemon started by NPM?

Can I have VSCode kick off my app using NPM run scripts and then attach to the resulting process to debug?
My project kicks off with nodemon via NPM script (to execute babel, etc). However, while the app kicks off, VSCode's debugger doesn't attach (breakpoints are skipped).
// from package.json
"scripts": { "debug": "nodemon --inspect --exec babel-node src/app.js" }
// from launch.json
{
"type": "node",
"request": "launch",
"name": "Debug",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"debug"
],
"address": "localhost",
"port": 9229,
"protocol": "auto",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"autoAttachChildProcesses": true
}
It seems the debugger starts but breakpoints I placed next to a few test calls are ignored. Here's the terminal output:
[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node --inspect src/app.js`
Debugger listening on ws://127.0.0.1:9229/13ef6ca8-40da-4741-854a-467e4230b2a7
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Hey!
Waiting for the debugger to disconnect...
[nodemon] clean exit - waiting for changes before restart```
Looks like this was a fool's errand because it technically can't be done.

How to debug Serverless Offline in Visual Studio Code using another port?

I have two Serverless Offline "servers" which I need to run locally at same time.
So I need to change the port of one of the servers.
I run a server using Visual Studio Code debugger. The configs of the servers are in launch.json files.
How can I change the port of a Serverless Offline application so that I can run it in parallel with another Serverless Offline application using VS Code debugger?
If you are using windows, update the vscode launch.json and package.json as below :
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Serverless",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"debug"
],
"outFiles": [
"${workspaceFolder}/handler.js"
],
"port": 9229,
"sourceMaps": true
}
]
}
// package.json
....
"scripts": {
"debug": "SET SLS_DEBUG=* && node --inspect %USERPROFILE%\\AppData\\Roaming\\npm\\node_modules\\serverless\\bin\\serverless offline -s dev"
}
If on linux your debug script will be:
// package.json
....
"scripts": {
"debug": "export SLS_DEBUG=* && node --inspect /usr/local/bin/serverless offline -s dev"
}
Solved by adding the following lines to the serverless.yml file:
custom:
serverless-offline: ## add this two lines
port: 4000 ## bellow "custom:" line
I'm using a Linux system, here is what my launch.json and package.json files look like.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Debug Serverless",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"debug"
],
"sourceMaps": true,
"attachSimplePort": 9229
}
]
}
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"debug": "node --inspect node_modules/serverless/bin/serverless offline -s dev"
},

Debug node modules in VS Code on webpack-dev-server npm script process

I'm trying to debug a node module which is throwing an error when I run npm start or npm run-script debug.
These are the scripts I have in package.json
"scripts": {
"start": "webpack-dev-server --port 9229 --env.mode development",
"debug": "webpack-dev-server --port 9229 --env.mode development",
"test": "echo \"Error: no test specified\" && exit 1"
},
and in VS Code's launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch via NPM",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script", "debug"
],
"port": 9229
}
]
}
when I start debugging it does not break on any of the breakpoints I set in webpack.config.js or in the node_modules but it outputs the errors I'm getting in the VS Code's debug console.

vscode debug ES6 application

I have VSCode 0.5.0. I set the compilerOptions flag to "ES6" and the editor started recognizing my ES6 code as correct.
I have babel installed.
My Mocha tests use the babel compilers and my tests pass.
My app runs from the command line with no problems when I launch it with babel-node .
When I debug the app from within VSCode, it starts up without the ES6 support, and the app fails for ES6 syntax issues.
Are there debug settings that I missed turning on?
Here's how to get VSCode debugger to work with Babel 6+:
First install dependencies locally:
$ npm install babel-cli --save
$ npm install babel-preset-es2015 --save
Then run babel-node:
$ node_modules/babel-cli/bin/babel-node.js --inspect --presets es2015 -- server.js --inspect
By default, the debugger will listen on port 5858, so make sure the port matches in launch.json for Attach configuration:
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}
Now attach the debugger in VSCode:
make sure debug configuration is set to Attach and not Launch
run with F5
Nodemon
Although not required, if you also want to use nodemon to pickup code changes without restarting the server, you can do this:
Make sure nodemon is installed:
$ npm install nodemon --save-dev
Run the server
$ node_modules/.bin/nodemon node_modules/babel-cli/bin/babel-node.js --inspect --presets es2015 -- server.js --inspect
Finally, attach the debugger as shown above.
Assuming you have babel-cli installed as a local module in your project the following should work.
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/babel-cli/bin/babel-node.js",
"stopOnEntry": false,
"args": [
"${workspaceRoot}/server.js"
],
...
You can try babel-register (you'll also need other companion babel modules as req'd):
npm install --save-dev babel-register
with a launch.json configuration along these lines:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/src/index.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy",
"--require",
"babel-register"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": true,
"outFiles": [
]
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outFiles": [],
"localRoot": "${workspaceRoot}",
"remoteRoot": null
},
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"processId": "${command.PickProcess}",
"port": 5858,
"sourceMaps": false,
"outFiles": []
}
]
}
This is loosely based on vscode-debug-nodejs-es6 with the addition of the babel-register runtime argument.
By default VSCode launches node just with a --debug-brk option. This is not enough to enable ES6 support. If you can find out what options 'babel-node' passes to node, you could specify the same options in the VSCode launch config (through the runtimeArgs attribute). But this does not solve the issue that babel-node transpiles your ES6 code before running it.
Alternatively you could try to set the 'runtimeExecutable' in your launch config to 'babel-node'. This approach works with other node wrappers, but I haven't verified that is works with babel-node.
A third option (which should work) is to use the attach mode of VSCode: for this launch babel-node from the command line with the '--debug' option. It should print a port number. Then create an 'attach' launch config in VSCode with that port.
babel + nodemon
In the VS Code Terminal, launch your server with the --inspect argument:
nodemon --inspect --watch src --exec node_modules/.bin/babel-node --presets react,es2015 src/server.js
Among the other lines, one will show the port on which the debugger is listening:
...
Debugger listening on port 9229
...
Create the following debug configuration:
{
"type": "node",
"request": "attach",
"name": "Attach to Port",
"address": "localhost",
"port": 9229
}
Launch the debugger, and if everything went fine you will see in the output Terminal:
Debugger attached.
From now on, you can debug your application.
There are two ways of doing it:
First Option using npm command prompt
In package.json file create build command that will execute babel
{
"scripts": {
"build": "babel src --out-dir dist --watch --source-maps"
},
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-preset-es2015-node6": "^0.4.0",
"eslint": "^3.16.0"
}
}
In launch.json Enter following code:
{
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/src/index.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"runtimeArgs": [
"--nolazy"
],
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/dist/**/*.js"
]
}
]
}
Open your cmd window, navigate to your package.json file and execute:
npm run build
Open your VS Code and run your code. It will run and it will stop at all your breakpoints. The reason it works because source maps are generated and VS knows how to map them to your code.
Second option using VS Code task:
In VS Code add following task (Ctrl + Shift + P) and type 'Tasks: Configure Task Runner':
Add following code to tasks.json file
{
"version": "0.1.0",
"command": "${workspaceRoot}/node_modules/.bin/babel",
"isShellCommand": true,
"tasks": [
{
"taskName": "watch",
"args": [
"src",
"--out-dir",
"dist",
"--watch",
"--source-maps"
],
"suppressTaskName": true,
"isBuildCommand": true
}
]
}
Now execute task, but pressing Ctrl + Shift + B (build command) and now you can run and debug your code. VS Code doing the same as what npm is doing in step one.
You will also need to configure babel in .babelrc (located in the root of the project) file like this:
{
"presets": [
"es2015-node6"
]
}
and jsconfig.json (located in the root of the project)
{
"compilerOptions": {
"target": "ES6"
},
"include": [
"src/**/*"
]
}
This is my configuration and it works great! I am using the VSCode debugging, mocha 6.1.4, node: v8.16.0, and Babel version 6.
Make sure to load babel-register and babel-polyfill in runtimeArgs, or else you will get regeneratorRuntime is not defined!
{
"type": "node",
"request": "launch",
"name": "Mocha test debug",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector",
"stopOnEntry": false,
"runtimeArgs": [
"--nolazy",
"--require",
"babel-register",
"--require",
"babel-polyfill",
"tests/*.js"
],
"sourceMaps": true
}
babel-node & vs code attach
config a npm script in package.json:
"scripts": {
"debug": "babel-node --debug-brk demo.js --presets es2015,stage-2"
}
add vs code debug configuration:
{
"name": "Attach",
"type": "node",
"protocol": "legacy",
"request": "attach",
"port": 5858
}
When transpiling with bael-node, you should add "--inspect-brk" in the script, so that the script may break when a breakpoint is hit.
Ex:
"start": "babel-node --inspect-brk app.js --presets es2015,stage-2"
Now when you run using npm run start, debugger will be launched and you can see following in your console:
Debugger listening on ws://127.0.0.1:9229/cf72a33c-ab38-4798-be5e-8b8df072f724
For help see https://nodejs.org/en/docs/inspector
That shows debugging process has started and we can attach to it on port# 9229.
Now, you need to add the following debugger config for vs-code to attach to this process: (in launch.json)
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"port": 9229
}
]
}
After saving, click the "start debugging" button, to attach to process initiated by node earlier. You may read more about this here

Resources