VS Code + Node Launch + Run Time Args - node.js

Currently attempting to setup VS Code to run a node app, however, unable to get it to work correctly. I can run it however manually without an issue. With the goal once it's running to be able to attach to the node process for breakpoint debugging:
Manual operation that works:
navigate to the directory: /Users/me/source/app_modules/main-api/
run nvm use 12 to be using v12.22.1
run node -r esm dev-server.js
Runs correctly, and loads the esm package module so imports work correctly.
When running with launch.json:
{
"version": "0.2.0",
"configurations": [
{
"envFile": "${workspaceFolder}/app_modules/main-api/.env.local",
"type": "node",
"request": "launch",
"name": "Launch API",
"cwd": "${workspaceFolder}/app_modules/main-api/",
"runtimeVersion": "12",
"runtimeArgs": ["-r esm"],
"program": "${workspaceFolder}/app_modules/main-api/dev-server.js",
}
]
}
Running with this results in: /Users/me/.nvm/versions/node/v12.22.12/bin/node: bad option: -r esm
It's not finding the node_modules even though I request it to use the main-api directory as the main working directory.

Related

trying to debug electron app, and window not showing up

I am attempting to debug an electron app in vs code (the main process, not the renderer process). The project I am trying to debug is this repo:
https://github.com/hello-efficiency-inc/raven-reader
it works really nice, once you have it cloned, and the right things are installed. Then I can just run:
yarn electron:serve
And boom, it runs nicely.
But I would like to also debug this. So I look around a bit, and find that I should probably use this launch.json file in the .vscode directory:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Electron: Main",
"protocol": "inspector",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"runtimeArgs": ["--remote-debugging-port=9223", "."],
"windows": {
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
}
}
]
}
In my package.json, I have this line for finding the startfile:
"main": "./src/background.js",
So, I try to run by clicking the debug icon, and then green triangle where it says
run and debug | Electron:Main
Which gives me this in the call stack menu:
But nothing actually seems like it's running, I dont see any desktop window which I would like so I can actually debug, and I can't hit my breakpoints either
Best way to sort this is to install code runner extension in vscode

Trying to run "npm run start" in VS Code's Debug on Windows 10

Normally, I run npm run start to run my program. I am trying to use the VS Code debugger to debug my program while running it.
Here's my launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch electron",
"type": "cppvsdbg",
"request": "launch",
"program": "npm",
"args": ["run", "start"],
//"preLaunchTask": "build:win32",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true
}
]
}
When I run this using VS Code the error I get is: launch program '' does not exist.
This all works fine if I use node under program for my debug sessions.
Other things:
$ which npm
/c/Program Files/nodejs/npm
I tried changing program to exactly that path but it didn't work.
I think the reason is in your "program" field, which is supposed to locate the executable files. Some common values are like "program": "${workspaceFolder}/app.js"
program - executable or file to run when launching the debugger

Visual studio code Debugger not getting attached with sample express project generated by express generator

Node js Version :v8.11.3
VS Code version :1.29.1 x64
npm version :6.6.0
Hi ,
I have created a default express skeleton project using express --view=jade myapp command after that cd myapp/ and npm install in the last.
Now I am trying to attach vscode debugger to this newly created project but no luck.
This is the content of my launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"protocol":"inspector",
"port":3000,
"program": "/home/pankaj/myfirstapp/myapp/app.js"
}
]
}
I am also not able to see any error in the debug console .There is nothing in debug console neither success nor failure.
Thanks In advance
Be careful for express default setup to set the path of program in launch.json to "program": "${workspaceFolder}/bin/www"
Port 3000 is the application port of your express application. If you specify this in your launch configuration, the VS Code debugger will try to use it as the debug port (which obviously cannot work).
In addition, use VS Code variables to specify where your programs lives. E.g. if you have opened VS Code on your myapp directory, use ${workspaceFolder}/app.js for the program attribute:
E.g.:
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js"
}

Node Jest- Config paths must be specified

