What is Node.js?
Node.js is a JavaScript runtime, which means it runs programs written in JavaScript. Most developers use it to create command-line tools or web server applications.
Skip Ahead?
Thatâs everything you need know about Node.js. If youâre eager to start programming, skip ahead to Chapter 2. That said, itâs worth revisiting this chapter later to learn about Nodeâs advantages and core features.
JavaScript, JScript, ECMAScript, ES6, ES2015?
To make learning more confusing for beginners, JavaScript has many names. It started life as âLive Scriptâ in 1994. Netscape rebranded it as âJavaScriptâ following a hasty deal with Sun Microsystems, despite it bearing little resemblance to Java or lightweight scripting. Microsoft couldnât use that name, so it became JScript
 in Internet Explorer.
In 2005, Mozilla (which grew out of Netscape) joined ECMA International and standardized the language as â ECMAScript â. Versions 1 to 3 documented the evolution of JavaScript up until 1999. Version 4 was abandoned, but ECMAScript 5 became the standard supported by most browsers in 2009.
Work then started on ECMAScript 6âor âES6â. The final specification was approved in 2015, which led to yet another name: âES2015â. New specifications now arrive every year.
Rightly or wrongly, this course refers to âJavaScriptâ throughout. References to specific versions (such as ES9/ES2018) are only made if they affect the version of Node.js you need to use.
Node.js was initially developed by Ryan Dahl. He took the V8 JavaScript engine from Googleâs Chrome browser, added some APIs, wrapped it in an event loop, and launched it as an open-source product on Linux and macOS in 2009. The Windows edition arrived in 2011.
The Node Package Manager (npm) was introduced in 2010. It allowed developers to use code modules published by others in their own projects. There was no official ECMAScript module standard at the time, so Node.js and npm adopted CommonJS.
The first (non-beta) release of Node.js arrived in 2015, with updates promised every six months.
Node.js wasnât the first JavaScript runtime, but unlike other optionsâsuch as Rhino and SpiderMonkey âits popularity grew exponentially. Even those writing PHP, Python, Ruby or other languages often use Node.js tools to supplement their development processes.
Why Learn Node.js?
JavaScript is hugely popular on GitHub, and itâs ranked highly by developers. Companies including Netflix, Uber, Trello, PayPal, LinkedIn, eBay, NASA and Medium have adopted Node.js, and most professional developers will have encountered Node.js tools.
Below, weâll look at some of the reasons you should consider using Node.js.
Itâs JavaScript
JavaScript is used on trillions of web pages, where it has a browser monopoly. Every professional web developer requires JavaScript knowledge to program client-side applications.
Server-side languages are more diverse. Historically, developers could opt for PHP, Ruby, Python, C# (ASP.NET), Perl, or Java, but these have different syntaxes and concepts. It can be difficult to switch contexts, so larger project teams often split into frontend and backend developers.
Node.js allows a developer with frontend JavaScript knowledge to leverage their skills on the backend. It wonât make you a full-stack developer overnight, but the concepts are similar, and thereâs less rigmarole when formatting JSON, handling character sets, using WebSockets, and so on.
JavaScript Alternatives
Some developers prefer languages such as TypeScript, PureScript, CoffeeScript, Reason, and Dart, which can transpile to JavaScript and run in a browser or Node.js. Ultimately, it still results in JavaScript code.
Itâs Fast
Most server-side languages are fast enough, but few match the speed of Node.js. The V8 engine is quick, and it evolves rapidly, having the weight of Google and Chrome behind its development. Node.js also has a non-blocking, event-driven I/O.
Letâs go through that again with less jargon. Most languages use synchronous blocking execution. When you issue a commandâsuch as fetching information from a databaseâthat command will halt further processing and complete before the runtime progresses to the next statement. To ensure that multiple users can have access at the same time, web servers such as Apache create a new processing thread for every request. This is an expensive operation, and Apache has a default limit of 150 concurrent connections. Busy servers can become overloaded.
Node.js code (and browser JavaScript) runs on a single processing thread. Long-running tasks such as a database query are processed asynchronously, which doesnât halt execution. The task runs in the background, and Node.js continues to the next command. When the task is complete, the returned data is passed to a callback function. A Node.js program can have many hundreds of ongoing operations that are completed whenever theyâre finished, meaning that the processor is free to tackle other tasks.
Asynchronous programming has challenges, but itâs possible to create fast Node.js applications that scale well.
Itâs Real-time
Web platform features such as WebSockets and server-sent events permit real-time functionalityâsuch as instant data updates, live chat, multiplayer games, and more. These can be difficult to implement in traditional server-side languages, where they often require third-party services. Real-time functionality in Node.js is significantly easier.
Itâs Lightweight
The Node.js runtime is small and cross-platform. As well as catering for Linux, macOS, and Windows, you find editions for embedded systems, the Raspberry Pi, and even SpaceX rockets.
Itâs Modular
Node offers a minimal standard library with good documentation. It contains basic functions for error handling, file system access, network operations, and logging.
For everything else âŚ
Itâs Extendible
Node.js has the largest package registry in the world, with more than one million modules. Youâll find pre-written code for task runners, loggers, database connectors, image processors, code compilers, web servers, API managers, and even client-side libraries.
The npm command-line tool is provided with Node.js and makes it easy to install, update, and remove modules. You can also use it to install global modules so Node.js scripts can run as commands from anywhere on your system.
Itâs Open Source
Node.js is an open-source project. The runtime is free to use without any commercial restrictions. The majority of modules are also free, because theyâre submitted by the community for the benefit of other developers.
Itâs Everywhere
This course concentrates on web applications, but you can use Node.js to create serverless functions, deployment scripts, cross-platform command-line tools, and even complex graphical apps such as VS Code, Slack, and Skypeâall of which use the Electron framework.
As a web developer, youâll almost certainly encounter Node.js, even if itâs not a core part of your technology stack. Knowing a little Node.js could help your projects and career. Youâll have a better insight into the possibilities available to modern web applications.
What About Deno?
Node.js is a cross-platform, V8-based JavaScript runtime released by Ryan Dahl in 2009.
Deno is a cross-platform, V8-based JavaScript runtime released by Ryan Dahl in 2020.
Deno smooths over some cracks and inconsistencies of Node.js, with the benefit of a decadeâs worth of hindsight. It directly supports TypeScript without a compiler, uses ES6 modules rather than CommonJS, replicates many browser APIs (window, addEventListener, Fetch, Web Workers, etc.), and provides built-in tools for linting, testing, and bundling.
Deno is greatâbut itâs new, and yet to achieve a fraction of Nodeâs popularity. The frameworks are similar: if you know one, itâs easy to switch to the other.
Summary
In this chapter, youâve learned that Node.js is a popular JavaScript runtime thatâs uniquely suited to web development. Iâve summarized it in this chapterâs video. Chapter 2 describes how to install Node.js on your platform of choice.
Quiz
Many chapters in this course end with a quick quiz to ensure youâve grasped the concepts. Beware! Some questions are designed to catch you out, so make sure youâve been paying attention!