简体   繁体   中英

How can I debug stand alone JavaScript files in Visual Studio Community 2015?

I am practicing JavaScript and have some stand-alone JavaScript source code that doesn't have to run inside a Web page.

I opened a New -> File in Visual Studio Community 2015 and pasted my JavaScript code in there and saved it to a file on my disk.

I set breakpoints where I wanted to.

Now, I see the Attach button on the top that looks like the Run button but I don't know process what to attach this code to. How do I run this JavaScript code in debug mode?

This question pertains to using Visual Studio as an editing and debugging environment for JavaScript stand-alone sources.

You certainly can do this with Visual Studio. You have two options:

  1. Use the EXE project system to launch and debug your JavaScript file with cscript.exe ( Makes full use of the IDE, but requires some setup )

  2. Use the Just-In-Time debugger to attach Visual Studio to cscript.exe, launching Visual Studio, if necessary. ( Gets you debugging with no setup, but does not really make use of the IDE )

Option 1: Using the EXE Project System

You can create a project from an EXE so that you can launch the EXE with F5, F10, or F11. From the File menu, choose Open Project and choose %windir%\\System32\\cscript.exe and click Ignore on the UAC prompt. You should now have a project named cscript.

Visual Studio解决方案资源管理器的屏幕截图,显示了为cscript.exe创建的EXE项目

Now you can either create or open a .js file for editing and setting breakpoints. To debug your .js file, setup the project properties.

Right-click on the project and select Properties to open the project properties page.

Visual Studio EXE项目属性页的屏幕截图

Fill in the following parameters:

  • Arguments : //D file
  • Debugger Type : Script
  • Working Directory : Path to the folder containing your .js file

The remaining parameters' default values should be good enough. At this point, you are ready to hit F5 and debug your .js file.

Option 2: Using the JIT Debugger

You can run your .js file with the following command in the command prompt:

cscript //X <your .js file> .

This should automatically bring up the Just-In-Time debugging dialog from which you can choose which Visual Studio installation/instance to launch and attach with (shown below).

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM