Run flutter app on multiple connected devices/emulators simultaneously [duplicate] - flutter

This question already has answers here:
Flutter Hot Reload to multiple devices
(6 answers)
Closed 3 years ago.
How can I run my flutter app on multiple devices at the same time without having to go through the sequential procedure of: select a device -> run, select other device -> run, etc.?
Using:
Android Studio 3.2.1
Flutter 1.0.0
Dart 2.1.0

Run command in terminal:
flutter run -d all
or create a script (e.g. runall.sh in root):
#!/usr/bin/env bash
flutter run -d all
and go to "Run" -> "Edit Configurations". Press "+" in upper left corner -> select "Bash". Then set:
Name: runall
Script: [path to runall.sh script]
Interpreter Path: /bin/bash
Select "runall" instead of "main.dart" beside run icon. Performing run (also through shortcut) will now run app on all devices.
Drawback: You'll have to enter "r" followed by Enter in run terminal for hot reload. Icon and shortcut does not work. Hot reload is performed on all devices though.
Just a workaround for now. I'm pretty sure the flutter plugin will cover this soon.

There are many ways to do this as previously answered. If you use VS Code instead of Android Studio as your Flutter IDE, this is how you can use the VSC launch configuration and tasks to run concurrently from a single launch and have hot reload enabled for all devices.
If you have an issue with executing flutter run -d all this is an alternative solution will which let you specify the devices that should run.
Ensure that the devices you specify are available when running flutter devices.
Your current launch.json file may look something like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"type": "dart",
"request": "launch",
"flutterMode": "debug"
}
]
}
Setup
You will need to update this launch.json file and create tasks.json in the same .vscode folder that is in your application's root directory.
Paste only the below code into launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Flutter-All",
"preLaunchTask": "Flutter-Launch-All",
"type": "dart",
},
{
"name": "Flutter-iOS",
"preLaunchTask": "Flutter-Launch-iOS",
"type": "dart",
},
{
"name": "Flutter-Android",
"preLaunchTask": "Flutter-Launch-Android",
"type": "dart",
},
{
"name": "Flutter-Web",
"preLaunchTask": "Flutter-Launch-Web",
"type": "dart",
}
],
}
Paste only the below code into tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Flutter-Launch-All",
"dependsOn": [
"Flutter-Launch-iOS",
"Flutter-Launch-Android",
"Flutter-Launch-Web"
]
},
{
"label": "Flutter-Launch-iOS",
"type": "shell",
"command": "flutter run -d 'iPhone 11' "
},
{
"label": "Flutter-Launch-Android",
"type": "shell",
"command": "flutter run -d 'AOSP on IA Emulator' "
},
{
"label": "Flutter-Launch-Web",
"type": "shell",
"command": "flutter run -d 'Chrome' "
}
]
}
Replace the device names accordingly ('iPhone 11', 'AOSP on IA Emulator', 'Chrome').
Firing up all devices
Press the F5 key.
And you're done.
If the F5 shortcut to Start Debugging does not work for you, navigate to Debug & Run on the side panel and select the Flutter-All Configuration you've just created and then Run.
You will then see the terminal window appear and will able to switch between the individual hot-reload sessions running (as Tasks in their own shell).
Some Background
We use 'Compound Tasks' by way of the dependsOn option on a Task and not 'Compounds' which are for Configurations.
As it is not possible to launch Configurations concurrently, only sequentially, we use tasks which can run concurrently.
Hence, the "Flutter-All" Configuration executes the tasks of the iOS, Android and Web Configurations.
If using Compounds, a Configuration will need to complete before the next runs which is not what we want.
With Tasks we can choose to execute them sequentially however by default they will execute concurrently when using the dependsOn option.
//Do not use this unless you want to use Configurations only by testing them sequentially and not tasks
"compounds": [
{
"name": "Flutter-All",
"configurations": ["Flutter-iOS", "Flutter-Android", "Flutter-Web"],
}
]

