Run JavaScript in Visual Studio Code

后端 未结 18 1002
北恋
北恋 2020-11-30 17:04

Is there a way to execute JavaScript and display the results using Visual Studio Code?

For example, a script file containing:

consol         


        
相关标签:
18条回答
  • 2020-11-30 17:36

    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.

    0 讨论(0)
  • 2020-11-30 17:37

    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!

    0 讨论(0)
  • 2020-11-30 17:38

    Another option is to use the developer tools console within Visual Studio Code. Simply select "Toggle Developer Tools" from the help menu and then select the "Console" tab in the developer tools that pop up. From there you have the same dev tools REPL that you get in Chrome.

    0 讨论(0)
  • 2020-11-30 17:40

    This may now be the easiest, as of v1.32:

    {
        "key": "ctrl+shift+t",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "node '${file}'\u000D" }
      }
    

    Use your own keybinding.

    See Release notes: sendSequence and variables.

    With vscode v1.32 you can sendSequence to the terminal using variables like ${file}, which is the current file. If you want some other path there, replace ${file} with your pathname in the keybinding above.

    The \u000D is a return so it will run immediately.

    I added 's around the ${file} variable in case your file path has spaces in it, like c:Users\Some Directory\fileToRun

    0 讨论(0)
  • 2020-11-30 17:41

    I faced this exact problem, when i first start to use VS Code with extension Code Runner

    The things you need to do is set the node.js path in User Settings

    You need to set the Path as you Install it in your Windows Machine.

    For mine It was \"C:\\Program Files\\nodejs\\node.exe\"

    As I have a Space in my File Directory Name

    See this Image below. I failed to run the code at first cause i made a mistake in the Path Name

    Hope this will help you.

    And ofcourse, Your Question helped me, as i was also come here to get a help to run JS in my VS CODE

    0 讨论(0)
  • 2020-11-30 17:43

    I used Node Exec, no config needed, builds the file that you are currently ending or what ever has been selected and outputs inside of VSCode.

    https://marketplace.visualstudio.com/items?itemName=miramac.vscode-exec-node

    With a bit of config you can add Babel to do some on the fly transpiling too.

    0 讨论(0)
提交回复
热议问题