button-icon

Entrar

Entrar
Archi's Academy
    Cursos
    Cursos
    #
  • Projetos
    Projetos
  • Archi's Academy

    Trilhas

    #
  • Blog
    Blog
  • Precos
    Precos
  • Contato
    Contato
  • Para clubes universitarios
    Para clubes universitarios

BLACK FRIDAY

85% de desconto durante todo novembro

whatsapp
Entre em contato
Archi's Academy

Navegacao

  • Cursos
  • Projetos
  • Blog
  • Precos
  • Para clubes universitarios
  • Contato

Cursos

    Trilhas

    • Desenvolvimento Frontend
    • Desenvolvimento Backend
    • Garantia de Qualidade (QA)
    • Programacao com IA Agente e LLMs
    • Desenvolvimento Mobile
    • DevOps

    Legal

    • Politica de Privacidade
    • Termos de Servico

    Contato

    +1 (217) 200 90 93
    Suite No: 8, 400 Emmet Street
    Kissimmee, Florida 34741 USA
    [email protected]

    Copyright © Tech Career Yazılım Danışmanlık A.Ş. 2026

    instagramlinkedingithubyoutubexfacebook
    visamastercardstripeiyzicoamerican-express
    ETBIS
    1. Home›
    2. Blog›
    3. How to set Environment Variable in NodeJS?

    Software Development

    Coding

    How to set Environment Variable in NodeJS?

    One of the best ways to use sensitive information in open source repositories without hard-coding the information within publicly available repositories is setting environment variables.
    Environment Variables are variables that are set by the Operating System. They are decoupled from application logic. They can be accessed from applications and programs through various APIs. There is a Node.js library called dotenv that helps you manage and load environment variables. Dotenv is a module that loads environment variables from a .env file into process.env. In Node.js, process.env is a global variable that is injected during runtime. When we set an environment variable, it is loaded into process.env during runtime and can later be accessed.
    Now we can see how it is done.
    dotenv can be added to your Node.js project by installing it using npm or yarn:
    npm install dotenv
    or with Yarn
    yarn add dotenv
    As early as possible in your application, require and configure dotenv.
    require('dotenv').config()
    Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE. For example:
    DB_HOST=localhost
    After you set the Environment variable, now your question is how you can access that environment variable? It is easy to access the variable in your project. process.env now has the keys and values you defined in your .env file. Now it can be accessed using
    process.env.DB_HOST
    Surumi-skill-training

    Surumi Riju

    Perşembe, Haz 3, 2021

    Pronto para transformar conhecimento em habilidades reais?

    Comece a construir com formacao guiada por projetos e ganhe experiencia pratica desde o primeiro dia.

    TOC

    Table of Content

    No sections found