If you don't want to use the command line directly each time, you can do the following workaround:
Download bashsupport plugin (link)
Create new configuration of bash, leave the script field empty, and in the interperter options cell insert:flutter run -d all. It should look something like:
If step two didn't work, create a file call something like run_all.sh in your root project. and put the next lines there(assuming that bin/bash is the path to your bash):
#!/bin/bash
flutter run -d all
type in your terminal:chmod 755 run_all.sh.
Specify run_all.sh in your configuration and bin/bash as your interprter path.
Remove flutter run -d all from interperter options.
It should look something like:

You can always have an external tool watch the files for you and trigger a hot reload.
Flutter supports certain signals to trigger a hot reload natively
--pid-file Specify a file to write the process id to. You can send SIGUSR1 to trigger a hot reload and SIGUSR2 to trigger a hot restart.
Here is an fast example:
#!/usr/bin/env bash
set -euo pipefail
# Remove previous pid files
rm -f /tmp/flutter.pid
# Run in a loop a hot reload call in a subshell
(while true
do
# Wait for flutter to start before monitoring pid
while [[ ! -f /tmp/flutter.pid ]]; do sleep 1; done;
# Send hot reload signal when files change
find lib/ -name '*.dart' | entr -n -d -p kill -USR1 $(cat /tmp/flutter.pid)
done) &
# Run all devices under 1 pid
flutter run -d all --pid-file /tmp/flutter.pid
Idea from: https://medium.com/#kikap/how-to-automatically-hot-reload-flutter-when-dart-source-files-change-6e8fdb523004
For more details on entr: http://eradman.com/entrproject/entr.1.html

Related

VSCode terminal task not using zsh profile

I'm trying to run a task on window load in VSCode where a terminal opens and nvm use && yarn dev is run by default. However, running this shell tasks seems to not load my zsh profile.
The output I get from running my task is:
The terminal process "zsh '-c', 'nvm use && yarn dev'" terminated with exit code: 127.
Terminal will be reused by tasks, press any key to close it.
But if I then manually start a new terminal and run the same command (ie: by pressing plus, opening a new integrated terminal), it will work as intended.
Suspecting that VSCode isn't loading my profile for some reason, I tried adding the following to my task, it resulted in the error /bin/zsh: can't open input file: nvm use && yarn dev The terminal process "zsh '-l', 'nvm use && yarn dev'" terminated with exit code: 127..
// in dev task
"options": {
"shell": {
"executable": "zsh",
"args": ["-l"]
}
},
.vscode/tasks.json
{
"version": "2.0.0",
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "dedicated",
"showReuseMessage": true
},
"tasks": [
{
"label": "Create terminals",
"dependsOn": [
"Dev",
],
// Mark as the default build task so cmd/ctrl+shift+b will create them
"group": {
"kind": "build",
"isDefault": true
},
// Try start the task on folder open
"runOptions": {
"runOn": "folderOpen"
}
},
{
"label": "Dev",
"type": "shell",
"command":
["nvm use && yarn dev"],
"isBackground": true,
"problemMatcher": [],
"presentation": {
"group": "dev-group"
}
},
]
}
This worked for me-
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh",
"args": ["-l", "-i"]
}
},
github.com/microsoft/vscode/issues/143061
try adding this to your settings.json
"terminal.integrated.profiles.osx": {
[...]
"zsh": {
"path": "/bin/zsh -l",
"args": [
"-l"
]
},
[...]
},
Note that the important part is
"path": "/bin/zsh -l",
I had the same problem and I found that for some reason VScode does not take into consideration the -l flag passed in args. So you can just include it with path.
If you do not have terminal.integrated.profiles.osx in your settings, you can copy it from the default settings (open the Command Palette and search for 'default settings').
I did not need to do this, but you can make sure that zsh is the default terminal profile for VScode by setting terminal.integrated.defaultProfile.osx to zsh
Try running echo $SHELL from VSCode's integrated terminal. If you're on a Mac or Linux machine, you can compare that output to the output from the terminal app (outside VSCode). It's possible your default shell in VSCode is set incorrectly or using a copy of zsh at another location. If so, set VSCode's default shell through the command palette (Terminal: Select Default Shell).
Also check out your shell's default profile (Terminal: Select Default Profile) from the command palette and make sure it's set to zsh -l... using the -c argument (non-login non-interactive) will prevent ~/.zshrc from being executed, which sounds like what's going on here given your error output.
Finally, confirm your profile is located correctly (at ~/.zshrc) and that both nvm and yarn PATHs are exported. Alternatively, if you're trying to reference yarn locally (if for some reason you only installed it locally), you'll need to run yarn via npx...
You may need to add an automation profile as well
"terminal.integrated.profiles.osx": {
"zsh": {
"path": "/bin/zsh -l",
"args": ["-l"]
}
},
"terminal.integrated.automationProfile.osx": {
"path": "/bin/zsh"
}
macOS 12.6.1 | vscode 1.74.0
I did not manage to do any if it since none of this worked, so I have just removed warning...
"terminal.integrated.showExitAlert": false
Or via GUI
I hope that will not get minus points here...

