I think what you mean is basically running the script in the console.
Visual Studio Code has very little to do with this - though it does offer a terminal window(docs) for your convenience, I personally always use the linux terminal(docs). But that's up to you.
So, to run javascript on the machine rather than the browser you need to install node.
- step 1: install node here are the docs for that.
Now all you need to do is write your script and then run it.
- step 2: write script
let name = prompt("what is your name");
prompt() (docs)would get you the name prompt and store the input as the script is running in the terminal. so write your script let's say something like:
nameProgram.js;
console.log("Welcome to this awesome software that can tell you your own name!");
let name = prompt("What is your name?");
console.log(`Your name is ${name}! see how cool!`);
- step 3: run program
open the terminal whichever way you prefer, go to the directory of the script and type in the following command:
node nameProgram.js
That's it, your script will run.
Answer from aviya.developer on Stack OverflowVideos
Hey all,
I'm a beginner programmer and am new to VS Code. I know that the Output panel won't allow for alerts or prompts, but is there a way (extension maybe?) to test that in-program? Or do I need to run it in a browser or something?
Thanks!
Are you writing JavaScript? If so, then yes, you need to run that in a browser basically.
Alert and prompt are both accessible through window, even though we usually omit the window.alert() and just access it though the global scope with alert(). So you need to run it in a browser. Just a helpful tip that makes working in the browser more rewarding if you aren't already doing it, install an extension called Live Server, then just right click on your html file with the inline or linked js code to run it with Live Server. It's got some nice added features like live reloading on save, etc.
I'm programming a card game to refresh my programming skills but I'm kind of going in blind. I have the code runner extension, which has been great so far, but when I try to prompt a user for input or display a window with text in it, I get "ReferenceError: prompt is not defined."
Based on my my reading it seems like this wouldn't happen if I ran it in a browser, but frankly I don't know how to do that lol.
Is there a way to run this sort of js code in VSCode? If not, how do I run things in my browser (I use firefox)?
» npm install @vscode/prompt-tsx
Hi,
I am trying to run the following Javascript code on Visual Studio Code:
try{
const prompt = require('prompt-sync')({sigint: true});
}catch(err) {
console.log(err);
}
let person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML = "Hello " + person + "! How are you today?";
}I am getting the following error:
Error: Cannot find module 'prompt-sync'
Require stack:
d:\Javascript\programs\posNegZero.js at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15) at Module._load (node:internal/modules/cjs/loader:985:27) at Module.require (node:internal/modules/cjs/loader:1235:19) at require (node:internal/modules/helpers:176:18) at Object.<anonymous> (d:\Javascript\programs\posNegZero.js:3:16) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) { code: 'MODULE_NOT_FOUND', requireStack: [ 'd:\Javascript\programs\posNegZero.js' ] } d:\Javascript\programs\posNegZero.js:7 let person = prompt("Please enter your name", "Harry Potter"); ^
ReferenceError: prompt is not defined at Object.<anonymous> (d:\Javascript\programs\posNegZero.js:7:14) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) at node:internal/main/run_main_module:28:49
Node.js v20.11.1
[Done] exited with code=1 in 0.148 secondsSomebody, please guide me.
Zulfi
I am surprised this has not been mentioned yet:
Simply open the .js file in question in VS Code, switch to the 'Debug Console' tab, hit the debug button in the left nav bar, and click the run icon (play button)!
Requires nodejs to be installed!
This is the quickest way for you in my opinion;
- Open integrated terminal on visual studio code (
View > Integrated Terminal) - type
'node filename.js' - press enter
note: node setup required. (if you have a homebrew just type 'brew install node' on terminal)
note 2: homebrew and node highly recommended if you don't have already.
have a nice day.