Using the NODE_ENV variable in your CI environments in Node.js

Kenny DuMez
Kenny DuMez
Graphite software engineer

Understanding the NODE_ENV variable and the role of continuous integration (CI) environment settings is important for managing Node.js applications. These configurations play a significant role in optimizing development workflows and ensuring reliable deployments. This guide will explain what the NODE_ENV variable is, how it interacts with CI environments, and best practices for using these settings effectively in Node.js projects.

NODE_ENV is an environment variable in Node.js that specifies the environment in which an application is running. It's primarily used to indicate whether the application is running in a development, testing, or production environment. This setting can affect how the application behaves and performs, particularly in terms of logging, debugging, and other environment-specific configurations.

  • Performance optimization: In production, you may want to disable detailed error logging and debugging to enhance performance.
  • Feature flags: Enable or disable certain features depending on the environment.
  • Configuration management: Load different configurations or databases depending on whether the app is in development, test, or production.

Continuous Integration (CI) environments use automated processes to build, test, and deploy software. You can integrate the NODE_ENV variable within these environments to ensure that the correct behavior is executed depending on the stage of the build/deploy lifecycle.

  1. Define environment variables: In your CI configuration file, define NODE_ENV based on the stage of the pipeline. For example, during the testing phase, you might set NODE_ENV=test.
  2. Use scripts conditionally: In production environments, you can leverage the NODE_ENV variable to control which scripts and tasks are executed, optimizing your workflow by running only the necessary processes. For example, you might run code linting and testing during the test phase, but skip them and instead focus on performance optimizations like minification and compression when NODE_ENV is set to production, thereby improving build times and efficiency.
  • Consistency: Ensure that NODE_ENV is consistently used across all environments to avoid configuration drifts.
  • Security: Securely manage environment variables, especially in public repositories or shared CI environments. Use encrypted secrets or secure vault solutions to store sensitive configuration.
  • Documentation: Document how NODE_ENV and other environment variables affect the application’s behavior to avoid confusion among new developers and teams.

Here’s an example of how you might configure NODE_ENV in a GitHub Actions workflow for a Node.js application:

Terminal
name: Node.js CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Test
env:
NODE_ENV: test
run: npm test
- name: Build
env:
NODE_ENV: production
run: npm run build

Here the NODE_ENV variable defines the environment context for different tasks, impacting how scripts execute and optimize behavior based on the specified environment.

  1. Test environment: In the "Test" step of the workflow, setting NODE_ENV to test configures the application to use test-specific settings, which might include disabling unnecessary logging, using a different database, or activating certain test frameworks and tools. This ensures that the test environment closely mimics a controlled setup that isolates testing from production behaviors and settings.

  2. Production environment: In the "Build" step, setting NODE_ENV to production triggers optimizations appropriate for a production deployment. This can include enabling compression, minification of scripts and stylesheets, and other performance enhancements that are typically not useful during development or testing. Production settings ensure that the application is optimized for performance, security, and resource utilization, providing an environment suitable for live users.

Configuring NODE_ENV within each specific job step ensures that each phase of the pipeline uses the most appropriate environment settings, enhancing the reliability and efficiency of the development lifecycle from testing to deployment.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2