How to Configure Environment Variables for Node.js with the Airbnb JavaScript Style Guide

The Airbnb JavaScript Style Guide explicitly allows reading environment variables via process.env by disabling the no-process-env ESLint rule in its Node.js configuration.

When building Node.js applications that follow the Airbnb JavaScript Style Guide, you can configure and access environment variables without triggering linting errors. The airbnb/javascript repository provides shareable ESLint configurations that handle Node.js-specific patterns, including how your code interacts with process.env.

Understanding the Airbnb ESLint Configuration for Node.js

The Airbnb style guide distributes its rules through the eslint-config-airbnb-base package, which includes a dedicated Node.js ruleset. This configuration file specifically addresses how environment variables should be handled in server-side JavaScript applications.

The no-process-env Rule in node.js

In packages/eslint-config-airbnb-base/rules/node.js, the Airbnb team explicitly disables the no-process-env rule:

// packages/eslint-config-airbnb-base/rules/node.js
'no-process-env': 'off',

Source

This configuration choice means that direct access to process.env is permitted by default when extending the Airbnb base configuration in Node.js projects. The rule is disabled because environment variables are the standard mechanism for configuring Node.js applications across different deployment environments, including Docker containers and CI/CD pipelines.

Reading Environment Variables with Airbnb Style Guide

Since the Airbnb configuration allows process.env access, you can implement environment variable configuration using standard Node.js patterns without ESLint interference.

Basic process.env Usage

You can reference environment variables directly in your application code:

// src/index.js
const http = require('http');

const PORT = process.env.PORT || 8080;
const NODE_ENV = process.env.NODE_ENV || 'development';

http.createServer((req, res) => {
  res.end('Hello, world!');
}).listen(PORT, () => {
  console.log(`Server running in ${NODE_ENV} mode on port ${PORT}`);
});

This pattern is fully compliant with the Airbnb style guide because the no-process-env rule is disabled in the base configuration.

Loading Variables from .env Files

For local development, you typically load environment variables from a .env file using the dotenv package. This approach remains compatible with Airbnb's configuration:

// src/index.js
require('dotenv').config(); // Loads .env into process.env

const http = require('http');

const { PORT, DATABASE_URL } = process.env;

http.createServer((req, res) => {
  res.end('Hello, world!');
}).listen(PORT || 3000, () => {
  console.log(`Server listening on port ${PORT}`);
});

The dotenv configuration should occur at your application's entry point before any other code executes, ensuring all modules have access to the loaded environment variables.

Enforcing Stricter Environment Variable Handling

While the Airbnb style guide permits direct process.env access, some teams prefer to centralize configuration management. You can override the default configuration to enforce stricter patterns.

Overriding the no-process-env Rule

To prohibit direct process.env usage and force developers to use a configuration wrapper, re-enable the rule in your project's ESLint configuration:

{
  "extends": ["airbnb-base"],
  "rules": {
    "no-process-env": "error"
  }
}

With this configuration, any direct reference to process.env will trigger an ESLint error, prompting developers to centralize environment variable access.

Centralized Configuration Pattern

When enforcing the no-process-env rule, implement a dedicated configuration module that loads and validates environment variables in one location:

// src/config.js
require('dotenv').config();

module.exports = {
  port: process.env.PORT || 3000,
  databaseUrl: process.env.DATABASE_URL,
  nodeEnv: process.env.NODE_ENV || 'development',
  apiKey: process.env.API_KEY,
};
// src/index.js
const http = require('http');
const config = require('./config');

http.createServer((req, res) => {
  res.end('Hello, world!');
}).listen(config.port, () => {
  console.log(`Server running on port ${config.port} in ${config.nodeEnv} mode`);
});

This pattern isolates environment variable dependencies to a single module, making your application easier to test and configure across different deployment environments.

Summary

  • The Airbnb JavaScript Style Guide disables the no-process-env rule in packages/eslint-config-airbnb-base/rules/node.js, allowing direct process.env access.
  • You can read environment variables without ESLint errors using standard Node.js patterns like process.env.PORT || 3000.
  • For local development, load .env files using dotenv at your application's entry point before other imports.
  • Teams requiring stricter control can override the rule to error and implement a centralized config.js module to isolate environment variable access.

Frequently Asked Questions

Does the Airbnb style guide allow using process.env directly?

Yes, the Airbnb style guide explicitly allows direct process.env usage. In the packages/eslint-config-airbnb-base/rules/node.js file, the no-process-env rule is set to 'off', which permits environment variable access without triggering ESLint warnings or errors.

How do I load environment variables from a .env file while following Airbnb standards?

Install the dotenv package and require it at the very top of your application's entry file before any other code executes. This loads the variables into process.env, which you can then access throughout your application. Since Airbnb disables the no-process-env rule, this pattern complies fully with the style guide.

Can I enforce stricter rules for environment variable handling than Airbnb recommends?

Yes, you can override the default configuration by setting "no-process-env": "error" in your project's .eslintrc.json file. This prohibits direct process.env access and forces developers to use a centralized configuration module, which is useful for larger applications requiring strict configuration management.

Where should I place the dotenv configuration in a Node.js project using Airbnb style?

Place the require('dotenv').config() call at the top of your main entry file (typically index.js or server.js) before any other imports or application logic. This ensures that all environment variables are available to every module in your application when they initialize.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →