Using the JavaScript console in Visual Studio Code

本文介绍如何在VSCode中创建一个JavaScript/Node任务,以实现在控制台显示当前活动文件的内容,类似于Sublime Text中的JavaScript控制台功能。通过简单的配置,可以方便地在编辑器中运行JavaScript代码。

Some time ago I published an article about the JavaScript console in Sublime Text. It’s turned out to be one of the most popular articles on this website. As I recently changed my code editor from Sublime Text to VSCode I found a solution to replicate this functionality.

Similar to Build Systems in Sublime Text, Visual Studio Code comes with Tasks that allows us to pass a file to an external program without manually switching between the code editor and the Terminal. This is essentially all that we need to do — pass a currently active file to a JavaScript interpreter (Node in this case - so make sure it is installed on your computer).

Create JavaScript / Node task in VSCode

A VSCode Task is a set of instructions in a JSON file that resides in our projects file. Unfortunately at this moment it isn’t possible to create globally available tasks — they need to be added per project. The good news is that the development of VSCode is rapid so we may see global tasks appear very soon as I’m not the only one who wants this feature. To create a Task hit cmd + shift + p on Mac, ctrl + shift + p on Windows / Linux or simply F1 on any platform to show the Command Palette, type “Tasks: Configure Task” then “Create tasks.json file from template” and choose “Others” from the list. Time to configure the task.

{
  "version": "2.0.0",
  "tasks": [
    {
      "taskName": "Show in console",
      "type": "shell",
      "osx": {
        "command": "/usr/local/bin/node ${file}"
      },
      "linux": {
        "command": "/usr/bin/node ${file}"
      },
      "windows": {
        "command": "C:\\Program Files\\nodejs\\node.exe ${file}"
      },
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

OK, so what is going on here? When we run a task called “Show in console” this will run a shell command that takes our currently opened file as an argument preceded by the path to our node executable. To make things even easier the group.kindproperty lets us run this task via shift + cmd + b keyboard shortcut.

In most cases the “node” in command property does the job instead of passing a full path. I found this method more reliable though. The path can vary depending on operating system, version and installation method. Update command according to the output of which node. On Windows the equivalent command is where node.

Find path to node executable in Terminal

Time for the fun bit! Create an amazing script and enjoy the instant output in your code editor by pressing shift + cmd + b or by running the “Show in console” task from the Command Palette. Hopefully this helped you out. Stay curious and build amazing things!

Find path to node executable in Terminal

### Visual Studio 的收费模式与版本介绍 Visual Studio 是由微软开发的一款功能强大的集成开发环境(IDE),广泛用于 C#、.NET 等多种编程语言的开发。关于其收费模式,微软在近年来进行了重大调整,使得更多开发者可以免费使用这一工具。 目前,Visual Studio 提供了多个版本,包括 **Community(社区版)**、**Professional(专业版)** 和 **Enterprise(企业版)**。其中: - **Visual Studio Community** 是完全免费的版本,适用于个人开发者、学生、开源项目以及小型团队(最多 5 人)。它几乎包含了 Professional 版本的所有功能,支持 Windows 应用、Web 和跨平台移动开发等场景[^2]。 - **Visual Studio Professional** 是面向专业开发者的付费版本,通常用于企业内部的软件开发。该版本提供更高级的支持服务和集成工具,适合需要企业级技术支持的开发人员使用[^1]。 - **Visual Studio Enterprise** 是面向大型企业和复杂项目的最高级别版本,除了包含 Professional 的所有功能外,还增加了高级测试工具、架构分析、性能优化等功能。该版本通常以订阅方式提供,用户需定期支付费用以维持使用权限[^3]。 此外,微软还推出了 **Visual Studio Code**,这是一款轻量级、开源、跨平台的代码编辑器,支持 C#、JavaScript、Python 等多种语言。虽然它不是完整的 IDE,但通过插件系统可实现强大的开发体验,且完全免费[^4]。 综上所述,Visual Studio 并非完全收费,而是采用“免费+付费”结合的模式。社区版对大多数开发者而言已经足够强大,而专业版和企业版则针对特定需求提供了更多高级功能。 --- ```csharp // 示例:使用 Visual Studio 编写一个简单的 C# 控制台应用程序 using System; class Program { static void Main() { Console.WriteLine("Hello, Visual Studio!"); } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值