Application configuration
Configure ports, feature flags, credentials, or deployment modes without hard-coding them in application source.
Node.js · Pipe
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.
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.
process.env
process.env.NAME
process.env.NAME = value
delete process.env.NAMEAn object containing the user environment.
process.env to obtain variables supplied to the running Node.js process.env setting.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.
Configure ports, feature flags, credentials, or deployment modes without hard-coding them in application source.
Set process-local variables before launching libraries or subprocess-related logic that reads them.
Provide each worker with a tailored environment during worker construction.
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.
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.
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.loadEnvFileWorkerworker_threadsNODE_OPTIONS