0

For example, if I have a .js file with pure JavaScript code like:

function doIt(a, b)
{
    return a + b;
}

Is it possible to, within the VS Code terminal, execute the code -- just like I would in a web browsers developer tools? Is there some special terminal extension I can get that will let me do this? Hopefully something that doesn't need me to manually run/compile every time I save the file?

I am not writing Node.js code so I don't know (think) the Node.js extensions will work.

** Update **

I don't want to just execute the script and exit/terminate. I want to execute it but keep it in memory, in the terminal window, so I can interactivly interact with it. For example, run doIt with various parameters to see the result/output -- just like I would from the dev console in Chrome.

IMTheNachoMan
  • 5,343
  • 5
  • 40
  • 89
  • 2
    Does this answer your question? [Run JavaScript in Visual Studio Code](https://stackoverflow.com/questions/31778413/run-javascript-in-visual-studio-code) – JBallin Nov 03 '20 at 04:07
  • Related: [js vs node](https://stackoverflow.com/a/38424777/4722345) – JBallin Nov 03 '20 at 04:08
  • That will execute the code in the script and exit/stop. I want to execute the code but keep it in memory so I can interact with it in terminal. For example, call `doIt` with various parameters to see the output. Just like I would from the console in Chrome developer tool pane. – IMTheNachoMan Nov 03 '20 at 04:09
  • @JBallin I am not sure how it is related. My question has nothing to do with Node. – IMTheNachoMan Nov 03 '20 at 04:10
  • To execute js in terminal you just run “node PATH_TO_FILE”. Install node first. – JBallin Nov 03 '20 at 04:10
  • @JBallin I updated my question. I don't want to just execute it. I want to execute it but keep it in memory so I can continue to interact with it. – IMTheNachoMan Nov 03 '20 at 04:11
  • Are you just looking for a [repl](https://nodejs.org/en/knowledge/REPL/how-to-use-nodejs-repl/)? – JBallin Nov 03 '20 at 04:12
  • @IMTheNachoMan - Running JavaScript in a terminal, whether that is from within VSCode or in a Windows cmd prompt or Linux terminal, you will be using NodeJS to run your code. This **is not the same as running it in a browser**. At minimum you'll need a simply HTML page with a script tag. Then, you can include the Chrome Dev extension to open your browser load your content. That gives you the ability you seek. – Randy Casburn Nov 03 '20 at 04:13
  • 1
    @JBallin - you should retract your close vote. That is not a duplicate question. The OP does not want to run this code using NodeJS. – Randy Casburn Nov 03 '20 at 04:15
  • @RandyCasburn So VS Code doesn't have the ability to interpret/compile/run JS code and keep it in memory like a browser would so I can continue to interact with it? Got it. Thanks! If you post that as an answer I can mark it as such. – IMTheNachoMan Nov 03 '20 at 04:17
  • I retracted, just because of the new requirement that the runner shouldn't exit. @RandyCasburn I think that OP does want to use node, but doesn't realize it yet. That's why I linked JS vs. Node. – JBallin Nov 03 '20 at 04:18
  • My comments are based on this statement you made in your question: "_just like I would in a web browsers developer tools_" - no, you cannot do that in the VSCode terminal. But I think @JBallin may be correct in that you just don't realize what you are asking. So, finally, I will say, the correct way to run code and interact with in **in any IDE** is to learn how to use the debugger, set break points, fiddle with code _while it is running_ in the proper way. You'll be a better programmer for it. – Randy Casburn Nov 03 '20 at 04:22
  • Read this: https://code.visualstudio.com/docs/editor/debugging#_start-debugging – Randy Casburn Nov 03 '20 at 04:24
  • My code doesn't inherently run by itself. I'm working on a `class`/library that would be called/executed elsewhere. I can set break points but I don't really need to right now. I just need a way to be able to write a function and then call it from the terminal. I'll see if Node can do what I'm after. – IMTheNachoMan Nov 03 '20 at 13:09

2 Answers2

0

Node is the most common way to run JavaScript locally. The browser console is essentially a REPL (Read Eval Print Loop), which Node has built in!

Install Node and open terminal:

$ echo "function doIt(a, b) { return a + b; }" > script.js
$ node
Welcome to Node.js v12.19.0.
Type ".help" for more information.
> .load script.js
function doIt(a, b) { return a + b; }
 
undefined
> doIt(1,2)
3
JBallin
  • 8,481
  • 4
  • 46
  • 51
  • Humm. So I guess I need to find a way to edit the code in VS Code and then do something such that `node` loads it in terminal and keeps the `node` session open so I can interact with it. – IMTheNachoMan Nov 03 '20 at 13:07
  • Yes you edit the code in VS Code (in this case `script.js`) and then you can interact with it in terminal when you `.load` it - the session _will_ stay open. If you want to edit the file you can just `.exit` and open a new session - is that the thing that's missing (automatically refreshing the session on save)? – JBallin Nov 03 '20 at 16:43
  • Yes. The automatic refresh. I think. But I’m still researching. – IMTheNachoMan Nov 03 '20 at 23:18
  • Did you try out my suggestion? – JBallin Nov 04 '20 at 00:35
  • I am working on it. Need to get Node installed. Work computer so I have to get all sorts of approvals. – IMTheNachoMan Nov 05 '20 at 15:02
0

This can't be used with only VSC, but can be done in Node.JS. You can simply include the readline module built into node and it will take in input from the user. Here's an example:

        const readline = require('readline')
        const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
        terminal: true,
        });
        console.log('Enter your height (centimeters)')
        rl.on('line', (input) => {
        console.log(`Your height is ${input}cm`);
        });```