Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Pipe

process.env

process.env is Node.js’s object for reading and changing environment variables available to the current process. Values assigned to it become strings, and worker threads normally receive separate copies.

Since
v0.1.27

How it works

process.env is the object Node.js provides for accessing environment variables available to the process. Properties can be read, changed, or removed with delete; values assigned to it are converted to strings, and changes generally stay within the process rather than updating the parent shell or other worker copies.

Syntax

process.env
process.env.NAME
process.env.NAME = value
delete process.env.NAME

Returns

An object containing the user environment.

Key points

  • Use property access on process.env to obtain variables supplied to the running Node.js process.
  • Remove an environment variable from the current process by deleting its property.
  • Workers receive independent environment-object copies unless their construction explicitly specifies another environment.
  • A worker's initial environment comes from its parent or from the constructor's explicit env setting.
  • Only main-thread updates can affect the operating system environment or native add-ons.

Common mistakes

  • Assignments are string-based; do not depend on numbers or booleans retaining their original types.
  • Worker threads normally use separate environment-object copies, so a change in one worker is not automatically shared with another.
  • Changes made outside the main thread do not propagate to the operating system or native add-ons.

Example: Read and update environment variables

process.env.APP_MODE = 'production';

const mode = process.env.APP_MODE;
const port = Number.parseInt(process.env.PORT ?? '3000', 10);

console.log({ mode, port });

Reads values from the process environment, supplies a fallback for a missing port, and writes a string value to the current process environment.

When to use it

Application configuration

Configure ports, feature flags, credentials, or deployment modes without hard-coding them in application source.

Runtime setup

Set process-local variables before launching libraries or subprocess-related logic that reads them.

Worker configuration

Provide each worker with a tailored environment during worker construction.

Interview questions

QWhat is process.env, and what happens when it is modified?

process.env exposes the environment inherited by the Node.js process as an object. Its properties can be read and modified, but modifications do not change the parent process's environment.

Follow-up: What happens when a non-string value is assigned? Node.js implicitly converts it to a string, and the documentation marks this behavior as deprecated.

QHow does process.env behave in Worker threads?

A worker receives its own copy of process.env, based on the parent thread unless an env option supplies another environment. Updates are therefore not visible across workers by default.

Follow-up: Which thread can affect the operating system environment? Only the main thread can make changes visible to the operating system or native add-ons.

QHow do you read and remove an environment variable?

Read a variable with property access, such as process.env.PORT, and remove one with delete process.env.PORT. Missing variables produce an absent property value, so application code should handle that case explicitly.

Follow-up: How can a worker receive a deliberately different environment? Pass an env option when constructing the Worker; otherwise it derives its copy from the parent thread.

  • process.loadEnvFile
  • Worker
  • worker_threads
  • NODE_OPTIONS

Source: Process | Node.js. Checked 2026-10-15.