0

need some help please.

How to run nodejs code that just runs over some JSON data and prints it out, in the browser? I know in the command line, we just enter node nameOfFile.js , but how can I run this in the browser, so that I could add CSS to it to play around with it etc.?

Thanks

Cjvan Bukj
  • 13
  • 1
  • what did i just read! – Shaunak Aug 04 '15 at 23:52
  • @Shaunak A question, presumably. – Brad Aug 04 '15 at 23:54
  • If you would like to use node properly, in this case, you could serve up your JSON from an endpoint built in Node. Node is meant to be an HTTP server in most common use cases, so use it for that purpose. – Josh Aug 05 '15 at 19:18
  • [This](http://caolanmcmahon.com/posts/writing_for_node_and_the_browser/) is sort of an old article now, but, should give you some clarity on how you could use node for client and server side code (without browserify). Not saying you shouldn't use Browserify, just saying you should know what it's doing: http://caolanmcmahon.com/posts/writing_for_node_and_the_browser/ – netpoetica Aug 05 '15 at 19:31

2 Answers2

1

Node is simply an engine that uses Google's V8 to interpret JavaScript, but adds a few features.

It has a few additions to it. But if your script isn't too localized to NodeJS, you should be able to run it just fine in the browser. Otherwise you'll have to make some polyfills for NodeJS-specific code (like Buffer for instance).

  • This doesn't explain *how* to do this, which is the core of the question. – Brad Aug 04 '15 at 23:53
  • 1
    What am I supposed to tell him? Copy and paste it? I told him to make polyfills if needed. There isn't much more to say. –  Aug 04 '15 at 23:54
  • Well, if he wants to know that, [this is a duplicate](http://stackoverflow.com/questions/11498068/how-to-use-external-js-files). Otherwise, I'm telling him what he should do to make the NodeJS work in the browser. –  Aug 06 '15 at 01:46
-1

In-browser, you need to reference your JavaScript from an HTML page. This is done with a script tag:

<script src="path/to/your/script.js"></script>

This tag is usually placed within <head> tags, but can be elsewhere as-needed.

From there, you can set HTML in the document:

document.body.innerHTML = '<h1>Hello, world</h1>';
Brad
  • 159,648
  • 54
  • 349
  • 530
  • Sorry, I am a complete newb, I thought node is run from server side? When i run my code in the console it says ReferenceError: require is not defined – Cjvan Bukj Aug 05 '15 at 00:05
  • Node.js is server-side. You asked how you could run that script client-side. – Brad Aug 05 '15 at 00:06
  • Sorry I might be asking the wrong thing here, I just included that js file into my html file using but when I run it the results don't appear in the console of the browser, the console just gives me this error ReferenceError: require is not defined – Cjvan Bukj Aug 05 '15 at 00:26
  • @CjvanBukj Well, you're not going to be able to use `require()` in the browser directly. You said your script didn't do anything but read some JSON? If it's doing more than that (like requiring other scripts), you should look into rolling up your code with Browserify. It will compile your required script into a single file, and will fill in some of Node.js modules as well. – Brad Aug 05 '15 at 00:43
  • it is requiring https, because the JSON file is on a remote server – Cjvan Bukj Aug 05 '15 at 00:44
  • Really appreciate, you taking the time to help me out. One last question, if i had to deploy this on a server to for example, show it to my friend or something, how would I do that? Is browserify still needed in that case? – Cjvan Bukj Aug 05 '15 at 01:04
  • Yes, Browserify is still needed to compile the JS. – Brad Aug 05 '15 at 01:21