vscode can't debug mocha test with es6 babel - node.js

Getting error Cannot use import statement outside a module when runing from vscode debug.
When I run mocha from command line it works perfectly fine.
VSCode config:
{
"args": [
"test",
"--require #babel/register"
],
"internalConsoleOptions": "openOnSessionStart",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/mocha",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
},
command line which isworking: ./node_modules/mocha/bin/mocha test --require #babel/register --exit

This configuration worked for me
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"cwd": "${workspaceRoot}",
"program": "${workspaceRoot}/node_modules/mocha/bin/mocha",
"args": [
"test",
"--require", "#babel/register",
"--timeout", "999999",
"--colors"
],
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**"],
"sourceMaps": true,
"runtimeArgs": ["--nolazy"]
}
Also updated all bable packages to 7.x earlier I had babel-cli in my dependencies changed it with #babel/cli

Related

In VS Code why does Yarn debug stop at cli.js when Caught Exceptions is enabled?

I'm trying to debug a Jest test and breakpoint at "Caught Exceptions" in VS Code but it stops at /.node/corepack/yarn/1.22.15/lib/cli.js which is irrelevant to me.
I've tried to add several paths to skipFiles in launch.json but no luck so far.
How can I ignore the Yarn/node core files?
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"name": "vscode-jest-tests.v2",
"request": "launch",
"runtimeExecutable": "yarn",
"runtimeVersion": "16.13.2",
"program": "jest",
"args": [
"--runInBand",
"--watchAll=false",
"--testNamePattern",
"${jest.testNamePattern}",
"--runTestsByPath",
"${jest.testFile}"
],
"cwd": "${workspaceFolder}/next/",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "jest"
},
"skipFiles": [
"<node_internals>/**/*.js",
"~/.node/**/*.js",
"**/.yarn/**",
"**/node_modules/**",
"**/yarn/**",
]
},
]
}
Adding **/.node/corepack/yarn/** made it ignore those files.
"skipFiles": [
"<node_internals>/**",
"**/node_modules/**",
"**/.node/corepack/yarn/**"
]

How to debug Typescript code in Visual Studio Code with ts-node-dev and correct line numbers

I have a Typescript project that is launched as follows:
ts-node-dev --preserve-symlinks --inspect=0.0.0.0 -- src/server.ts
I can debug it with Visual Studio Code, but the debugger breaks at the wrong lines. The only reasonable explanation I can think of, is that ts-node-dev does not point the debugger to the source maps (which are there).
How can I correctly debug Typescript code executed by ts-node-dev?
Configuration for debugging in vs code with ts-node-dev to attach and launch debugger:
package.json:
{
"scripts": {
"dev:debug": "ts-node-dev --transpile-only --respawn --inspect=4321 --project tsconfig.dev.json src/server.ts",
"dev": "ts-node-dev --transpile-only --respawn --project tsconfig.dev.json src/server.ts",
}
}
launch.json:
{
"version": "0.1.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to dev:debug",
"protocol": "inspector",
"port": 4321,
"restart": true,
"cwd": "${workspaceRoot}"
},
{
"type": "node",
"request": "launch",
"name": "Debug",
"protocol": "inspector",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "dev"]
}
]
}
I had the same question, which brought me to this (old) post. I found the solution to my problem at https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695 so am posting it here in case anyone else finds themselves here!
This VS Code configuration allowed me to stop at breakpoints on the correct lines in my TypeScript code:
{
"version": "0.2.0",
"configurations": [
{
"name": "Example",
"type": "node",
"request": "launch",
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"args": ["src/script.ts", "--example", "hello"],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**", "node_modules/**"]
}
]
}
This is what worked for me. attach type debugger was not working for me but launch type is working fine. Breakpoints works as well even though sometimes it goes to the ts-node-dev source files and I guess we can't do anything about it.
It runs tsnd which is just the alias for ts-node-dev.
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"name": "launch",
"request": "launch",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/tsnd",
"program": "${file}",
"args": [
"--transpile-only",
"--respawn",
"--project",
"tsconfig.dev.json",
],
"skipFiles": [
"<node_internals>/**"
],
},
}
"program" could be changed to run a specific file by replacing ${file} with that filename but with the above config, it will run the opened file with the debugger.

How to debug while testing in sailsjs

I am trying to debug a 500 Internal server error in sailsjs. I have written tests in mocha and am getting the error from there. I am not sure on how to run a debugger when I start the testing with npm run test.
I am getting the error from another file, is it possible to put breakpoints over there and view it while testing
Thank you for your help
So, this is the solution that I came up with.
Using VScode,
configure the launch.json to be
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test/**/*.spec.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": ["--timeout", "999999", "--colors", "${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
Change according to how the test files are labeled in your project

VSCode node debugger

Hi there I am trying to set up breakpoints in VSCode and use the Chrome debugger. My repo has a Node JS server that runs on http://localhost.lmig.com:3000/.
The main npm command that I need to run is npm start.
My node version is 6.10.0. My local os is Mac OSX and I use zsh with NVM. So far my launch.json is this:
configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome",
"url": "http://localhost.lmig.com:3000/",
"webRoot": "${workspaceFolder}"
},{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"runtimeExecutable": "npm",
"runtimeVersion": "6.10.0",
"runtimeArgs": [
"run-script",
"start",
"--debugger=3000"
],
"port": 3000,
"restart": true,
"protocol": "legacy",
"remoteRoot": "0.0.0.0:3000",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
// "outFiles": ["${workspaceRoot}/build/**/*.js"],
"sourceMaps": true
}
My package.json is this:
It runs the node process in the integrated terminal but still doesn't seem to hit my breakpoints.
Please help
Just providing this as an alternative launch config, but it is what I use.
You may need to change your "protocol" back to legacy... since you're on an older version of node.
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Debug",
"type": "node",
"request": "launch",
"restart": true,
"program": "${workspaceRoot}/bin/www",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": "build",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development",
"PORT": "3000"
},
"console": "internalConsole",
"outputCapture": "std",
"sourceMaps": true,
"protocol": "inspector",
"skipFiles": [
"${workspaceRoot}/node_modules/**/*.js",
"${workspaceRoot}/lib/**/*.js",
"<node_internals>/**/*.js"
],
"internalConsoleOptions": "openOnSessionStart"
}
]
}

