How to configure Visual Studio Code to work with Node.js - node.js

I have Visual Studio Code and I use it to write my Node.js applications. In tutorials I see that Visual Studio Code has IntelliSense and other debugging tools related to Node.js, but I don't find it when I write my apps so is there any extension, or how can I configure it to work with Node.js?
This is the first time for me to ask a question related to configuration not to code, so please be patient.

You need to install a plugin for Visual Studio.
You can find more information about this here:
https://github.com/Microsoft/nodejstools#readme

My project's working config ${workspaceRoot}/.vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/test-script.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"PROFILE": "development"
},
"externalConsole": false,
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
},
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"processId": "${command.PickProcess}",
"port": 5858,
"sourceMaps": false,
"outDir": null
}
]
}
NodeJS is in the %Path%:
>node -v
v6.7.0

Related

Debugger being skipped during gatsby develop process

Help please, I use to be able to place debugger/breakpoints in the build process of gatsby and debug fine but this is no longer working. It ignores the "debugger". Console.log works fine. Heres my lunch.json, it was working with this setting prior but no longer is.
{
"version": "0.2.0",
"configurations": [
{
"name": "Gatsby develop",
"type": "node",
"request": "launch",
"protocol": "inspector",
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby",
"args": ["develop"],
"stopOnEntry": false,
"runtimeArgs": ["--nolazy"],
"sourceMaps": false
},
{
"name": "Gatsby build",
"type": "node",
"request": "launch",
"protocol": "inspector",
"program": "${workspaceRoot}/node_modules/gatsby/dist/bin/gatsby",
"args": ["build"],
"stopOnEntry": false,
"runtimeArgs": ["--nolazy"],
"sourceMaps": false
},
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome",
"url": "http://localhost:8000",
"webRoot": "${workspaceFolder}"
}
]
}

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"
}
]
}

How to work with multiple files of Typescript in Visual studio code?

I am learning typescript and try to get also the debugging working as well.
I am using Visual Studio Code version 1.14.1
Typescript version 2.4.1
NodeJs version 8.1.4
Here is the repository with the code https://github.com/sherry-ummen/tryingouttypescriptdebugging
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-w", "-p", "."],
"showOutput": "silent",
"isBackground": true,
"problemMatcher": "$tsc-watch"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/Test.ts",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "integratedTerminal",
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/*.js"]
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"outDir": "out/",
"sourceMap": true
},
"files": [
"IShape.ts",
"Circle.ts",
"Triangle.ts",
"Test.ts"
]
}
So the compilation is working fine atleast does not give error. But when I press F5 on visual studio code to run it then I get the following error
Could someone please guide me on how to get this working with visual studio code ?
Ok I figured it out with the help of the comments I got and also tweaking my tsconfig.json file.
I wrote a blog post about it on how to get started with the debugging if someone bumps up with the same issue then they can refer this post https://sherryummen.in/2017/07/14/debugging-multiple-typescript-files-using-vscode/

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.

Visual Studio Code and Debugging mode

I have a small js file in the path /home/***/Downloads/main.js. The content of this file is:
console.log("Hello, world")
My launch.json file looks this way:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/main.js",
"stopOnEntry": false,
"args": [],
"cwd": "/home/matan/Downloads",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
}
]
}
and still I get the following error:
Program 'nodejs /home/matan/Downloads/main.js' is not an absolute path; consider adding '${workspaceRoot}/' as a prefix to make it absolute.
Do you have any idea what I'm doing wrong?
Change
"cwd": "/home/matan/Downloads"
to
"cwd": "${workspaceRoot}"

Resources