Run JavaScript in Visual Studio Code

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

    The shortcut for the integrated terminal is ctrl + `, then type node <filename>.

    Alternatively you can create a task. This is the only code in my tasks.json:

    {
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "node",
    "isShellCommand": true,
    "args": ["${file}"],
    "showOutput": "always"
    }
    

    From here create a shortcut. This is my keybindings.json:

    // Place your key bindings in this file to overwrite the defaults
    [
    {   "key": "cmd+r",
    "command": "workbench.action.tasks.runTask"
    },
    {   "key": "cmd+e",
    "command": "workbench.action.output.toggleOutput"
    }
    ]
    

    This will open 'run' in the Command Pallete, but you still have to type or select with the mouse the task you want to run, in this case node. The second shortcut toggles the output panel, there's already a shortcut for it but these keys are next to each other and easier to work with.

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

    I would suggest you to use a simple and easy plugin called as Quokka which is very popular these days and helps you debug your code on the go. Quokka.js. One biggest advantage in using this plugin is that you save a lot of time to go on web browser and evaluate your code, with help of this you can see everything happening in VS code, which saves a lot of time.

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

    There are many ways to run javascript in Visual Studio Code.

    If you use Node, then I recommend use the standard debugger in VSC.

    I normally create a dummy file, like test.js where I do external tests.

    In your folder where you have your code, you create a folder called ".vscode" and create a file called "launch.json"

    In this file you paste the following and save. Now you have two options to test your code.

    When you choose "Nodemon Test File" you need to put your code to test in test.js.

    To install nodemon and more info on how to debug with nodemon in VSC I recommend to read this article, which explain in more detail the second part on the launch.json file and how to debug in ExpressJS.

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Nodemon Test File",
                "runtimeExecutable": "nodemon",
                "program": "${workspaceFolder}/test.js",
                "restart": true,
                "console": "integratedTerminal",
                "internalConsoleOptions": "neverOpen"
            },
            {
                "type": "node",
                "request": "attach",
                "name": "Node: Nodemon",
                "processId": "${command:PickProcess}",
                "restart": true,
                "protocol": "inspector",
            },
        ]
    }
    
    0 讨论(0)
  • 2020-11-30 17:31

    Another way would be to open terminal ctrl+` execute node. Now you have a node REPL active. You can now send your file or selected text to terminal. In order to do that open VSCode command pallete (F1 or ctrl+shift+p) and execute >run selected text in active terminal or >run active file in active terminal.

    If you need a clean REPL before executing your code you will have to restart the node REPL. This is done when in the Terminal with the node REPL ctrl+c ctrl+c to exit it and typing node to start new.

    You could probably key bind the command pallete commands to whatever key you wish.

    PS: node should be installed and in your path

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

    This solution intends to run currently open file in node and show output in VSCode.

    I had the same question and found newly introduced tasks useful for this specific use case. It is a little hassle, but here is what I did:

    Create a .vscode directory in the root of you project and create a tasks.json file in it. Add this task definition to the file:

    {
        "version": "0.1.0",
        "command": "node",
        "isShellCommand": true,
        "args": [
            "--harmony"
        ],
    
        "tasks": [
            {
                "taskName": "runFile",
                "suppressTaskName": true,
                "showOutput": "always",
                "problemMatcher": "$jshint",
                "args": ["${file}"]
            }
        ]
    }
    

    Then you can: press F1 > type `run task` > enter > select `runFile` > enter to run your task, but I found it easier to add a custom key binding for opening tasks lists.

    To add the key binding, in VSCode UI menu, go 'Code' > 'Preferences' > 'Keyboard Shortcuts'. Add this to your keyboard shortcuts:

    {
        "key": "cmd+r",
        "command": "workbench.action.tasks.runTask"
    }
    

    Of course you can select whatever you want as key combination.

    UPDATE:

    Assuming you are running the JavaScript code to test it, you could mark your task as a test task by setting its isTestCommand property to true and then you can bind a key to the workbench.action.tasks.test command for a single-action invocation.

    In other words, your tasks.json file would now contain:

    {
        "version": "0.1.0",
        "command": "node",
        "isShellCommand": true,
        "args": [
            "--harmony"
        ],
    
        "tasks": [
            {
                "taskName": "runFile",
                "isTestCommand": true,
                "suppressTaskName": true,
                "showOutput": "always",
                "problemMatcher": "$jshint",
                "args": ["${file}"]
            }
        ]
    }
    

    ...and your keybindings.json file would now contain:

    {
        "key": "cmd+r",
        "command": "workbench.action.tasks.test"
    }
    
    0 讨论(0)
  • 2020-11-30 17:35

    Just install nodemon and run

    nodemon your_file.js
    

    on vs code terminal.

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