如何在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
// quokkacomment 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:
To save time, create a tiny wrapper script (like> const myUtils = require('./utils.js'); > myUtils.calculateTotal(10, 20);run-function.js) that imports your module and runs the function with predefined args, then execute it withnode run-function.js. ts-nodefor TypeScript: Skip compiling TS files first—usets-nodeto run your TypeScript function directly in the terminal. You can either run a wrapper script or use thets-nodeREPL just like the regular Node one.- Custom npm Scripts: Add a script to your
package.jsonto call your function with a single command. For example:
Then just run{ "scripts": { "greet-user": "node ./scripts/greet.js" } }npm run greet-userwhenever 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:
- Create a simple script that imports your function and calls it.
- Set a breakpoint inside the function.
- 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
相关产品推荐
相关产品推荐

