So you’ve heard of Node.js, but aren’t quite sure what it is or where it fits into your development workflow. Or maybe you’ve heard people singing Node’s praises and now you’re wondering if it’s something you need to learn. Perhaps you’re familiar with another back-end technology and want to find out what’s different about Node.
If that sounds like you, then keep reading. In this article, I’ll take a beginner-friendly, high-level look at Node.js and its main paradigms. I’ll examine Node’s main use cases, as well as the current state of the Node landscape, and offer you a wide range of jumping off points (for further reading) along the way.
Please note that, throughout the article, I’ll use “Node” and “Node.js” interchangeably.
What Is Node.js?
There are plenty of definitions to be found online. Let’s take a look at a couple of the more popular ones. This is what the project’s home page has to say:
And this is what Stack Overflow has to offer:
However, when we say that Node is built on the V8 engine, we don’t mean that Node programs are executed in a browser. They aren’t. Rather, the creator of Node (Ryan Dahl) took the V8 engine and enhanced it with various features, such as a file system API, an HTTP library, and a number of operating system–related utility methods.
How Do I Install Node.js?
In this next section, we’ll install Node and write a couple of simple programs. We’ll also look at npm, a package manager that comes bundled with Node.
Node Binaries vs Version Manager
Many websites will recommend that you head to the official Node download page and grab the Node binaries for your system. While that works, I would suggest that you use a version manager instead. This is a program that allows you to install multiple versions of Node and switch between them at will. There are various advantages to using a version manager. For example, it negates potential permission issues when using Node with npm and lets you set a Node version on a per-project basis.
If you fancy going the version manager route, please consult our quick tip: Install Multiple Versions of Node.js using nvm. Otherwise, grab the correct binaries for your system from the link above and install those.
“Hello, World!” the Node.js Way
You can check that Node is installed on your system by opening a terminal and typing
node -v. If all has gone well, you should see something like
v12.14.1 displayed. This is the current LTS version at the time of writing.
Next, create a new file
hello.js and copy in the following code:
This uses Node’s built-in console module to display a message in a terminal window. To run the example, enter the following command:
If Node.js is configured properly, “Hello, World!” will be displayed.
Save this code to a file called
index.js and run it from your terminal using the command
node index.js. You should see
As I mentioned earlier, Node comes bundled with a package manager called npm. To check which version you have installed on your system, type
Installing a Package Globally
Open your terminal and type the following:
npm install -g jshint
This will install the jshint package globally on your system. We can use it to lint the
index.js file from the previous example:
You should now see a number of ES6-related errors. If you want to fix them up, add
/* jshint esversion: 6 */ to the top of the
index.js file, re-run the command and linting should pass.