Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Identifying Browser vs. Node.js Code by Environment APIs

Recommended resources

Hello! Last lesson, you saw that TypeScript types disappear before JavaScript runs. The next question is where that JavaScript runs. The same language can execute in a browser or in Node.js, but each environment supplies different capabilities.

In this lesson, you’ll identify the intended environment of a code snippet by looking at the APIs it uses. That is useful when reading an unfamiliar project—and when explaining why code that works in a webpage fails inside a backend process.


The language is shared; the surroundings are not

JavaScript provides language features such as objects, functions, arrays, and conditionals. A runtime environment executes that language and supplies additional APIs. A browser supplies APIs for interacting with a webpage. Node.js supplies APIs for tasks such as reading server files and accessing process configuration.

The distinction is visible in this browser diagram:

A browser runs core JavaScript alongside browser-provided APIs; an application may also use third-party APIs. The browser APIs are not part of the JavaScript language itself.

For example, document.querySelector() is not a feature of every JavaScript runtime. The browser provides document to represent a webpage and lets code search that page. An ordinary Node.js process has no webpage to represent, so it does not provide document by default.

Watch a short demonstration of what window and document refer to. The important distinction is the one between the browser tab and the page loaded inside it.

Javascript Window vs. Document vs. Screen WHAT??

“Javascript Window vs. Document vs. Screen WHAT??” by Colt Steele makes two common browser globals concrete.

Watch the window for its role in a browser tab. Then watch the document for its relationship to page content, and DOM methods for how code finds or changes that content. You can skip the discussion of screen; it is not needed for this lesson.

The presence of a familiar JavaScript syntax tells you little about the environment. The API being called is better evidence. document.querySelector("h1") points to code that needs a browser page; readFile() imported from Node’s file-system module points to code that needs Node.js.



Recognizing browser-provided APIs

Imagine a page that displays a membership status:

const statusElement = document.querySelector("#membership-status")

if (statusElement) {
  statusElement.textContent = "Active"
}

The clue is document, not the fact that the file ends in .ts. The code needs a page containing an element with that ID. It belongs in a browser context with a DOM, not in an ordinary Node.js server process.

Other browser clues include window.location for information about the current page’s URL and localStorage for browser-side storage. These APIs describe or affect the user’s browser environment. They do not give a backend access to the user’s tab.

Read MDN’s explanation of browser APIs, then its examples of document manipulation and data fetching. Notice that fetching data is an activity, while an API name alone may not uniquely identify the runtime—a distinction we will return to shortly.

Introduction to web APIs - Learn web development | MDN

MDN’s “Introduction to web APIs” separates the JavaScript language from capabilities supplied by the browser and gives practical examples you might see in frontend code.

In “APIs in client-side JavaScript,” read browser API basics, focusing on what supplies those APIs. Then go to “What can APIs do?” and its “Common browser APIs” subsection. Read the paragraph beginning with APIs for manipulating documents, from document APIs; read the following paragraph, from fetching data. The document paragraph is the strongest clue for identifying browser-only page code.

A useful qualification: not every kind of browser script has a document—a browser worker, for example, does not represent a page. For the ordinary page scripts considered here, though, document and window are strong evidence of browser execution.



Recognizing Node.js-provided APIs

Your backend may need to read a file from its own machine:

import { readFile } from "node:fs/promises"

const template = await readFile("./email-template.html", "utf8")
console.log(template)

The decisive clue is the import from node:fs/promises, Node’s file-system API. An ordinary webpage cannot import that module to read arbitrary files from its visitor’s computer. Browser security boundaries do allow users to grant access to selected files through browser APIs, but that is different from unrestricted Node file-system access.

Another common Node clue is process.env:

const serviceSecret = process.env.SERVICE_SECRET

A backend reads configuration from the environment of its running process. Browser code should not be expected to read the server’s process environment, and a server secret must not be shipped to the browser. A build tool might replace an environment-looking expression while preparing frontend files, but that would not give the browser access to Node’s process.env at runtime.

