You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code中通过点击或终端命令便捷调用函数

Absolutely, there are several handy tools and tricks to call functions directly without going through Jest tests—whether you want a one-click solution in your editor or a quick terminal command. Here are my top recommendations:

1. VS Code Plugins for One-Click Execution

  • Code Runner: This is a go-to for many developers. Just right-click any function in your code and select "Run Code" (or hit the shortcut Ctrl+Alt+N). It works with Node.js out of the box, so it’ll execute your function instantly. You can even adjust settings to pass arguments if your function needs them.
  • Quokka.js: Perfect for live, inline execution. Add a // quokka comment above your function, and it’ll run it in real-time, showing results directly in your editor. It’s great for quick testing without switching to a terminal.
  • JavaScript and TypeScript Nightly: If you’re working with TypeScript, this extension adds a "Run Function" option when you right-click a function definition. It integrates smoothly with VS Code’s existing tooling, no extra setup needed.

2. Terminal-First Solutions

  • Node.js REPL: Fire up the Node REPL with node, then import your module and call the function directly. Example:
    > const myUtils = require('./utils.js');
    > myUtils.calculateTotal(10, 20);
    
    To save time, create a tiny wrapper script (like run-function.js) that imports your module and runs the function with predefined args, then execute it with node run-function.js.
  • ts-node for TypeScript: Skip compiling TS files first—use ts-node to run your TypeScript function directly in the terminal. You can either run a wrapper script or use the ts-node REPL just like the regular Node one.
  • Custom npm Scripts: Add a script to your package.json to call your function with a single command. For example:
    {
      "scripts": {
        "greet-user": "node ./scripts/greet.js"
      }
    }
    
    Then just run npm run greet-user whenever you need to call that function.

3. Debugger Trick for Inspected Execution

If you need to check variables while running the function, use VS Code’s debugger:

  1. Create a simple script that imports your function and calls it.
  2. Set a breakpoint inside the function.
  3. Use the "Run and Debug" panel to launch the script—you’ll be able to step through the code and inspect values as it runs.

These methods are way quicker than setting up a Jest test for every function call, especially for quick sanity checks or iterative testing.

内容的提问来源于stack exchange,提问作者imv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 01:47:40