0

I'm trying to include some libraries in from some server scripts that I'm running from the Mac Terminal (command line). This will not be run in a browser - I'm using Parse Cloud Code for my iOS app's backend. Any ideas why the following code would not work, whereas it does work in Google Chrome?

document.write('<script src="http://code.jquery.com/jquery-1.8.3.min.js" type="text/javascript"></script>');
document.write('<script src="https://raw.github.com/carlo/jquery-base64/master/ jquery.base64.min.js" type="text/javascript"></script>');
document.write('<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha1.js" type="text/javascript"></script>');

Running this from the terminal gives the error: {"code":141,"error":"Uncaught ReferenceError: document is not defined"}.

nickd717
  • 181
  • 2
  • 12

1 Answers1

0

there's usually no document in server side Javascript. This is because there's no DOM being created, and that's why you get that error (Uncaught ReferenceError: document is not defined).

Also, usually, script tags won't be use to load in javascript file (there's no DOM).

Node.js use a module interface using require to request modules (this also mean module won't be used in the global space; e.g. window in the browser, process in Node.js). And unfortunately, a lot of client side javascript library won't work on the server side (that's the case of jQuery because it is used to manipulate DOM which is not present server side).

In node however, there's cheerio who basically implement jQuery syntax on the server to generate DOM string. But in your case, I'd rather looks for specific library working on your environnement.

Enaway, after you clarified your question a little bit. I think you have an answer to why you get the document not defined error (you're not working in a browser, and as so you have no DOM). From there, you'll really need to read Cloud Code Guide documentation and see how their environment is working; but definitly, it's not because it says it uses javascript that you can transpose how you work on the browser to the server.

I wish you good luck.

Simon Boudrias
  • 42,953
  • 16
  • 99
  • 134