I am running Jest test cases into a reactjs project. I am trying to debug my jest test cases in VS code. The tests run ok on command line. But when I launch debugger in VS code I see error.
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest Tests",
"program": "${workspaceRoot}/xxx/xxx/node_modules/jest/bin/jest",
"args": [
"-i"
],
"internalConsoleOptions": "openOnSessionStart",
"outFiles": [
"${workspaceRoot}/xxx/xxx/**/*"
]
}
]
}
Error
Debugger listening on ws://127.0.0.1:31182/2bf113f1-002f-49ed-ad91-5510affd172a
Debugger attached.
Error: Could not find a config file based on provided values:
path: "/Users/xxx/xxx/xxx-ui"
cwd: "/Users/xxx/xxx/xxx-ui"
Configh paths must be specified by either a direct path to a config
file, or a path to a directory. If directory is given, Jest will try to
traverse directory tree up, until it finds either "jest.config.js" or
"package.json".
Just specify -c option in your launch config:
{
"name": "Debug Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"{$PathToNPMRoot}/node_modules/jest/bin/jest.js",
"--runInBand",
"-c", "{$PathToConfig}/jest.config.js",
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
},
You will have to replace {$PathToNPMRoot} and {$PathToConfig}
Source
I'll tell you what did the trick for me.
As documentation says, in case that your code is not in the root of your project folder you should create a new config file to specify where jest is located in z .vscode folder, for example:
.vscode/settings.json
{
"jest.pathToJest": "functions/node_modules/.bin/jest"
}
(You should replace "functions" to your own folder name, in case you have your code on your project's root you won't need to do the previous step)
After doing that I was still getting the same error as you even though I configured correctly the path to jest and my jest configuration was specified correctly on my package.json, then I moved my jest configuration to jest.config.js file on the root of my project and that was all, jest was now up and running after I used the command Jest: Start Runner.
//jest.config.js on root dir
module.exports = { verbose: true };
I hope you can get it working
cheers!
I never created a Jest config file. Instead, I just set the working directory in the VSCode launch.json file:
How to configure vs code working directory in the launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit:
// https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
// https://jestjs.io/docs/troubleshooting
{
"name": "Debug Jest Tests for Modules",
// https://stackoverflow.com/questions/47540627/
// how-to-configure-vs-code-working-directory-in-the-launch-json
"cwd": "${workspaceRoot}/pathToMyJestModuleTestDir",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
// https://stackoverflow.com/questions/60372790/
// node-v13-jest-es6-native-support-for-modules-without-babel-or-esm
"--experimental-vm-modules",
"${workspaceRoot}/pathToMyJestModuleTestDir/node_modules/jest/bin/jest.js",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
Values are for illustrative purposes only. They do not reflect what I use on my development machines.
The main thing to note here is the following line:
"cwd": "${workspaceRoot}/pathToMyJestModuleTestDir",
That line is explained in the SO Thread mentioned at the top of this answer.
I am new to Jest, as so I had this problem several times! and came back here several times, no solution! nothing worked. No matter how stupid it sounds, the problem was lacking the package.json, you cannot run without it. Maybe it is somewhere on the documentation, I am not very patient to keep reading documentation! Thus, test the obvious: if you created your package.json!!!
Just type in the following and you will be good to go.
npm init

Can I run/debug Heroku Node.js app locally with VSCode?

TL; DR
On Microsoft VSCode v1.6.1, how to:
Properly set runtime executable?
Properly pass Heroku arguments?
Run Heroku Node.js app?
Debug Heroku Node.js app?
Details
I have created a Heroku Node.js application, which is launched using the CLI command:
heroku local web
and successfully starts at port 5000.
I am trying to debug it using Microsoft Visual Studio Code, using the following launch.json configuration:
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/app.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": "/usr/local/bin/heroku",
"runtimeArgs": [
"local web",
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": false,
"outFiles": []
}
But VSCode is automagically passing --debug-brk argument to heroku, causing the error:
/usr/local/bin/heroku --debug-brk=23080 'local web' app.js
! `--debug-brk=23080` is not a heroku command.
! See `heroku help` for a list of available commands.
VSCode also does not find heroku command without its full path (seems like it is not loading PATH environment variable).
Any ideas about how to setup the editor?
The following solution works for me:
1) In your procfile add the parameter --debug to the node process
web: node --debug server.js
By default the debugger listens in the port 5858
2) Once you have the node process running, open VSCode and add the following configuration to your launch.json file
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 5858
}
3) Finally, click the play button in VSCode with the option "Attach to Process" and it should debug your process.
The following solution worked for me.
In my package.json "scripts", I added:
"debug": "node --inspect-brk server.js"
Then, in launch.json I added an envFile entry to the default "Launch via NPM" configuration, which now looks looks like this:
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script",
"debug"
],
"port": 9229,
"envFile": "${workspaceFolder}/.env"
}
The above solution enables the VSCode debugger to run my server via an npm script, and my server runs with the env vars set in my .gitignore'd .env file, just like in the "regular" Heroku node.js workflow.
I struggled with this as for some reason the solution propsed didn't work for me. However, an alternate solution did so I thought I would share.
From the default debugging options in VS Code choose
Attach by Process ID
When you start the debugger with this configuration it should list available processes to attach to and one should be simply be server.js. This requires manually attaching each time, and if the other automatic attachment works for you that may be better, but this is still a workable solution.

Resources