Is there a way to open a Visual Studio Code internal powershell window from NPM Script?

I am currently doing some work with npm scripts and can't find an answer to my question online!
I want to run 3 NPM scripts by typing one command. Here the scripts:
"start-jsonserver:platform": "ng serve --configuration jsonserver"
"start:corePlugins": "ng serve corePlugins",
"start:jsonserver": "cd ../json-server & npm run start",
"start:allJsonEnvironment": "npm run start-jsonserver:platform && npm run start:corePlugins && npm run start:jsonserver",
Note that the last command is not working as I want it to as it stops after the first ng serve is "done". I have also tried the same command with just one & but this has the same effect
I have found a solution which is opening 3 seperate Powershell windows:
"start:allJsonEnvironment": "start powershell npm run start-jsonserver:platform && start powershell npm run start:corePlugins && start powershell npm run start:jsonserver"
The Problem is this opens the normal 'standalone' Powershell windows which, to be honest is really ugly and I am used to seeing the VS Code internal Powershell windows (3 at the same time) since its easy to spot if something went wrong.
Like that:
So if there is a way to open these 'internal' Powershell windows from the npm script I would really appreciate help.
(I know there is a way to run all three scripts in one internal window but that is not what I am looking for!)
Okay after some research I have found another solution.
Its not envolving NPM scripts but it is actually the better solution.
My personal solution to the Problem was to use the VS Code Tasks and dependsOn to connect all three commands:
{
"version": "2.0.0",
"tasks":
[
{
"label": "corePlugins",
"type": "shell",
"options": {
"cwd": "${workspaceFolder}/frontend"
},
"command": "ng serve corePlugins"
},
{
"label": "serve_conf_json",
"type": "shell",
"options": {
"cwd": "${workspaceFolder}/frontend"
},
"command": "ng serve --configuration jsonserver"
},
{
"label": "json-server",
"group": "test",
"type": "shell",
"options": {
"cwd": "${workspaceFolder}/json-server"
},
"command": "npm run start"
},
{
"label": "startAllJson",
"dependsOn": [
"json-server",
"corePlugins",
"serve_conf_json",
]
}
]
}
So now I can just run the Task startAllJson and it opens 3 VSCode Terminals and runs those commands.
I know this might not be the perfect answer to my question but it was the best solution I found in a short amount of time.

Problem of using args in VS Code custom tasks.json

So, Inside of the tasks array in the .vscode/tasks.json, I have this one task:
{
"label": "buildReleaseIos",
"type": "shell",
"command": "flutter build ios -t lib/main_prod.dart --dart-define=DART_DEFINE_APP_NAME=App Name --dart-define=DART_DEFINE_APP_SUFFIX=.myapp"
}
This task is valid and I'm able to execute it properly.
The problem is, if I move the --dart-define from command to the args array (which I think is the better practice), like this:
{
"label": "buildReleaseIos",
"type": "shell",
"command": "flutter build ios -t lib/main_prod.dart",
"args": [
"--dart-define=BCP_DART_DEFINE_APP_NAME=App Name",
"--dart-define=BCP_DART_DEFINE_APP_SUFFIX=.myapp",
]
}
There's this error:
> Executing task: 'flutter build ios -t lib/main_prod.dart' '--dart-define=DART_DEFINE_APP_NAME=App Name' --dart-define=DART_DEFINE_APP_SUFFIX=.myapp <
/bin/bash: flutter build ios -t lib/main_prod.dart: No such file or directory
The terminal process "/bin/bash '-c', ''flutter build ios -t lib/main_prod.dart' '--dart-define=DART_DEFINE_APP_NAME=App Name' --dart-define=DART_DEFINE_APP_SUFFIX=.myapp'" terminated with exit code: 127.
But, I also have similar args usage in the launch.json which works fine:
{
"name": "Flutter Debug Prod",
"request": "launch",
"type": "dart",
"program": "lib/main_prod.dart",
"args": [
"--dart-define=DART_DEFINE_APP_NAME=App Name",
],
}
Maybe I'm mistaken on how args in tasks.json works? Can you show me how to properly use args?
Thanks
That translation in bash should look like
"/bin/bash '-c', 'flutter' 'build' 'ios' '-t' 'lib/main_prod.dart' '--dart-define=DART_DEFINE_APP_NAME=App Name' '--dart-define=DART_DEFINE_APP_SUFFIX=.myapp'
which means you need flutter in program and everything else as separate elements of args, or you can just put everything in program, but then whitespace quoting can be a bit wonky.

