How do I properly setup VS Code to debug in chrome, using an existing logged in Chrome user profile? - reactjs

I have a React application that I'm writing in VS Code. In order to test and debug my work, I go to the the terminal and enter npm start. This starts the application server and it properly launches chrome under the current logged in user, which has the React Devtools extension installed.
However, I'm also using the Debugger with Chrome extension in VS Code. In order to use those debugger tools, once the application has been started by npm, I need to hit F5 and this launches a new browser window for Chrome. Under this mode, my breakpoints are being hit in VS code, and I can inspect as I need too.
However, the difference is that when I hit F5, VS Code opens a new browser Window that looks like a newly installed instance of Chrome. Since I use Chrome in multiple VMs and architectures, I've setup my Google account so that as I login into my profile on each machine in Chrome, all necessary bookmarks and extensions are loaded into that instance of Chrome.
F5 debugging isn't showing that user profile information, even though a regular launch of Chrome is showing those details, as is the initial launch of the browser when I run npm start. Is there a way to alter my VS Code configurations so that when debugging in VC Code with the Debugger for Chrome extension, I can access my Chrome profile with all my desired extensions and tools?
Also, here's my launch.json file. I think this is used by the debugger.
{
// 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": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}",
}
]
}
I think these are defaults and I think, maybe, this is what NPM is using rather than whatever is launching the debugger & browser with F5.

Add "userDataDir": false to your launch config, like 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": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}",
"userDataDir": false
}
]
}
BUT you must first quit all running instances of Chrome, because it's not possible to put a running Chrome user profile into debug mode. This is why the extension creates a new user profile by default.

Related

Set up & debug a create-react-app and ASP.NET Core Web API on the same port

I've got a Visual Studio solution with an ASP.NET Core Web API and React app (created using create-react-app). I have no issues getting things going, as long as they're on different ports.
But I'm trying to avoid the API calls from the React app needing the port, as I'd like all API calls to use relative paths (for both debug and production + so things will work when testing locally from other devices). In addition, this would avoid having to set up CORS.
I managed to figure out how to set up both React app & API to run from the solution by setting multiple startup projects in the solution. I even was able to get both the React app and API running on the same port -- but not at the same time.
If I try to run them both from Visual Studio as multiple startup projects, the FE app will start up just fine (and on the right port), and even load up in the browser, and when the API loads afterwards the FE can even make calls to the API.
However, refreshing the browser will show the FE has died (it 404s). And if I shut down the BE without shutting down the FE, it will enable me to load the FE again. So it seems like the BE running is killing the FE on the same port.
Here is the API launchSettings.json:
{
"profiles": {
"http": {
"commandName": "Project",
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:7032"
}
}
}
Here is the FE launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "localhost (Edge)",
"url": "http://localhost:7032",
"webRoot": "${workspaceFolder}"
},
{
"type": "chrome",
"request": "launch",
"name": "localhost (Chrome)",
"url": "http://localhost:7032",
"webRoot": "${workspaceFolder}"
}
]
}
Additionally, on the FE I have a .env file that is simply:
PORT=7032
In my solution's Startup Project properties, I have both API and FE projects set as "Start", with the API first in the order:
My understanding is this should be possible, but it seems like I should have it set up properly, but I'm obviously missing something.
Due to the various server components mixed (JavaScript dev server and Kestrel/IIS Express), you shouldn't expect port sharing to work on your local dev machine. Thus, running React and ASP.NET Core at different ports is what you should follow, and you should run ASP.NET Core on a port other than 7032.
Once you move ASP.NET Core away, revise your React project with the proper proxy options in setupProxy.js, as documented,
https://learn.microsoft.com/en-us/visualstudio/javascript/tutorial-asp-net-core-with-react?view=vs-2022#start-the-project
Note that this only works when you debug the React web app locally and when deploying to production environment the proxy must be configured on your web server (nginx/IIS and others) and the actual steps vary a lot.

How to debug server-side code in a express.js/React/Redux/gql/Apollo stack in VSC?

Coming from a PHP background I am having real trouble debugging server-side code using the following set-up:
Visual Studio Code
Yarn
node.js/express.js
React
Redux
gql/Apollo
I start the local server with yarn start and then "Launch Edge against localhost" with 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": "msedge",
"request": "launch",
"name": "Launch Edge against localhost",
"skipFiles": ["<node_internals>/**"],
"url": "http://localhost:3001",
"webRoot": "${workspaceFolder}"
}
]
}
I have made changes to a gqp call but I am unable to get the debugger to trigger inside what I believe to be the server-side code that executes the database query located at src/data/queries/[filename].js OR after the call is made on the client side, nor do I see any error that could shed some light on what has gone wrong.
Could someone please kindly point me in the right direction for debugging in this kind of set-up? At the moment the call seems simply to fail and nothing happens.
Thank you.

How to preserve extensions in Firefox with VSCode and request Launch

