DEV Community

Cover image for Getting to Know Node.js (Part I)
Oscar Luna
Oscar Luna

Posted on • Edited on

Getting to Know Node.js (Part I)

Hello! For this next series I'm going to skip a little farther ahead as a means to get caught up in transcribing all these handwritten notes, and jump right into Node.js.

What is Node.js?

Node.js is a powerful tool that takes our JavaScript code out of the client side and into the network for a variety of uses, from writing servers to installing packages, I can really go on and on about it. Before we get started, here is a quick breakdown as to what Node.js is:

  • Node.js is a cross-platform, open-source JavaScript runtime environment that allows data to be sent and received asynchronously, that is, at the same time, as a single process.
  • Node.js allows the use of JavaScript for writing server-side code, which the browser as an environment does not provide. THIS. IS. HUGE.
  • Node.js contains an entire library with millions of user-created libraries and frameworks that can be integrated to our code simply by installing them.
  • Node.js provides full control of the environment, since it is not constrained by the client's browser.
  • Node.js is powered by V8, the same JavaScript engine that powers Chrome (Other browsers use different JavaScript engines, ie. SpiderMonkey for FireFox).

Crazy stuff, right? We'll go a little deeper into some of the core parts throughout this series. For now, let's dive into our command line! If you don't have Node installed you can do so here.

The Node Environment

From the command line, we can run node to open the Node environment. Appending a file will execute the file, similar to the JavaScript console.

//HelloWorld.js

const greeting = "Hello world";
console.log(greeting); //Hello world

//On the command line:
$node HelloWorld.js //Hello world
Enter fullscreen mode Exit fullscreen mode

Node.js has a global process binding that allows us to inspect our program (similar to console). It also has access to JavaScript global bindings like JSON and Array. However, with Node we don't have access to browser objects like document or window.

To exit the Node environment we can simply run process.exit, which can also receive an exit status code indicating the runtime's success (0 meaning successful, non-zero values mean there was an error). The process module also has an env property that provides the environment variables set upon starting, such as NODE_ENV, which is an environment variable that can be set to production or default to development.

process.env.NODE_ENV || 'development'

Enter fullscreen mode Exit fullscreen mode

That's it for now! Next time I will dive into the module system, which is a precursor to my favorite feature in Node.js-- the Node package library! Stay tuned!


Works Cited

  1. Haverbeke, Martin "Node.js", Eloquent JavaScript - A Modern Introduction to Programming, 3rd Edition, 2019, Chapter 20, No Starch Press, Inc.
  2. NodeJS Foundation. "Introduction to Node.JS" Node.js. Accessed November 13, 2020. https://nodejs.dev

Top comments (0)