EXPLAINER

Node.js

Node.js is a JavaScript runtime outside the browser, used for servers, tools and scripts. It is built on V8 and an event loop, and it is the usual host for Express, Next.js servers, and SPFx build tooling.

Short answer: Node.js runs JavaScript on the server and in tools. npm installs packages. AI-oriented patterns are in Node.js for AI Applications; this page is the runtime. SPFx's build also needs a specific Node version.

What it is

Node.js is a runtime that executes JavaScript outside a browser. It uses the V8 engine and a library called libuv to do file and network I/O on an event loop. There is no DOM and no window. There is a module system, a standard library for HTTP, files and processes, and access to npm. People say "Node" for the runtime and "Node.js" when they want to be precise.

Node is why the same language can power a CLI, an Express API, a Next.js server, and the SPFx build tools. Node.js for AI Applications and Streaming AI Responses with Node.js cover model calls. This page is the runtime decision and the mental model.

Why and when it is used

Use Node when the team writes JavaScript or TypeScript and needs a server or a build tool. It is a strong fit for HTTP APIs that mostly wait on databases and other APIs, which is most product backends. Use it when you want one language across a React UI and the API.

Do not use Node for heavy CPU work on the main thread; it will stall every request. Do not use it because a tutorial started there if your team is fluent in another stack and the problem is ordinary. A supported Node version is a hard requirement for SPFx builds; "newest Node" is often wrong for that toolchain.

How it works

One process runs your script. The event loop handles callbacks and promises. Blocking the loop with a long synchronous computation blocks everyone. Native addons and worker threads exist for the rare CPU-heavy case. The module system loads files and caches them. Environment variables configure the process. A web server is a callback on a port: parse the request, do async work, write the response. If you forget to write the response, the request hangs until a timeout.

npm reads package.json and a lockfile and fills node_modules. The version of Node itself is not in that folder. Use a version manager or your platform's pinned runtime so production matches CI. TypeScript typechecks and emits JS, or a loader strips types; Node runs the JS.

Technologies and dependencies

A Node LTS release, npm (bundled with it), and your application code. Express is a small HTTP layer you can add. Next.js includes its own server. Databases need a driver. Process managers or containers keep it running in production. Logs go to stdout so the platform can collect them. Environment variables hold configuration and secrets.

How to get started

  1. Install an LTS Node and run node -v and npm -v.
  2. npm init a folder. Add "type": "module" if you want modern import syntax and accept that choice.
  3. Write a script that reads a file and prints a line count using the fs promises API, not a sync call you copy from a gist.
  4. Create an HTTP server with the standard library that returns "ok" on /health. Hit it with curl.
  5. Add a dependency only when the standard library is genuinely painful. Commit the lockfile.
  6. If this is for SPFx, stop and install the Node version that SPFx release documents, not the one you just used for the experiment.

Set engines in package.json to the major version you tested. A teammate on an ancient Node will get strange syntax errors otherwise.

Cautions and trade-offs

Uncaught promise rejections can crash modern Node. Handle them. process.env values are always strings or undefined; a missing variable should fail startup, not fail three requests later. Do not run Node as root in a container.

The npm ecosystem is huge and uneven. Pin versions, review install scripts for sensitive apps, and do not import a package for a one-line helper. Native modules break when you switch CPU architecture (a Mac laptop image on a Linux server). Rebuild in CI on the target platform. Finally, Node's single thread is a feature until a JSON.parse of a giant body stalls you. Limit payload sizes.