PortfolioHire me for one like this

Sample website by Chayangkoon

CodeSprout

Lesson 1 / 6

Lesson 1

What is Node.js?

6 min read

You already know that a web page is HTML, and that browsers can run JavaScript to make that page interactive. For a long time, the browser was the only place JavaScript could run. Node.js changed that. Node.js is a program you install on your computer (or on a server) that runs JavaScript files directly, with no browser window involved.

That sounds small, but it means JavaScript can do jobs that used to need other languages: read and write files, talk to databases, start a web server, and run build tools. Next.js is built on top of Node.js, so Node.js is the first thing you install.

Your first Node.js program

Make a file called hello.js with one line in it, then run it from a terminal with the node command. The text appears in the terminal, not in a browser.

hello.js
console.log("Hello from Node.js!");

// In the terminal:
// $ node hello.js
// Hello from Node.js!

Notice what is missing: there is no document and no window. Those belong to the browser. Node.js gives you different tools instead, such as access to files and the network.

Why Next.js needs Node.js

Next.js uses Node.js in three moments of a website's life:

  • While you build it: npm run dev starts a development server on your computer that reloads the page every time you save a file.
  • When you publish it: npm run build runs on Node.js and turns your components into HTML, CSS and JavaScript files the browser understands.
  • While visitors use it: in a normal deployment, a Node.js server keeps running and renders pages on each request, so it can show fresh data, check who is logged in, or save a form.

That last point matters. Next.js is not only a tool for making static files. Out of the box it is a server framework: the same React components can render on the server, on every request, and then become interactive in the browser.

Browser JavaScript vs Node.js

The language is the same in both places. What changes is what the code can reach. Code in the browser can change the page but cannot read your files. Code in Node.js can read files and keep secrets such as database passwords, but it cannot see a page. In Next.js you will write both kinds, and the framework helps you keep them in the right place.

In the next lesson you will meet npm, the tool that comes with Node.js and installs Next.js itself.