Configuring multiple commands to run in parallel in VS Code tasks (to compile and autoprefix Sass)

I had previously been using Koala to compile my Sass with autoprefixing and minifying (on Windows), but have come to find that Koala is no longer maintained. I'm therefore trying to figure out how people usually compile Sass, autoprefix it, and minify it automatically on save.
I'm not super experienced with command line tools like Gulp but have used NPM enough to get to the point of being able to install Dart Sass, PostCSS, etc., and since I use VS Code, have decided that its internal Tasks feature seems like the easiest way to go.
Currently if I do this in the VS Code terminal:
sass --watch sass:. --style compressed
It works, i.e., it successfully watches for changes in the sass directory and outputs a minified .css file in the parent directory.
If I stop that and do this instead:
postcss style-raw.css --output style.css --use autoprefixer --watch
It also works. I had to rename the original .scss file because apparently postcss doesn't allow --replace and --watch at the same time.
So right now, I have style-raw.scss which compiles to style-raw.css when I run the sass command, and style-raw.css gets autoprefixed and output to style.css when I run the postcss command.
Where I'm stuck is getting both commands to run at the same time via a Task. In my tasks.json file I have:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Sass Compile",
"type": "shell",
"command": "sass --watch sass:. --style compressed | postcss style-raw.css --output style.css --use autoprefixer --watch",
"problemMatcher": [
"$node-sass"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
This is connected to the Build task, which has a keyboard shortcut of ctrl+shift+B, so my ultimate goal thus far has been to be able to just hit ctrl+shift+B to start everything up getting watched and compiled and autoprefixed, etc.
Currently though, only the Sass command runs when I use the keyboard shortcut. I found another post somewhere that said the pipe should work for multiple commands, but it doesn't seem to, or perhaps you can't --watch two things at the same time, I have no idea. I tried an array for command: but that either doesn't work or I didn't have the right format.
Or perhaps there's an entirely different and better way to go about all this, but if anyone can help with using these two commands together, that'd be much appreciated.
UPDATE: SOLUTION --------------------------------------------------------
With the kind help of #soulshined below, I got the multiple commands working (the dependsOn option was the trick), but evidently it won't run two commands with the --watch parameter in the same terminal. For my use case this wouldn't work and I eventually found this extremely helpful article that explains how to run multiple tasks in a split terminal by grouping them.
If anyone else runs across this with the same use case, here is the working tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Compile CSS",
"dependsOn": [
"Sass Compile",
"Prefix Output",
],
"group": {
"kind": "build",
"isDefault": true
},
},
{
"label": "Prefix Output",
"type": "shell",
"command": "postcss style-raw.css --output style.css --use autoprefixer --watch",
"presentation": {
"group": "cssCompile"
}
},
{
"label": "Sass Compile",
"type": "shell",
"command": "sass --watch sass:. --style compressed",
"problemMatcher": [
"$node-sass"
],
"presentation": {
"group": "cssCompile"
}
}
]
}
UPDATE 2: GULP --------------------------------------------------------
Randomly ran across my own post and thought I would add that I now use Gulp. I don't remember why but the VS Code tasks turned into a hassle later on, and Gulp turned out to be not that hard to learn.
Where I'm stuck is getting both commands to run at the same time via a Task
Running concurrently can be tricky to accomplish; consider taking advantage of the dependsOn property. Here is a brief example of running commands tasks consecutively:
"version": "2.0.0",
"tasks": [
{
"label": "Echo All",
"type": "shell",
"command": "echo Done",
"dependsOn": [
"Last"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Last",
"type": "shell",
"command": "echo 'Did it last'",
"dependsOn": "First",
},
{
"label": "First",
"type": "shell",
"command": "echo 'Doing it first'",
}
]
That's a language [shell] agnostic solution. If you would like to run multiple commands you can try adding a semi colon after each statement:
"command": "sass --watch sass:. --style compressed; postcss style-raw.css --output style.css --use autoprefixer --watch"

Cannot debug in VS Code by attaching to Chrome

I have default config in launch.json. The site runs on port 8080.
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080",
"webRoot": "${workspaceRoot}"
},
{
"type": "chrome",
"request": "attach",
"name": "Attach to Chrome",
"port": 9222,
"webRoot": "${workspaceRoot}"
}
]
}
However, when I click on the Debug button, I get this error:
Cannot connect to the target: connect ECONNREFUSED 127.0.0.1:9222
Question 1: Why does VS Code assign port 9222 when creating this json?
What is so special about this port that MS decided to put it in this launch.json?
Question 2: What do I need to change to make things work?
The Launch debug always launches a new window.
I am asking specifically about Attach debug option, so that it will open in a new tab instead.
When using the configuration url, vscode will search for a tab with the EXACT url and attach to it if found.
Use the configuration urlFilter, which can have wildcards like *, to attach the debugger to any sub route in your url.
e.g. "urlFilter": "http://localhost:4200/*"
The complete exacts steps to take:
configure your launch.json file to looks something like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "attach",
"name": "Attach to Chrome",
"port": 9222,
"urlFilter": "http://localhost:4200/*",
"webRoot": "${workspaceFolder}"
}
]
}
Close all opened chrome instances (make sure that all of them are killed using task manager in windows).
Launch chrome with the following parameter: --remote-debugging-port=9222
Make sure that the port in this parameter is the same as the one configured in 'port' property of the attache to chrome configuration in the launch.json file (like the example above)
You can add this parameter in your chrome shortcut properties by:
Right click your shortcut file and select properties
Chain it to Target property, e.g.
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
Navigate to your site. In this example, http://localhost:4200
Run 'Start debugging' in VS Code.
You need to install Debugger for Chrome extension for this to work. Open extensions in VS Code and search for Debugger for Chrome
You need to run a web server on the URL specified in the first configuration (default to http://localhost:8080). I use serve npm package that I installed globally. From my app folder I run serve -p 8080
Select Launch Chrome against localhost option. It will launch the browser and you can set breakpoints in your code and the debugging should work.
Regarding the second configuration (Attach to Chrome). There's nothing special about the port. In order to attach to Chrome you need to run Chrome with remote debugging enabled on port specified in the config. For example chrome.exe --remote-debugging-port=9222. I personally never use this options. Just follow the three steps above and you should be fine.
The Debugger for Chrome extension is deprecated. You do not need it anymore.
I came across this question when looking for help using the "Attach to Chrome" configuration in VSCode. While the accepted answer did give me a few hints, I did have to do some more digging. Here are the steps that worked for me in case anyone else finds them useful:
Install the Debugger for Chrome extension in VSCode Skip this, it's not needed anymore
Serve your files with a web server of your choice
Launch Chrome with remote debugging enabled
In this new Chrome window navigate to the url that your web server is hosting (http://localhost:8080 for example).
In VSCode, add a configuration to your launch.json file that looks like this:
"configurations": [
{
"type": "chrome",
"request": "attach",
"port": 9222,
"name": "Attach Chrome",
"url": "http://localhost:8080/",
"webRoot": "${workspaceFolder}"
}
]
Press the play button in VSCode with the 'Attach to Chrome' option selected from the drop down.
The key thing needed in the configuration file is the url field. This needs to be the URL where your files are being hosted and this URL needs to be currently open in the Chrome window that you just launched with remote debugging enabled. If you enter everything else right except this field, VSCode will give you an error message that says which urls are available. Something like Cannot connect to runtime process, timeout after 10000 ms - (reason: Can't find a valid target that matches: localhost:8080/. Available pages: ["http://localhost:8080",...
For the sake of completeness, here's how you launch Chrome with remote debugging enabled (from the Debugger for Chrome README):
Windows:
Right click the Chrome shortcut, and select properties
In the "target" field, append --remote-debugging-port=9222
Or in a command prompt, execute <path to chrome>/chrome.exe --remote-debugging-port=9222
MacOS:
In a terminal, execute /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
Linux:
In a terminal, launch google-chrome --remote-debugging-port=9222
A 2020 complement answer...
VS Code has a new debug that is not stable yet but works as default on Insiders version at the moment.
It opens automatically a new Chrome instance with debugging for attached with these settings:
{
"name": "Attach to Chrome",
"port": 9222,
"request": "attach",
"type": "pwa-chrome",
"webRoot": "${workspaceFolder}/src"
}
Like any attached, you can run the debug any time at this new window opened by VS Code.
Or you can run the Chrome as debug mode previously to avoid the VS Code to open a new debug window just like #quicklikerabbit answer https://stackoverflow.com/a/51392142/7050878 but this time without the need of the URL parameter.
The following procedure is for React applications created by create-react-app:
Click the Extensions button in the Activity Bar and install the Debugger for Chrome extension if it's not already installed.
Open a new Terminal window. cd to the client folder if necessary and install chrome-launcher:
npm install chrome-launcher
Switch to the Debugger panel in the Side Bar.
Click the Configuration drop-down at the top of the Side Bar and choose "Add Configuration..." Alternatively, press the little gear icon at the top of the Side Bar to open launch.json and then press the big blue button labeled "Add Configuration..."
From the list of configuration templates, choose Chrome: Attach. The following configuration should be added to launch.json:
{
"name": "Attach to Chrome",
"port": 9222,
"request": "attach",
"type": "pwa-chrome",
"webRoot": "${workspaceFolder}"
},
There's no need to change anything in this configuration.
Add a new file named .env with the following content to the root folder of your project (or to the root of the client folder if it's a full-stack application):
BROWSER=launchChrome.js
Add a new file named launchChrome.js to the same folder, with the following content:
const chromeLauncher = require('chrome-launcher');
chromeLauncher.launch({
startingUrl: process.argv[2],
port: 9222,
}).then(function (chrome) {
console.info('Chrome remote debugging port:', chrome.port);
});
Launch your React app from the Terminal window:
npm start
After a few seconds, you should see the following text:
Starting the development server...
Chrome remote debugging port: 9222
At the top of the Side Panel, choose the configuration Attach to Chrome and press the green triangle.
You can now place breakpoints in your React code and the debugger will break when it hits them. You can even debug your server and your client simultaneously by adding a Node.js: Launch Program configuration to launch.json.
I will put my measly two cents in :) unfortunately or fortunately I had a similar problem and the above post pointed me in the right direction and I learned a little something along the way. Please see my notes below:
1) If you are on Linux, after starting vsCode run the following Linux command:
sudo lsof -i -P -n | grep LISTEN
this will allow you to see what ports are being used, in my case you can see code on 5500.
2) Assuming you have some test html/js code and vsCode has a server and debugger installed then the following files need to be configured as such:
code-workspace file:
/*
Workspace settings override user settings.
https://code.visualstudio.com/docs/getstarted/settings
Check to see if needed ports are listening: sudo lsof -i -P -n | grep LISTEN
*/
{
"folders": [
{
"path": "."
}
],
"settings": {
"liveServer.settings.AdvanceCustomBrowserCmdLine": "google-chrome-stable --remote-debugging-port=9222",
}
}
launch.json:
{
/*
Workspace settings override user settings.
https://code.visualstudio.com/docs/getstarted/settings
Check to see if needed ports are listening: sudo lsof -i -P -n | grep LISTEN
*/
"version": "0.2.0",
"configurations": [
{
"name": "Launch 127.0.0.1:5500",
"type": "chrome",
"request": "launch",
"url": "http://127.0.0.1:5500/${relativeFileDirname}/${fileBasename}",
"webRoot": "${workspaceFolder}/${relativeFileDirname}"
},
{
"name": "Attach 127.0.0.1:5500",
"type": "chrome",
"request": "attach",
"port": 9222,
"url": "http://127.0.0.1:5500/${relativeFileDirname}/${fileBasename}",
"webRoot": "${workspaceFolder}/${relativeFileDirname}/"
},
]
}
setting.json:
/*
Workspace settings override user settings.
https://code.visualstudio.com/docs/getstarted/settings
Check to see if needed ports are listening: sudo lsof -i -P -n | grep LISTEN
*/
{
"cSpell.language": "en",
"git.enableSmartCommit": true,
"git.autofetch": true,
"[html]": {
"editor.defaultFormatter": "vscode.html-language-features"
},
"liveServer.settings.donotShowInfoMsg": true,
"cSpell.userWords": [
"lsof",
"readonly"
],
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"cSpell.enabledLanguageIds": [
"asciidoc",
"c",
"cpp",
"csharp",
"css",
"git-commit",
"go",
"handlebars",
"haskell",
"html",
"jade",
"java",
"javascript",
"javascriptreact",
"json",
"jsonc",
"latex",
"less",
"markdown",
"php",
"plaintext",
"pug",
"python",
"restructuredtext",
"rust",
"scala",
"scss",
"text",
"typescript",
"typescriptreact",
"yaml",
"yml"
],
}
3) NOTE make sure all browsers are closed before starting up the 'Live Server'. Spin-up your server 'Live Server'to open the file you would like to debug as per the configuration for the chrome browser stipulated in the vsCode File>Preferences>Settings, NOTE use 127.0.0.1 I had no luck with localhost, also the default port is 5500. The browser is now launched as per the vsCode-workspace file setting "liveServer.settings.AdvanceCustomBrowserCmdLine": "google-chrome-stable --remote-debugging-port=9222", this is where the debug magic takes place. NOTE make sure all browsers are closed before starting up the 'Live Server'.
Especially check to make sure there are no chrome extension like Hangouts running, this will also prevent Chrome from opening port 9222, I had to click the Exit option on the task to kill all Chrome extension in my example:
4) Now if you run sudo lsof -i -P -n | grep LISTEN you will see vsCode 'Live Server' serving on port 127.0.0.1:5500 and the debugger doing its thing on port 127.0.0.1:9222; If you do not see both ports opened then something is not correct and you will need to confirm STEP 3) listed above.
5) You can check the web interface for the debugger by entering http://127.0.0.1:9222/ in a empty browser tab, this url will display links to every tab and extensions open and allow you to poke around with the debugger, click on the link to the file you want to debug, in my case 127.0.0.1:5500/Leason_66/index.html, this is the port and link vsCode will communicate with and re-render in the IDE Debugger.
6) Note: Make sure you are on the file you want to debug. We are almost there, now just click on the Debug Icon then go to the GREEN Play Button and select the attach option from the drop down, please note the information configured in the launch.js file is what will appear in the drop down.
7) Time for some action! Now all you have to do is click on the GREEN Play Button and the Debugger will now attach to tab you have open at 127.0.0.1:5500/<path you your file> and do its debug on port 127.0.0.1:9222
8) Happy Engineering :)
Try this:
"type": "edge",
"request": "launch",
"name": "Launch Edge",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}"
"file": "${workspaceFolder}/index.html"
Peek into Task Manager. You may have Chrome instances hanging there. Only after killing them will you be able to run the remote and successfully start the debugger.
Run Chrome on macOS without command lines.
We'll create a shortcut of Chrome that will run --remote-debugging-port=9222 for us.
Steps
Open the native application called Automator.
It opens a selector. Our goal is to create an application, hence we choose Application.
Look for shell.
Add the next script /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
Then save it with your desire. Our application shortcut is done.
Run the Spotlight with CMD + Space and type your app name, mine starts with Chrome Debug. Open it and voilĂ 
Add the launch.json this configuration (you can edit the localhost's port):
{
"name": "Chrome lh:4200",
"type": "chrome",
"request": "attach",
"urlFilter": "http://localhost:4200/*",
"port": 9222,
"webRoot": "${workspaceFolder}"
}
Run that debug configuration. VSCode is waiting. Open your localhost URL on the Chrome, VSCode debugger will show this state: