5

I'm wondering (actually I know there definitely is such a way to run js in command line, because when I watch google I/0 presentations, they use a command like d8, maybe it's part of V8 engine) is there any way to run/debug my javascripts in command line? such as d8 myJsfile.js --prof etc.?

Does anybody have any idea about this?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Lihong Qiu
  • 51
  • 1
  • 1
  • 6
  • The first thing I found when I googled for `d8 javascript` was this: http://www.sandeepdatta.com/2011/10/using-v8-javascript-shell-d8.html. Is this what you wanted to know? – Felix Kling Aug 23 '12 at 13:28

2 Answers2

5

Yes, D8 is a command line tool that allows you to run and debug Javascript scripts. It comes with V8.

Getting D8

Getting V8 on your machine is not hard. Follow these instructions: https://developers.google.com/v8/build

(The part with GYP looks a little messy but I got through it easily on a Linux box.)

With V8 installed, you now have d8, which allows you to run javascript from the command line.

$ d8 myscript.js

Using D8

Look at d8 --help for the profiling options. These will be very similar to the options you can use when you want to use profiling on Chrome.

If you want to debug start d8 with --debugger. The script will break when it encounters the statement debugger; in your code, or when you type debugger; in the d8 shell. (You get the shell by starting d8 with --shell.) You know you're debugging when the shell prompt goes from d8> to dbg>. To get help with the debugger type help at the dbg> prompt.

The d8 debugger is an old school command line debugger that will be painful to use on large complex scripts if you are used to GUI debuggers.

Using a GUI debugger with D8

If you want to use a GUI debugger you can use the Chrome DevTools for Java, which includes an Eclipse debugger:

https://code.google.com/p/chromedevtools/

To get it working:

  • install Eclipse
  • install ChromeDevTools plugin (I got help from this video: http://www.youtube.com/watch?v=_uzSw_fb7NQ)
  • create a new debug configuration of type "Standalone V8 VM", setting host:localhost and port:5858 for the connection params.

To debug code running in D8:

  • start D8 with your script and with --debugger_agent, and --debugger_port if necessary based on your config; D8's default port is 5858.
  • in Eclipse launch the debug configuration you created above. The scripts will show up in the interface
  • set breakpoints in the Eclipse UI and have at it. (You do not need debugger; statements in your code.)

Note! D8 only listens for the debugger on the localhost. So if you are on a separate machine, then this won't work. There is no flag that will make it listen on another IP. See: https://code.google.com/p/v8/issues/detail?id=1855

Gotchas of running Javascript in D8

Note that the Javascript environment in D8 is not the same as the one in a browser window. There are a number of functions you can't use:

  • setTimeout and its cousins
  • XMLHttpRequest
  • and quite a few more. Check the ECMA specs to see what you actually CAN use.

Remember there is no window.* or document.* or DOM!

teleclimber
  • 318
  • 3
  • 7
2

Node.JS is really solid for this.

You won't be able to test browser things, such as window.location and what not, but for running straight scripts, it is helpful.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Here's a related SO post on using Node: http://stackoverflow.com/questions/6737824/how-to-run-a-hello-js-file-in-node-js-on-windows You will probably also want to know [how to use the debugger in Node](https://nodejs.org/api/debugger.html). – sameers Jun 26 '15 at 16:50