1

I am new to JavaScript (coming from c++) and asking myself whether it is possible to compile and debug JavaScript code. I tried to use VisualStudio2012 and SublimeText2 with Node.js, but in both cases, I got a compile error when I tryed to use a library (3djs).

In SublimeText2 the error message is "ReferenceError: d3 is not defined". I got that error trying to compile this d3js example: http://bl.ocks.org/mbostock/3828981

Strangely the example worked perfectly when I opened it in some browsers..

Can anyone tell me what I am doing wrong or whether it is even possible to compile or debug JavaScript code? (Sorry, this seems to be a very beginner question but I did not find the answer searching for some hours...)

Best regards!

Enemenemiste
  • 121
  • 1
  • 6
  • How do you try to "compile" JavaScript? It's a script language, interpreted at runtime. – Aaron Digulla Jan 21 '14 at 10:37
  • That's why I asked whether it is possible to compile it. I know that normally JavaScript is not compiled before running it but then I saw this tutorial and tried it: http://www.youtube.com/watch?v=3qW7cseYJu4 It worked well but if I try to use a library it throws this ReferenceError. – Enemenemiste Jan 21 '14 at 11:20

1 Answers1

1

node.js also doesn't compile the JavaScript (not in the C++ sense; JavaScript interpreters often Just-in-Time compilation). The main difference between node.js and a browser is that node.js doesn't try to hide errors from you.

So the first question is whether you correctly included the library in your project. For this, we need to see the source code that you use to load / import it.

If you're sure that the library has loaded correctly, then you might be triggering a bug in the JavaScript interpreter. Try to install the latest version or the lastest stable version or the version before that.

[EDIT] In HTML, you use a <script> element to include scripts in the context of the page. When using node.js, then you have to use require():

var d3 = require('./d3.js')

That said, D3.js is a client framework. It expects the standard browser environment like a DOM. It will not work inside of node.js. node.js expects modules (= stuff that you can import using require) to have a certain format.

So the simple answer is: You can't "compile" and debug JavaScript code in your IDE. You always need to create a HTML page and open that in a browser. All modern browsers have consoles, plus tools to debug and examine JavaScript, CSS, HTML and the DOM.

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Thank you very much for answering! I think the error is too big to be simply hidden by the browser. If the d3 object could not be found, no svg graphics would be shown, but the example shows correctly in the browser. Plus VisualStudio showed the same error so I thought it cannot be a bug in node.js... But I will try a later version. – Enemenemiste Jan 21 '14 at 12:48
  • Why do you think `d3` should be defined in your code? In the browser, you will have a `script` element that includes the framework. How do you include the framework in your code? – Aaron Digulla Jan 21 '14 at 15:41
  • I made an JavaScript Project in VisualStudio that generated a project-structure automatically with the .html-file integrated. At the beginning I thought that the inclusion of the JavaScript-Framework in the .html-file would work through the Project-Structure, because the html-file is linked in the .jsproj file of my VisualStudio project. I did not find the right way to use frameworks in pure JavaScript text. In each discussion I read in the internet, people said that you can do it only with the .html-file or JSON... – Enemenemiste Jan 22 '14 at 11:37
  • Okay, I'm starting to understand what the misunderstanding is. Please read my edits. – Aaron Digulla Jan 22 '14 at 12:46