Using the JavaScript console in Visual Studio Code

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mastering Visual Studio 2019: Become proficient in .NET Framework and .NET Core by:using advanced coding techniques in Visual Studio, 2nd Edition eBook Details: Paperback: 374 pages Publisher: WOW! eBook (August 9, 2019) Language: English ISBN-10: 1789530091 ISBN-13: 978-1789530094 eBook Description: Mastering Visual Studio 2019, 2nd Edition: Explore the new features in Visual Studio 2019 and build apps using WPF, .NET Core, TypeScript, and Azure Visual Studio 2019 is the latest IDE from Microsoft for developers targeting Windows and other platforms to build stunning desktop, web and mobile applications. In this book, you’ll learn how to effectively use this IDE to build, debug, and test your applications to enhance productivity by simplifying your most common tasks and allowing more time to focus on other aspects of your project. Starting with an overview of the new features in Visual Studio 2019, you’ll understand what’s improved since its previous edition. This Visual Studio book will help you build applications for Windows using Windows Presentation Foundation (WPF), which provides a unified programming model for developing dynamic, rich, data-driven desktop applications. You’ll then learn about Microsoft’s implementation of cloud computing with Azure, followed by building applications using .NET Core and TypeScript. This book will also help you explore NuGet, the package manager for the Microsoft development platform, that enables you to create, publish, and consume packages from a central repository by managing a reference list of packages. Finally, you’ll delve into the debugging and live unit testing techniques that are built into the IDE. Increase your productivity with Visual Studio 2019’s new features Understand how the installation wizard works and create an offline installation package Build stunning applications using WPF, .NET Core, and TypeScript Explore NuGet packages in depth Accelerate cloud development with Azure Debug and test your applications efficiently Get to grips with integrating Visual Studio with Git repositories By the end of the Mastering Visual Studio 2019, Second Edition book, you’ll have become a more efficient Visual Studio 2019 user, and be able to use it for developing, testing, debugging and deploying your applications.
Mastering Visual Studio 2017 by Kunal Chowdhury English | 27 July 2017 | ISBN: 1787281906 | ASIN: B06X3QS36H | 466 Pages | AZW3 | 20.5 MB Key Features Focus on coding with the new, improved, and powerful tools of VS 2017 Master improved debugging and unit testing support capabilities Accelerate cloud development with the built-in Azure tools Book Description Visual Studio 2017 is the all-new IDE released by Microsoft for developers, targeting Microsoft and other platforms to build stunning Windows and web apps. Learning how to effectively use this technology can enhance your productivity while simplifying your most common tasks, allowing you more time to focus on your project. With this book, you will learn not only what VS2017 offers, but also what it takes to put it to work for your projects. Visual Studio 2017 is packed with improvements that increase productivity, and this book will get you started with the new features introduced in Visual Studio 2017 IDE and C# 7.0. Next, you will learn to use XAML tools to build classic WPF apps, and UWP tools to build apps targeting Windows 10. Later, you will learn about .NET Core and then explore NuGet, the package manager for the Microsoft development platform. Then, you will familiarize yourself with the debugging and live unit testing techniques that comes with the IDE. Finally, you'll adapt Microsoft's implementation of cloud computing with Azure, and the Visual Studio integration with Source Control repositories. What you will learn Learn what's new in the Visual Studio 2017 IDE, C# 7.0, and how it will help developers to improve their productivity Learn the workloads and components of the new installation wizard and how to use the online and offline installer Build stunning Windows apps using Windows Presentation Foundation (WPF) and Universal Windows Platform (UWP) tools Get familiar with .NET Core and learn how to build apps targeting this new framework Explore everything about NuGet packages Debug and test

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值