VS Code-debugger, How to use node with command line option in launch.json for starting a script - node.js

The script that is meant must be executed with the command
node --no-experimental-fetch server.js
otherwise a error occurs.
To use the VS Code-debugger and not to start the script always with the shell,
I want to integrate the command in the launch.json.
I generated and edited the following 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": "Start server",
"skipFiles": [
"<node_internals>/**"
],
"args": [
"--no-experimental-fetch"
],
"program": "${workspaceFolder}/server.js"
}
]
}
but the error occurs.
Has someone an idea what should be edit in the launch.json that fixes the issue?
Thanks for reading and answering this question, I appreciate it.

Instead of "args" you should use "runtimeArgs" with arguments for node(.exe).
You might as well use environment variable definition for "NODE_OPTIONS":
...
env: {
"NODE_OPTIONS": "--no-experimental-fetch"
},
...

Related

Node Debugging in WSL

I am using Windows 10 WSL in VSCode.
I have the extensions remote development, WSL, I have node installed, nvm as well.
When in work in a remote WSL and try to debug I don't have the Node option at all.
When I run the file I get:
No debugger available, can not send 'variables'
My launch.json is as follows:
// 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",
"skipFiles": [
"<node_internals>/**"
],
"program": "${file}"
}
]
}```
Can someone please help me fix it by adding Node.js option so I can properly debug?
Thanks a lot in advance!
Did you found the solution eventually?
I add similar issue, I could get things working only by specifying the node binary path explicitly.
get your node binary path by issuing:
which node
then add it to your launch.json config:
{
"name": "Launch Program",
"program": "${workspaceFolder}/helloworld.js",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "pwa-node",
"runtimeExecutable": "/home/user01/.nvm/versions/node/v14.18.0/bin/node"
},
Still there is something is wrong going on, even though I simply the setup tutorial:
https://learn.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-wsl

VSCode Nodejs debugger does not save changes?

I am just starting to learn using the Nodejs debugger. It has been really helpful already but I spent a a lot of time changing a js file I was debugging but I did not know that the changes I made were not being implement right away.
Is this an expected behavior? Can I set the debugger up so that it restarts on each save and notices the new changes?
Edit 1 :
Here is my debuf config:
{
// 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",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\app.js"
}
]
}
Normally, after every save, you have to restart your node program to see the changes. I'm sure there are other programs but one I use exclusively (I never use node alone) is nodemon. It's globally installed npm package program. Here is sample config for vs code debugger:
{
"type": "node",
"request": "launch",
"name": "Launch app - nodemon",
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--inspect=9250"
],
"program": "${workspaceRoot}/api/app",
"cwd": "${workspaceRoot}/api",
"autoAttachChildProcesses": true,
"restart": true
},
nodemon will monitor all the files and restart node every time it detects changes.
Add runtimeArgs to change debugger port.

Can i skip node internal modules in nodejs debugging while using visual studio code? [duplicate]

When debugging in vscode I'd like to make some "blackboxing" and do not enter into code I didn't write. How can I do this?
In your launch or attach debug task you can enter a
"skipfiles"
option which is
"An array of file or folder names, or path globs, to skip when debugging."
For example, from skipping node internals during debugging
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js",
"${workspaceFolder}/yourLibToSkip/**/*.js"
]
Also, there is a "magic reference" to the built-in core node modules you can use:
"skipFiles": [
"<node_internals>/**/*.js"
]
I was confused on where to put the setting, so just in case, if you want to skip both node_modules deps and node_internals files, your .vscode/launch.json file should look something like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Tests",
"type": "node",
"request": "launch",
...
"skipFiles": ["<node_internals>/**", "${workspaceFolder}/node_modules/**/*.js"]
}
]
}
Only this worked for me.
"debug.javascript.terminalOptions": {
"skipFiles": [
"<node_internals>/**"
]
}
Just to amplify on Mauro Aguilar's correct answer, here are the complete contents of my launch.json file. Note that I am debugging a ReactJS app (circa 2021) with VS Code 1.60.2 on Windows 10 using Chrome v.94. If you're using a Linux machine or a Mac, YMMV.
{
// 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": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}",
"skipFiles": ["<node_internals>/**/*.js", "${workspaceFolder}/node_modules/**/*.js"]
},
]
}
this is my launch.json file (it works for me):
{
"version": "0.2.0",
"configurations": [
{
"type": "edge",
"request": "launch",
"name": "Launch Edge against localhost",
"url": "http://localhost:4200",
"webRoot": "${workspaceFolder}",
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js"
]
}
]
}
For flutter apps, add to your user settings the following:
"dart.debugExternalLibraries": false,
"dart.debugSdkLibraries": false,
For some reason, I've needed to add both types of skip file entries,
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js",
"<node_internals>/**"
],
This seems to have resolved the issue.
"dart.debugExternalLibraries": false,
"dart.debugSdkLibraries": false,
If I add this, the debug stop after a few steps into
For Typescript built with Webpack, I had to put the exclusions in launch.json - putting them in settings.json/terminalOptions had no effect.
Also, I had to exclude the pattern of the generated files, not the source files. In my case it looks like:
"skipFiles": [
"<node_internals>/**",
"**/vendors-*",
],

Attribute 'Program' does not exist - error in VS Code

So I have created a new folder called "Node Projects" and I added it to "Workspace" on VS Code. I then created two sample files called "test.js" and "test2.js". In these files I simply have a single log command to determine which is running.
When I run the test.js, I get the error message "Attribute 'program' does not exist (C:\Users\MyName\Documents\NodeProjects/Node Projects/test.js' so I click on "Open launch.json" button and see this:
{
// 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",
"program": "${workspaceFolder}/Node Projects\\test.js"
}
]
}
Based on my search here on stackoverflow, I believe the problem is with the "program" line, so I go ahead and change it to
"program": "${workspaceFolder}\\test.js"
So now, when I do a F5, the debugger runs the test.js which is great. But because the test.js value is hard-coded, even when I open test2.js on the editor and do an F5, it runs test.js again! So I tried removing the file name (i.e. test.js) from launch.json. But now when I try to run a file, I get the error that "Cannot launch program". Oh and deleting the launch.json didnt help either (it just recreated the initial launch.json file and I was back to square one).
Try this,
"program": "${file}"
${file} is a predefined variable in VS Code for the current opened file.
See https://code.visualstudio.com/docs/editor/variables-reference
Add another key "cwd", and it works for me.
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/xxx/xxx.js",
"cwd": "${workspaceFolder}"
}
]

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

Resources