0

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.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • How are you hosting the JavaScript? Visual Studio requires a process to attach too, typically w3wp.exe. But that requires an IIS instance, etc. If you want to debug Javascript (only) why don't you just use the native debug tools in all modern browsers? They're typically much better than Visual Studio anyway – Liam Aug 02 '16 at 09:46
  • Possible duplicate of [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Aug 02 '16 at 09:46
  • It is a stand-alone script to be executed by the Windows shell. I can run it on the command line but that way I get no debugging capabilities. I tried using Scratchpad (http://stackoverflow.com/q/38716746/303685) but I can't set break-points in there unless I write `debugger` statements manually in the code. I looked up one thread and it said Visual Studio had support for running stand-alone JavaScript in the debugger without attaching to any process. I am desperately looking for a hassle-free JavaScript debugging experience. There seems not to be any. – Water Cooler v2 Aug 02 '16 at 09:50
  • You can't run JavaScript "stand alone". It is not a native windows language. JavaScript is always ran by third party engines, i.e. browsers, Node.js, etc. You can't run JavaScript in the "windows shell". What do you mean by windows shell? Cmd, Powershell? What you're asking for makes no sense. – Liam Aug 02 '16 at 10:10
  • You *can* run JavaScript stand-alone in the Windows Scripting Host (WSH) shell. – Water Cooler v2 Aug 02 '16 at 10:13
  • That's not the windows shell...I don't think anyone has used the WSH for a very, very long time. I'm surprised window still has it installed. What are you trying to do here? This all sounds very odd. If you want to script windows you should really be using PowerShell. If you want to use JavaScript in a windows environment you should really be using Node.js. If you just want to experiment with Javascript you should really be doing this in a browser (in something like [jsfiddle](https://jsfiddle.net/) maybe) – Liam Aug 02 '16 at 10:30
  • I try all those things off and on. I just wanted to know if there is one IDE that can make my life easy. – Water Cooler v2 Aug 02 '16 at 10:31
  • You can debug javascript in Visual Studio but typically this is done via a browser. Build a web project not a windows project – Liam Aug 02 '16 at 10:34
  • 1
    I know that. That's what I ended up doing for now. I've done that many times. I just wanted to know if I could run a stand-alone JavaScript file in Visual Studio. It appears that I cannot. – Water Cooler v2 Aug 02 '16 at 10:37

1 Answers1

2

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.

Screen capture of Visual Studio Solution Explorer, showing the EXE project created for cscript.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.

Screen capture of Visual Studio EXE project properties page

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).

enter image description here

John
  • 1,379
  • 8
  • 16