Menu

Node.js vs JavaScript: What Is the Difference?

JavaScript is the language; Node.js is a program that runs it outside the browser. The syntax is identical, but the globals and built-in APIs around your code are different.

This page includes runnable editors. Edit, run, and see output instantly.

JavaScript is a programming language; Node.js is a runtime, a program that runs JavaScript outside the browser. The syntax, types and standard objects (Array, Promise, JSON) are the same in both. What changes is the environment around your code: a browser gives you window and document to work with a web page, while Node.js gives you global, process, require and modules like fs to work with files, servers and the operating system.

The browser globals printed undefined, the Node.js ones object or function, and the last line true, because in Node.js globalThis and global are the same object. Paste the first six lines into a browser console and the results flip.

The difference in one table

JavaScript in a browserJavaScript in Node.js
What it isThe language, run by the browser's engineThe same language, run by the Node.js runtime (V8)
Global objectwindowglobal
Works onThe page: document, the DOM, eventsThe machine: fs, http, process, os
Modules<script type="module">, importrequire (CommonJS) and import (ES modules)
PackagesBundled into the pageInstalled into node_modules with npm
InputClicks, forms, keyboard eventsCommand line arguments, standard input, HTTP requests
Array, Promise, JSON, setTimeout, consoleYesYes

The module row is the one that trips people up most. Node.js supports both systems, require and import, while browsers only understand import. The CommonJS vs ES modules page explains which one to pick.

The language is the same

A function that uses only the language runs anywhere. This block has nothing Node-specific in it, and it would print the same lines in a browser:

It printed {"best":95,"average":85} and Passed: 2 of 3. Spread syntax, reduce, template strings and JSON belong to the language, defined by the ECMAScript standard, so every runtime has them.

What only Node.js has

The Node-specific part is the set of built-in modules and globals that talk to the operating system:

It wrote a file, read it back, and printed .txt 2 lines, then an empty list of arguments. Neither file access nor process.argv exists in a browser, where a page cannot touch your disk.

Which language features you get

The language has versions (ES2015, ES2020, ES2024 and so on), and each engine adds new features at its own pace. Node.js bundles one specific V8 engine per release, so your Node.js version decides which features work: Object.groupBy and Array.prototype.findLast, for example, need a recent one. A browser feature table therefore lists Node.js as one more column next to Chrome, Firefox and Safari. Run node --version when a newer syntax fails with a SyntaxError or a method is undefined, and upgrade before reaching for a polyfill.

Writing code that runs in both

Library authors sometimes check where their code runs before using an environment-specific API:

const isNode = typeof process !== 'undefined' && process.versions?.node != null;
const isBrowser = typeof window !== 'undefined';

Check for the feature you need, not the environment name, when you can: typeof fetch === 'function' says more than "is this Node". Both have fetch, URL, TextEncoder, setTimeout and structuredClone today, so less code needs a check than a few years ago.

Common mistakes

Using document or window in a Node.js script. You get ReferenceError: document is not defined. That code belongs in a browser.

Using require in a browser. Browsers do not have it. Use import, or a bundler that turns require calls into one file.

Thinking Node.js is a different language. It is not. If a JavaScript tutorial teaches it, it works in Node.js, unless it uses the DOM.

Frequently Asked Questions

Can I learn Node.js without learning JavaScript?

Not really. Every line you write in Node.js is JavaScript, so the language comes first. Once you know functions, objects and promises, Node.js is a set of extra APIs to learn.

Why do I get "document is not defined" in Node.js?

document is the browser's DOM, and Node.js has no page to give you one. Code that touches document or window has to run in a browser, or in a test setup that fakes the DOM, such as jsdom.

Is TypeScript the same as Node.js?

No. TypeScript is a typed version of JavaScript that compiles to JavaScript. You can run the result in Node.js or in a browser, and recent Node.js versions can also run many .ts files directly by stripping the types.

Coddy programming languages illustration

Learn to code with Coddy

Get started