9

How can I quickly test JavaScript in the VSCode Console as shown in the picture below? Do I have to install an extension to do this? I'm using VSCode in both Ubuntu and Windows 10 but I'm not seeing Console. All I see is Debug Console, Problems, Output and Terminal. I'm currently using the Quokka extension but I would really like to test JS in the way pictured below without having to open a browser to do that.

Testing JS in VSCode Console

EDIT 2 Chrome JavaScript Console is used in conjunction with an index.html file and VSCode. I really wish the JS Console was built into VSCode. enter image description here

asked Apr 3, 2020 at 0:22
11
  • 2
    You could always try running node from the Terminal Commented Apr 3, 2020 at 0:34
  • 2
    Also, this is what unit tests are for. You should write some. Jest is pretty easy to set up. Commented Apr 3, 2020 at 0:36
  • 1
    That picture looks exactly the same as the chrome devtools. I reckon they just got a devtools window and put it on the side of the screen. Commented Apr 3, 2020 at 0:46
  • 1
    @Phil Thanks for recommending Jest. I'm researching it now. Commented Apr 3, 2020 at 0:46
  • 2
    You might find it difficult to test your JS files if you're writing them for the browser (eg functions declared in the window scope). I'm not actually sure what best practices are for that these days but hopefully somebody here does Commented Apr 3, 2020 at 0:52

3 Answers 3

9

Open launch.json

Then paste this entire chunk if empty, or add the single object to an existing list.

{
 "version": "0.2.0",
 "configurations": [
 {
 "name": "Node: Current File",
 "type": "node",
 "request": "launch",
 "program": "${file}",
 "console": "integratedTerminal"
 }
 ]
}

Now when you click on the Debug menu (the bug 🐞> button) on the side panel (or press F5), You'l have a Node: Current File as a run time dropdown option that will execute your JS via Node, and display the results in the attached VSC terminal.

Debug Menu RUN Options See Terminal output on the right ?!

Click the green triangle (my first image) to start debugging (or press F5) and pay attention to the terminal output (in VS code).

NOTE: If you attach break points, by clicking on the line numbers, indicated with red dots, you can use the Debug Console (left of Terminal tab)as a namespace environment to check variables etc (much like Quokka), during control flow evaluation.

I took this snippet from the VS Code documentation. It's a solid read.

answered Apr 3, 2020 at 1:20
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Tobiah. Incidentally, if launch.json doesn't exist this needs to be inside a .vscode directory.
0

I just want to add to this. I had this working as accepted answer with the "Chrome debugger" plugin, which is now deprecated and the built-in js-debug is referenced as the replacement. I spent a good 2 hours for this to work with Angular yesterday. I tried both using the node configuration and also the chrome and edge type like the documentation says should work. Didn't get it to work.

What actually solved it for me was:

  • ng serve in the terminal to bring up the angular dev server on https://localhost:4200 (I have added a localhost certificate to use SSL/TLS, out of the box, dev server runs on http)
  • open chrome and go to https://localhost:4200
  • using ctrl+shift+P >Debug: Open Link and then typing the url https://localhost:4200 to my dev chrome instance

It is now attached properly and the debug console is reflecting that it is actually attached. I can set break points and debug the code.

I should add that I am running WSL and using vs code remote to point to my WSL instance, where my code also resides, within the wsl filesystem.

Typescript debugging documentation, Angular

answered Feb 14, 2023 at 8:44

Comments

0

React Debugging

There is an awesome section in the official VS Code docs that explains how to debug React apps:

https://code.visualstudio.com/docs/nodejs/reactjs-tutorial#_debugging-react

In short, you need to ...

  1. Run your app using npm start
  2. Create a launch.json file with the following contents
{
 "version": "0.2.0",
 "configurations": [
 {
 "type": "msedge",
 "request": "launch",
 "name": "Launch Edge against localhost",
 "url": "http://localhost:3000",
 "webRoot": "${workspaceFolder}"
 }
 ]
}
  1. After your app is running, launch the new run configuration using the green arrow in VS Code
answered Feb 1, 2024 at 2:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.