debugging typescript in vscode with autorestart

I am setting up a typescript project with node.
I can debug my main.ts file in vs code with this launch.json configuration:
{
"type": "node",
"request": "launch",
"name": "Lancer le programme",
"program": "${workspaceRoot}/src/main.ts",
"outFiles": [
"${workspaceRoot}/dist/**/*.js"
]
}
This works fine but there is no auto restart when I edit main.ts
To implement auto restart, I launch in my project directory tsc --watch, and then this lauch configuration:
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "nodemon",
"runtimeArgs": [
"--debug=5858"
],
"program": "${workspaceRoot}/src/main.ts",
"outFiles": [
"${workspaceRoot}/dist/**/*.js"
],
"restart": true,
"port": 5858,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"sourceMaps": true
},
The above configuration does autorestart when I edit source files, but the vscode debugger doesn't break anymore...
Has anyone achieved : debugging typscript in vscode with autorestart ?
This is my launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "nodemon",
"runtimeExecutable": "${workspaceFolder}/node_modules/nodemon/bin/nodemon.js",
"program": "${workspaceFolder}/build/index.js",
"restart": true,
"runtimeArgs": [
"--debug=5858",
"--inspect-brk"
],
"port": 5858,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
Note that the prop program must indicate compiled js file. In your case it is ${workspaceRoot}/src/main.ts, but should be ${workspaceRoot}/*compile directory eg. build*/main.js.
Also make sure the typescript recompiles the files to the target directory.

Resources