What Is Node.js?
What Node.js Does and Why It Exists
Node.js is an open-source runtime that runs JavaScript outside the web browser, usually on a server. A runtime is the software that executes a program, so Node.js lets developers use one language on the server and in the browser.
JavaScript was designed to run in browsers, where it updates pages and reacts to clicks. Node.js keeps the same language but gives it access to the computer it runs on, including files, network connections and operating system features. Node.js handles many visitors at once through a mechanism called the event loop.
Developers use Node.js to build:
- Web servers and APIs that send data to websites and mobile apps
- Real-time features such as chat and live dashboards
- Command-line tools and build tools used during development
Most of the packages for these tasks come from npm, the package manager that ships with Node.js.
How Node.js Differs From JavaScript in the Browser
Browser JavaScript and Node.js share the same core language, but their surroundings differ. In a browser, code reads and changes the page through the Document Object Model (DOM), the tree of elements that makes up a web page, and it works with the global window object. Node.js has no page to work with, so these browser features are absent.
In return, Node.js provides built-in modules for working with the file system, network sockets and HTTP. A server written in Node.js can read a file from disk, query a database and send a response, all within one program.
The other major difference is where the code runs. Browser code is downloaded by each visitor and runs on the visitor's device. Node.js code runs on your server, so visitors never receive it. That makes the server a suitable place to keep database passwords and API keys out of the visitor's reach.
Why Node.js Needs Different Hosting Than PHP
PHP is usually run per request: the web server hands a request to the PHP engine, the script runs, the response is sent and the process ends. A Node.js application works differently. It is a program that starts once, keeps running and listens for connections on a network port.
Because of this, a host that runs Node.js must keep the application alive, restart it after a crash and route visitor traffic to its port. A reverse proxy such as Nginx or Apache often sits in front of the application to handle HTTPS and forward requests. Typical options are virtual private servers, platform services and some managed Node.js hosting. Many shared plans built around PHP alone do not support long-running applications.
For a side-by-side view of the two approaches, see PHP vs Node.js.
Frequently asked questions
Is Node.js the same as JavaScript?
No. JavaScript is the programming language, and Node.js is a runtime that executes JavaScript on a server or computer. Node.js uses the same language as browsers but adds server features, such as file access and networking, that browsers do not provide.
Can Node.js run on shared hosting?
Sometimes. Some shared hosting plans include a Node.js application manager, but many only support PHP and static files. Because Node.js needs a process that stays running, check that your host supports long-running applications before you choose a plan. A VPS or platform service gives more control over this.