Run JavaScript in Visual Studio Code

后端 未结 18 1003
北恋
北恋 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:46

    There is no need to set the environment for running the code on javascript,python,etc in visual studio code what you have to do is just install the Code Runner Extension and then just select the part of the code you want to run and hit the run button present on the upper right corner.

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

    Follow these steps in VS code.[performed in windows os]

    1. Create new file

    2. Write javascript codes in it

    3. Save file as filename.js

    4. Go to Debugging menu

    5. Click on Start debugging

    6. or simply press F5

    screenshot of starting debugging

    screenshot of output of js code in terminal

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

    There is a much easier way to run JavaScript, no configuration needed:

    1. Install the Code Runner Extension
    2. Open the JavaScript code file in Text Editor, then use shortcut Control+Alt+N (or ⌃ Control+⌥ Option+N on macOS), or press F1 and then select/type Run Code, the code will run and the output will be shown in the Output Window.

    Besides, you could select part of the JavaScript code and run the code snippet. The extension also works with unsaved files, so you can just create a file, change it to Javascript and write code fast (for when you just need to try something quick). Very convenient!

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

    Well, to simply run the code and show the output on the console you can create a task and execute it, pretty much as @canerbalci mentions.

    The downside of this is that you will only get the output and thats it.

    What I really like to do is to be able to debug the code, lets say Im trying to solve a small algorithm or trying a new ES6 feature, and I run it and there is something fishy with it, I can debug it inside VSC.

    So, instead of creating a task for it, I modified the .vscode/launch.json file in this directory as follows:

    {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "node",
            "request": "launch",
            "program": "${file}",
            "stopOnEntry": true,
            "args": [],
            "cwd": "${fileDirname}",
            "runtimeExecutable": null,
            "runtimeArgs": [
                "--nolazy"
            ],
            "env": {
                "NODE_ENV": "development"
            },
            "externalConsole": false,
            "sourceMaps": false,
            "outDir": null
        }
    ]
    }
    

    What this does is that it will launch whichever file you are currently on, within the debugger of VSC. Its set to stop on start.

    To launch it, press F5 key, in the file you want to debug.

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

    For windows: just change file association of .js file to node.exe

    1) Take VSCode
    2) Right click on the file in left pane
    3) Click "Reveal in explorer" from context menu
    4) Right click on the file -> Select "Open with" -> Select "Choose another program"
    5) Check box "Always use this app to open .js file"
    6) Click "More apps" -> "Look for another app in PC"
    7) Navigate to node.js installation directory.(Default C:\Program Files\nodejs\node.exe"
    8) Click "Open" and you can just see cmd flashing
    9) Restart vscode and open the file -> Terminal Menu -> "Run active file".
    
    0 讨论(0)
  • 2020-11-30 17:58

    It's very simple, when you create a new file in VS Code and run it, if you already don't have a configuration file it creates one for you, the only thing you need to setup is the "program" value, and set it to the path of your main JS file, looks like this:

    {
        "version": "0.1.0",
        // List of configurations. Add new configurations or edit existing ones.  
        // ONLY "node" and "mono" are supported, change "type" to switch.
        // ABSOLUTE paths are required for no folder workspaces.
        "configurations": [
            {
                // Name of configuration; appears in the launch configuration drop down menu.
                "name": "Launch",
                // Type of configuration. Possible values: "node", "mono".
                "type": "node",
                // ABSOLUTE path to the program.
                "program": "C:\\test.js", //HERE YOU PLACE THE MAIN JS FILE
                // Automatically stop program after launch.
                "stopOnEntry": false,
                // Command line arguments passed to the program.
                "args": [],
                // ABSOLUTE path to the working directory of the program being debugged. Default is the directory of the program.
                "cwd": "",
                // ABSOLUTE path to the runtime executable to be used. Default is the runtime executable on the PATH.
                "runtimeExecutable": null,
                // Optional arguments passed to the runtime executable.
                "runtimeArgs": [],
                // Environment variables passed to the program.
                "env": { },
                // Use JavaScript source maps (if they exist).
                "sourceMaps": false,
                // If JavaScript source maps are enabled, the generated code is expected in this directory.
                "outDir": null
            }, 
            {
                "name": "Attach",
                "type": "node",
                // TCP/IP address. Default is "localhost".
                "address": "localhost",
                // Port to attach to.
                "port": 5858,
                "sourceMaps": false
            }
        ]
    }
    
    0 讨论(0)
提交回复
热议问题