I'm attempting to debug a React app with React Dev Tools and Firefox Developer Edition. I can't install React Dev Tools to the profile Firefox is using when it is launched via VSCode. I have React Dev Tools installed when I launch Firefox myself via opening it in /Applications.
However, when I run the below launch.json profile, there are no extensions shown under Preferences -> Extensions & Themes -> Extensions in FireFox after it launches.
{
"name": "Launch localhost",
"type": "firefox",
"request": "launch",
"reAttach": true,
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}"
},
However, I can follow the directions here and launch Firefox with the terminal and attach my debugger to it. This Profile has React Dev Tools and it works fine running:
/Applications/Firefox\ Developer\ Edition.app/Contents/MacOS/firefox -start-debugger-server
launch.json:
{
"name": "Attach",
"type": "firefox",
"request": "attach",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}"
},
But how do I make the first one work with a launch request instead of attach? From reading it seems to have something to do with the profile
I had the same problem and was able to fix it by adding to my launch.json:
"profile": "my-firefox-profile"
Firefox can have miltiple profiles installed. You can check them by typing about:profiles in firefoxes adress bar.
It will show you a list of profiles. The one marked as Default profile: yes should be the one to choose.
My launch.json now looks like this:
{
"name": "Firefox",
"type": "firefox",
"request": "launch",
"reAttach": true,
"url": "http://localhost:4300",
"webRoot": "${workspaceFolder}",
"profile": "default-release-1"
},
Background:
Firefox profiles hold settings like browsing history, bookmarks - and most importantly - add-ons. By telling VSCode to use a specific profile it serves with the installed plugins.
This will also work if you have the same problem with Angular or Vue.
In addition to the accepted answer, I had to also add the following to launch.json to make Firefox launch with the specified profile.
"keepProfileChanges": true
I am using VSCode 1.52.1 and Firefox 84.0.2.

How to deploy Gmail Contextual Gadget on Google Marketplace as App for Gmail?

I have a simple plugin that renders gmail data. I want to deploy on Google Marketplace.
I followed this link : https://developers.google.com/gmail/contextual_gadgets
I have uploaded the zip with manifest.json file on chrome developer console. The plugin is available in the marketplace. But when I install and run the plugin it opens the website rather than enabling the plugin on Gmail.
I have entered details into Google Apps Marketplace SDK, defined the email extractors.
I have created the manifest.json file as follows and uploaded to developer console. Below code is just an example, I have mentioned valid values in the JSON.
{
"manifest_version": 2,
"name": "My App",
"short_name": "My App",
"version": "1.0.0",
"description": "Desctription................",
"app" : {
"launch" : {
"web_url" : "https://www.myapp.com"
}
},
"icons": {
"128": "icons/128x128-icon.png",
"16": "icons/16x16-icon.png"
},
"container": ["DOMAIN_INSTALLABLE"],
"api_console_project_id": "XXXXXXXXXX"
}
The application was published on marketplace. But the plugin is not enabled in my gmail app. Rather it opens the website link.
Please guide what is missing in the configuration or in the manifest file.
Thanks in advance!

Debugging webpack dev server in vs code?

Is it possible to configure launch.json for debugging webpack dev server? In my case, I'm working on a universal (server-rendered via express) React app and it would be really nice to be able to debug the server side directly in VS Code.
I've been working on a VueJS application using Webpack and I was able to successfully debug it using the VSCode chrome debugger after digging around for a few hours. I know your application is a React one, but I'll try and explain the steps I went through to get debugging working. Hopefully that helps you configure your launch.json to debug a react/webpack app in VSCode. Here was my final launch.json:
{
// Using the chrome debugger to debug a Vue application
"type": "chrome",
"request": "launch",
"name": "Chrome launch",
// Set the URL to match the root URL of your application
"url":"http://localhost:8000/#/",
"webRoot": "${workspaceRoot}",
/** Configure source map paths using the instructions below */
"sourceMapPathOverrides": {
"webpack:///./src/*.js": "${workspaceRoot}/src/*.js",
"webpack:///src/*.vue": "${workspaceRoot}/src/*.vue",
"webpack:///./node_modules/*": "${workspaceRoot}/node_modules/*"
}
}
The key for me was correctly configuring the soureMapPathOverrides option in launch.json. First, make sure that the "devtool" option in the webpack config has been set to either "source-map" or "cheap-eval-source-map" (I used "source-map", other settings may also work, but I have not tested them).
--Setting the sourceMapPathOverrides--
What you want to do is map the source map files (which seem to be incorrectly mapped by default, at least for me) to their corresponding location on your local machine. To do this, run the program normally using webpack-dev-server or webpack. Then, open it up in Chrome and open up the devtools "sources" tab. In the "sources" tab, within the navigator (on the left by default), open the network tab and look at the folder structure.
You should see a folder structure that is something like:
top
localhost:8000
assets
src
...
(no domain)
...
webpack://
(webpack)
...
(webpack)-dev-server
...
.
...
src
...
Now, you should be able to find both the transpiled files and your original source files somewhere here (If not, double check your "devtool" was set to "source-map" or "cheap-eval-source-map". Now, you need to map each source file to their location on your hard drive. Preferably, you want to map them by file extension, rather than individually. In the case of Vue, I had to map .js and .vue files to their corresponding locations, with the node-modules folder mapped separately (as you can see in my launch.json). It's probably going to be a different mapping for react.
Set the launch.json url to match the URL of your webpack application and your launch.json should be set!
Now, you should be able to run the file using webpack-dev-server (or webpack) and then start the debugger. You should be able to set breakpoints in VSCode and debug as normal. Hopefully this helps someone.

Resources