Is there an "immediate window" in Visual Studio Code to run Javascript?
Asked Answered
C

6

51

Yes, I use F12 in the browser all the time to test out Javascript snippets with the console. I'm tired of that (for the moment anyway).

I've read that in Visual Studio you can use the immediate window to run Javascript interactively.. I've haven't tried it that hard. I think when I did it told me it can't evaluate while in design mode... ugh, what a pain.

I do like to use Visual Studio Code (sublime text historically) sometimes to just mess around with syntax of snippets. Would also be nice if I could just run Javascript there too quickly. Is there a package I could download in VSCode to do so? Or something already built in?

Courtenay answered 21/9, 2015 at 16:24 Comment(7)
Maybe I can try visualstudio.com/en-us/features/node-js-vs.aspx which suggests to install node.js tools for VS. Maybe there is something similar for VS CodeCourtenay
Do you want to run server-side JavaScript (on their computer) or client side (on yours)?Thromboplastic
Not even interested in doing it for anything production-wise. It's more for my own development style of "hey I need to figure out some syntax really quickly. I don't want to deploy even to my test environment to test out the code.. I just need to quickly see some output"Courtenay
I think VSC's new window is meant to evaluate complex expressions while nodejs is running; eg, find a value accessible via method, inside an important object. It wasn't meant for simply evaluating expressions from a neutral state. For that, use either a browser window or NodeJS in interactive mode. You can also try using a site like JSFiddle.Fulmar
Hm...that's a good point. I am just "tire" of the browser window. So maybe NodeJS in interactive is the solution. Would I do that in a console window though? Thinking more deeply... I think I am wanting the features of a good text editor like VSCode - but the immediate functionality of a console.Courtenay
You can set up Run configurations in VSCode to simply run a JS file you have through NodeJS, even if it quits with a simple output. You can set up one such file to just be a "test project" you use for trying out features before coding them in.Fulmar
I would love some feedback for my extension Node.js REPL that may solve your problem. I really missed that from VS; marketplace.visualstudio.com/…Toreutic
C
35

There is no Immediate Window unlike Visual Studio in VSCode. But you can still execute Javascript in VSCode.

Install the Code Runner Extension - https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner

Open the JavaScript file in VSCode, then use shortcut Ctrl+Alt+N, the code will run and the output will be shown in the Output Window.

Corley answered 14/1, 2017 at 12:37 Comment(2)
Note: If you don't want to save or open an existing file, you can also use Change Language Mode (shortcut Ctrl + K M) on a new untitled tab.Erick
This answer is from 2017. I just tried with latest VCode in Linux, from Terminal, run Node<ENTER>. Node must be installed and on path... see @Diego's answer below, and upvote it if helpful. I upvoted this and Diego's answer.. this answer was helpful before and provides contextual history of the issue, while Diego's answer seems most relevant today. Thanks to both!Aleksandr
P
45

As of (at least) my current version of VS Code (1.5.2), the "Debug Console", while debugging, lets you run arbitrary JavaScript code as you would in the VS Immediate Window. (Similar to as you would for the Chrome Dev Tools Console.)

Peeve answered 13/9, 2016 at 19:44 Comment(2)
Debug Console also executes C# commands in the current debug scope, no need for an extension imo. This should be the accepted answerChaffinch
There are use cases to run code while not debugging.Nachison
C
35

There is no Immediate Window unlike Visual Studio in VSCode. But you can still execute Javascript in VSCode.

Install the Code Runner Extension - https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner

Open the JavaScript file in VSCode, then use shortcut Ctrl+Alt+N, the code will run and the output will be shown in the Output Window.

Corley answered 14/1, 2017 at 12:37 Comment(2)
Note: If you don't want to save or open an existing file, you can also use Change Language Mode (shortcut Ctrl + K M) on a new untitled tab.Erick
This answer is from 2017. I just tried with latest VCode in Linux, from Terminal, run Node<ENTER>. Node must be installed and on path... see @Diego's answer below, and upvote it if helpful. I upvoted this and Diego's answer.. this answer was helpful before and provides contextual history of the issue, while Diego's answer seems most relevant today. Thanks to both!Aleksandr
C
10

I've found this extension that makes a scrathpad for JS, that runs at the same time as you are typing: https://quokkajs.com/

Works on VS Code, Jet Brains, and Atom.

Calculus answered 5/10, 2017 at 0:36 Comment(0)
C
10

If you don't want to start a debugging session or installing an extension, a simple way to have a JavaScript console is to start Node in a Terminal.

  1. View -> Terminal
  2. Start node (without any argument -you'll need node in your PATH)
  3. Now you have a repl with auto-complete and value preview.

It doesn't have the features of the Chrome Console, but I find it good enough for evaluating JS code while I'm working.

Calvaria answered 21/4, 2021 at 14:4 Comment(0)
E
1

Run the command node in the terminal below the editor, this will create a node environment where arbitrary JavaScript can be entered. You must first have node (and apparently npm) installed from nodejs.

Ezaria answered 27/2, 2020 at 18:11 Comment(0)
C
-2

This might do it: https://code.visualstudio.com/Docs/runtimes/nodejs

Node.js is a platform for building fast and scalable server applications using JavaScript. Node.js is the runtime and NPM is the Package Manager for Node.js modules.

To get started, install Node.js for your platform. The Node Package Manager is included in the Node distribution.

Courtenay answered 21/9, 2015 at 16:27 Comment(1)
This comment is not relevant here.Tabb

© 2022 - 2024 — McMap. All rights reserved.