Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How does Node.js work?

Hey guys. I just recently finished the node.js basics which was pretty simple enough. I'm am just confused on how it works. When I install node to my visual studio code does it convert the entire script to a node script? I noticed when I type in "document" within that script it says "not defined" because the DOM doesn't exist in node. So my questions are:

When I activate node will the entire script be a node script? How does this work when I'm using a hosting service? Do I just make a new file to write client side js?

1 Answer

Hi Abdullah,

Node is an environment that allows you to write javascript that can be run outside of a browser. That’s the simplest way I think I can explain it.

There isn’t such a thing as a Node script, really. It’s just plain JavaScript, but because the DOM is a browser feature, the Node environment has no idea what “document” is, as document is not a keyword in Node. You seem to understand that part of it though.

You can read the DOCs at: https://nodejs.org/docs/latest-v13.x/api/ to find out what’s available for you to access and manipulate within the Node environment. Outside of writing your own servers, one of the things that I found cool about Node was its fs (file system) module, which allowed me to both read csv files from my computer as well as write csv files to it.

There’s tons you can do with Node. You’re gonna love it the more you work with it.

As far as hosting goes, companies like Netlify and Heroku build the apps based on your repo on sites like Github. But if you’re using a different hosting platform I imagine that the file extension clues the hosting service on which environment/interpreter can be used to read/execute your files. For example, a script that has an extension of py signifies that the code inside that file is written in python, and a script that has an extension of js signifies that the code inside that file is written in JavaScript (or for the Node environment).

Hey Brandon, thanks for the response.

The thing that is confusing me is this "node environment". When I install node on visual studio, I install it in the console. But when I try to access the DOM in my js file it says "DOM is undefined". So my question is does the whole script become the node environment, or is it just the command line in the console? If it is just the command line in the console then how would that work on an actual hosting site. How would I install node there? And if the entire script becomes the environment then do I have to create another script to access the dom?

Okay Abdullah,

A few things, just to get us on the same page.

  1. You don't/can't install Node on Visual Studio (unless you're referring to the IDE, and not Visual Studio Code—the code editor). You can install Node with Visual Studio Code's console/terminal, but the actual application should/will be installed locally on your machine.
  2. Once Node is installed properly, entering the command node at the terminal prompt runs the Node application. That's the node environment, and it's essentially the same style of environment as the console in the developer tools section of a browser. So you can do arithmetic, create variables, etc. It's like a REPL (read evaluate print loop), if you happen to be familiar with the term.
  3. If you want to use the node environment to run a script, you need to make sure that your terminal is telling you that you're in the same directory as the script, and you enter the command node <name of script>, and node will attempt to interpret the script.
  4. If you're trying to create a simple web page with an index.html, css file, and javascript file; you don't need to run that in the node environment. You'll just open that in your browser like I imagine you're used to. Because Node does not have access to the DOM, it has no idea what that is.
  5. But say you're trying to write a program that allows a user to enter their name, and your program would take each letter of that name and produce a "silly" acronym. You could write that program in Node and a user could run that program without needing access to the Internet (it could be run outside of the browser, within the Node environment).
  6. If you want to serve files, you can use Node to run a server. To do this you would write the code for the server as a javascript file. Let's say we're going to name this file "server.js". You'd enter the command node server.js in your terminal, and Node would execute the file. The code inside server.js would essentially tell your computer to listen for any requests at a certain address on your computer (typically 127.0.0.1, also known as localhost). And when a request is made to that address (and the correct port), your server (the server.js file which is still being run via Node) would send back the pages you've instructed to be sent back.
  7. As far as hosting goes, the hosting service keeps your files organized and it is that entity that more-or-less executes the command node server.js which causes your program to run. So you don't install Node on the hosting service's site. That's abstracted from you. They will already have the correct version of Node already installed.
  8. If you want to manipulate the DOM then you need to link your javascript files that have the code for that to your html file.