Starting from Zero with Node.js on Windows 10

September 30, 2019
Written by
AJ Saulsberry
Contributor
Opinions expressed by Twilio contributors are their own

Starting from Zero with Node.js on Windows 10

JavaScript may be ubiquitous in the world of web development, but not every developer has been working on web front-end projects, and not every web developer has had the opportunity to get into server-side JavaScript. You might also be new to JavaScript—lots of folks are—and getting a handle on what you can do with the language.

If your development experience fits into these broad categories your awareness of Node.js may be limited to having heard the name. You might also have heard something about running JavaScript without a browser. You might be wondering whether there’s a place for Node.js in your programming toolbox.

If you’re ready to launch your Node.js learning mission, this post is for you: it’s a roundup of the essential information you need to get off the launchpad. The explanations below are valid for Windows, macOS, Linux, and AIX, but the instructions apply only to Windows 10.

Understanding why Node.js came to be and what it does

You probably know that JavaScript is the predominant scripting language built into modern web browsers, the halcyon days of VBScript having passed along with those of Internet Explorer. JavaScript provides the functional programming capabilities needed to “activate” the content in the Domain Object Model (DOM) of a web page. It’s also used to handle communication and data manipulation tasks.

Web browsers run JavaScript in a JavaScript engine which provides either just-in-time (JIT) compilation into machine code or runtime interpretation. The Google V8 engine used in the Google Chrome browser is one of the most widely-used JIT compilation engines for JavaScript.

As JavaScript grew in sophistication and capacity, developers saw advantages to using it on the web server side of the browser/server relationship. To be useful on the server JavaScript needed an engine to run in, multitasking, and a way to handle HTTP requests.

This is what Node.js does. It provides an event-driven runtime environment for JavaScript code using the V8 engine and a concurrency model, a way of doing multitasking, that’s efficient and suited to building scalable network applications. Support for HTTP is built in.

To develop a deeper understanding of concurrency and the event model in JavaScript runtime engines, check out the Understanding JavaScript asynchronous code section of the post Asynchronous JavaScript: Understanding Callbacks. It’s the first part in a whole series on Asynchronous JavaScript.

If you’re coming from the .NET world, it’s important to know that JavaScript takes a different approach to multitasking. Also, while some of JavaScript’s asynchronous capabilities, like callbacks and Promises, are built in, others, like Observables, are provided by add-on libraries.

Getting started with Node.js

You can download and use Node.js for free and there are no fees for building and distributing apps with Node.js. The source code is published on GitHub and the repository contains substantial documentation about the project, software, license, and contributing. It costs you only time, and there’s a good return on your investment if you stick with it.

Extending JavaScript with Node.js packages

Much like the NuGet ecosystem, there is a vast array of add-on libraries for JavaScript designed to work with Node.js. They are installed with npm, the Node Package Manager. It’s so much a part of Node development that the Node.js installer includes npm. There is a searchable repository of npm packages at npmjs.org where you can find useful tools like ESLint and RxJS.

Installing Node.js painlessly on Windows 10

There’s a Windows installer package for Node.js and npm, so unless you have special installation requirements installation is going to be quick and easy. The .msi comes in 64-bit and 32-bit editions and LTS (long term support) and current branch (latest features) flavors. Make your life simple by using the LTS branch of the bit edition that corresponds to your operating system.

Unless you or your company’s system administrator know a good reason not to, accept the defaults when you run the Node.js installer. In particular, be sure you accept the defaults to install npm and the documentation, and add the Node.js directory to your PATH environment variable.

A correctly configured 64-bit installation using the default values should add the following to your path:

C:\Program Files\nodejs\

The npm CLI is updated more frequently than Node.js, so after you’ve completed the Node.js installation update npm by executing the following npm command-line instruction:

npm install npm -g

You can verify that you have the appropriate software installed and the environment variables configured directly by executing the following instructions at a command prompt in any directory:

node -v
npm -v

The version numbers should correspond to those shown on the Node.js download page and npm/cli GitHub repository (The preceding link takes you to the latest stable release.) Information about npm packages can be found at npmjs.org, the home of the package repository.

Running Node.js

You run Node.js from the command line. Try it out with the obligatory “Hello world!” example by following these instructions in the documentation. See the documentation for a complete list of Node.js command-line arguments.

Basic Node.js security

By default, the Node.js server is only available on your machine, localhost at the IP4 address 127.0.0.1 Port 3000. You will have to consciously perform a number of steps to make your Node server accessible from beyond your machine.

Once you start building JavaScript projects for Node and using npm packages you’ll need to be alert for security vulnerabilities in those code packages. You can check the npm packages in your projects by using the npm audit command.

Node.js impact on system resources

You’ll be impressed with how unobtrusive Node.js is after installation. Aside from reserving about 8 – 10 MB of memory, there’s very little impact. Unless you’re running a Node.js program the executables don’t register CPU consumption in the Task Manager. If you see a Node.exe instance that’s chugging away for minute after minute, check for an instance of a program you’ve left running in a console window.

Learning more about Node.js

There is an excellent series of guides in the documentation. These cover core concepts, debugging, and deployment.

These answers above should get you launched on a worry-free trajectory with Node.js. It’s not going to mess up your Visual Studio configuration and you’ll probably enjoy all the things you can do with this compact and efficient runtime environment. JavaScript: it isn’t just for front-end anymore.

When you’re ready to move on to more advanced topics, there are many excellent JavaScript programming posts on the Twilo Blog. In particular, check out our series on Asynchronous JavaScript and JavaScript Microservices. They’re informative reading on essential topics for the aspiring JavaScript developer.

 

A. J. Saulsberry is a technical editor at Twilio. He helps developers teach other developers through Twilio’s Developer Voices program, which publishes technical posts on programming topics here on the Twilio Blog. If you’d like to write a post, or suggest a topic, contact AJ at asaulsberry@twilio.com.