Skip to main content
Dev Centre House Ireland Company LogoDev Centre House Ireland
  • About Us
  • Services
  • Technologies
  • Industries
  • Case Studies
  • Startup Program
Dev Centre House Ireland Company LogoDev Centre House Ireland
  • Contact Us
  • [email protected]
  • +353 1 531 4791

FOLLOW US

LinkedIn iconFacebook iconX iconClutch icon

Services

  • Custom Software Development
  • Web Development
  • Mobile App Development
  • Artificial Intelligence (AI)
  • Cloud Development
  • UI/UX Design
  • DevOps
  • Machine Learning
  • Big Data
  • Blockchain
  • Explore all Services

Technologies

  • Front-end
  • React
  • Back-end
  • Java
  • Mobile
  • iOS
  • Cloud
  • AWS
  • ERP&CRM
  • SAP
  • Explore all Technologies

Industries

  • Finance
  • E-Commerce
  • Telecommunications
  • Retail
  • Real Estate
  • Manufacturing
  • Government
  • Healthcare
  • Education
  • Explore all Industries

Quick Navigation

  • About Us
  • Services
  • Technologies
  • Industries
  • Case Studies
  • Exclusive Partnership Program
  • Careers [We're Hiring!]
  • Blogs
  • Privacy Policy
  • InvestOrNot – Company checker for investors
  • Norway (Oslo)
© 2026 Dev Centre House Ireland All Rights Reserved
Flag of IrelandRepublic of Ireland
Flag of European UnionEuropean Union
Back to Blog
Technology

Using Environment Variables in NestJS Projects

Anthony Mc Cann
Anthony Mc Cann
15 May 2025
2 min read
Using Environment Variables in NestJS Projects

Table of contents

  • Why Use Environment Variables?
  • Setting Up Environment Variables in NestJS
  • Accessing Environment Variables
  • Validating Environment Variables
  • Using Multiple .env Files
  • Security Considerations
  • Real-World Use Cases
  • Final Thoughts

When building modern applications, configuration flexibility is essential. Hardcoding sensitive data such as API keys, database URLs, or secrets directly into your codebase is a recipe for trouble. That’s where environment variables in NestJS shine.NestJS supports environment variables out of the box via its built-in @nestjs/config package, helping developers write clean, secure, and easily configurable applications.In this article, you’ll […]

When building modern applications, configuration flexibility is essential. Hardcoding sensitive data such as API keys, database URLs, or secrets directly into your codebase is a recipe for trouble. That’s where environment variables in NestJS shine.
NestJS supports environment variables out of the box via its built-in @nestjs/config package, helping developers write clean, secure, and easily configurable applications.
In this article, you’ll learn how to set up, use, and manage environment variables in your NestJS project the right way.

Why Use Environment Variables?

Environment variables offer a secure and flexible way to manage configuration values. By storing credentials, tokens, and configuration settings outside your source code, you can:

  • Keep secrets out of version control
  • Easily switch configurations between development, staging, and production
  • Avoid code repetition and simplify deployments

Setting Up Environment Variables in NestJS

To begin using environment variables in your NestJS app, install the official @nestjs/config package:

npm install @nestjs/config

Then import ConfigModule into your AppModule:

Create a .env file in your root directory:

Now these variables are accessible throughout your app using ConfigService.

Accessing Environment Variables

To access environment variables inside services, controllers, or providers, inject ConfigService:

The second argument to .get() is a default value in case the environment variable isn’t set.

Validating Environment Variables

A best practice is to validate your .env variables to avoid runtime errors. Use the Joi library for schema validation:

npm install joi

Update ConfigModule setup:

This ensures that your app won’t start if essential environment variables are missing or incorrectly typed.

Using Multiple .env Files

For better organisation across environments, you can specify different .env files like:

  • .env for default values
  • .env.development
  • .env.production
    Set envFilePath dynamically based on the NODE_ENV:

Set the NODE_ENV in your scripts:

Security Considerations

  • Never commit your .env files to version control — use .gitignore
  • Store production secrets securely using CI/CD secrets or managed services (e.g., AWS Secrets Manager)
  • Always validate and sanitize values coming from the environment

Real-World Use Cases

You can use environment variables to configure:

  • Database connection strings
  • API base URLs
  • 3rd-party keys (Stripe, Twilio, Firebase)
  • Feature flags
  • Logging levels
  • Environment-specific configurations

Final Thoughts


Using environment variables in NestJS projects ensures that your app remains secure, flexible, and easy to manage across different environments. By leveraging the power of @nestjs/config, you get seamless integration with validation and support for multiple configurations out of the box.
Ready to take your Node.js backend to the next level? Discover how professional teams manage configurations and build scalable architectures with NestJS and Node.js at Dev Centre House Ireland.

Share
Anthony Mc Cann
Anthony Mc CannDev Centre House Ireland

Table of contents

  • Why Use Environment Variables?
  • Setting Up Environment Variables in NestJS
  • Accessing Environment Variables
  • Validating Environment Variables
  • Using Multiple .env Files
  • Security Considerations
  • Real-World Use Cases
  • Final Thoughts

Free Consultation

Have a project in mind? Let's talk.

Our engineers help businesses build scalable software — from MVP to enterprise. Book a free 30-min session.

Related Articles

View all →
Why Business Owners in Limerick Should Always Plan for Scalability from Day One
Technology

Why Business Owners in Limerick Should Always Plan for Scalability from Day One

Anthony Mc Cann28 January 2026
Why Dublin Startups Should Rethink IT Consultancy Before Their Next Project
Technology

Why Dublin Startups Should Rethink IT Consultancy Before Their Next Project

Anthony Mc Cann4 December 2025
The Future of Software Delivery Pipelines in an AI Supported Engineering World in Galway
Artifical Intelligence

The Future of Software Delivery Pipelines in an AI Supported Engineering World in Galway

Anthony Mc Cann4 December 2025

Contact Us!

Fill out the form below or schedule a call and we will be in touch. * indicates a required field.

Remaining Characters: 1000

By clicking Send, you agree to our Privacy Policy.

WHAT'S NEXT?

  1. 1

    We'll review your request, and start talking about your project.

  2. 2

    Our team creates a project proposal with timelines, costs, and team size.

  3. 3

    We meet, finalise the agreement, and begin your project.

Crunchbase badgeClutch badgeGoodFirms badgeTechBehemoths badge