The Node.js documentation states the central contrast directly:

Differences between Node.js and the Browser | Node.js Learn

The Node.js documentation’s “Differences between Node.js and the Browser” gives a concise comparison of the APIs each environment provides.

In “Differences between Node.js and the Browser,” read the opening discussion starting with the shared language. Focus especially on its contrast between document and window in the browser and file-system functionality in Node.js. The discussion ends with the paragraph about file-system access; you do not need the later material on versioning or module systems.

For a quick demonstration of the consequence, watch Node.js attempt to access window:

Node.js Tutorial for Beginners: Learn Node in 1 Hour

“Node.js Tutorial for Beginners: Learn Node in 1 Hour” by Programming with Mosh shows the failure you would expect when browser-specific code runs in Node.js.

Watch the Node error. Notice that the JavaScript itself can be executed, but window is not defined in that environment. The demonstration is the point; you do not need the rest of the hour-long tutorial.

Node.js is not limited to file access: it can also support a backend handling network requests. The key test for this lesson remains the same: which environment supplies the particular API used by this code?



Classify by evidence, not by filename or framework

Some snippets clearly indicate one environment. Others do not:

Code clue What it tells you
document.querySelector(...), window.location, localStorage Needs browser-provided page or browser-storage APIs.
import ... from "node:fs/promises", process.env Needs Node.js-provided APIs.
Array.isArray(...), JSON.parse(...) Uses shared JavaScript features; neither environment is identified.
console.log(...), setTimeout(...) Available in both environments; neither identifies the runtime.
fetch(...) Available in browsers and modern Node.js; inspect surrounding code and configuration.

This is why “it uses fetch, so it must be frontend code” is unreliable. A browser might fetch data to update a page; a Node backend might fetch data from another service. Similarly, import is syntax used in both environments. What is imported—for example, node:fs/promises—is the more useful clue.

Consider a snippet with conflicting clues:

import { readFile } from "node:fs/promises"

const pageTitle = document.title
const template = await readFile("./email-template.html", "utf8")

As written, this is not a normal browser page script or a normal Node.js script. The browser does not provide Node’s file-system module; Node.js does not provide the page’s document. In a real application, the work needs to be separated: backend code can read its template, while browser code can read the page title. The two sides can communicate through an API when they need to exchange data.

Even a successful TypeScript check would not prove that an API exists at runtime. As in the previous lesson, TypeScript’s declarations are erased. Editor or compiler settings can describe browser APIs, but they cannot create a document inside Node.js.

You may encounter a library that can be used in more than one runtime. Its name alone does not settle where a particular file runs. Look at its entry point, imports, and environment-provided globals. Conversely, a React component that accesses document needs a browser page for that access, even if some rendering work elsewhere in the application also happens on a server.



Putting the distinction to work

Suppose a backend has a secret configuration value and a browser needs to display a membership status. The backend can read its configuration and send an appropriate non-secret result in an HTTP response. Browser code can then use document to show that result. The browser cannot directly read the backend’s process.env, and the backend cannot directly update the user’s document.

When diagnosing an unfamiliar snippet, use this order:

  1. Find the API calls and globals. Look beyond ordinary JavaScript syntax.
  2. Identify who provides them. Is it the browser’s page environment, Node.js, or both?
  3. Check for conflicts. A browser-only API and a Node-only API in the same execution path suggest misplaced code or a need to split responsibilities.

That approach is more dependable than guessing from .ts, .js, “frontend,” or “backend” in a filename. It also gives you a precise explanation when an interview asks why a particular line fails: name the missing environment-provided API, not just the error message.



Conclusion

Browser and Node.js code can use the same JavaScript language, but they do not have the same surrounding APIs. document and window indicate browser page code; Node’s file-system modules and process.env indicate Node.js code. Shared features such as JSON.parse, console.log, and modern fetch require more context before you classify them.

Next, you’ll put the Node.js side into practice by running a small TypeScript program and interpreting what it prints.