Enemenemiste
Enemenemiste

Reputation: 121

Compile Error in JavaScript: "libraryVar is not defined"

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!

Upvotes: 1

Views: 605

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328556

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.

Upvotes: 1

Related Questions