Skip to content

Instantly share code, notes, and snippets.

@Gaurav8757
Last active July 6, 2024 08:20
Show Gist options
  • Save Gaurav8757/e9c6c93443feee1f596f358312495cea to your computer and use it in GitHub Desktop.
Save Gaurav8757/e9c6c93443feee1f596f358312495cea to your computer and use it in GitHub Desktop.
Environment Variable

Node.js Environment

  1. CommonJS
npm install dotenv
const dotenv = require("dotenv").config();
const apiKey = process.env.API_KEY;
  1. ModuleJS (ES6)
npm install dotenv
import dotenv from "dotenv";
dotenv.config();
const apiKey = process.env.API_KEY;

React Library

  1. CommonJS
npm install dotenv
const dotenv = require("dotenv").config();
const apiKey = process.env.REACT_APP_API_KEY;
  1. ModuleJS
npm install dotenv
import dotenv from "dotenv";
dotenv.config();
const apiKey = process.env.REACT_APP_API_KEY;

Vite Environment

  1. ModuleJS
const { VITE_API_KEY } = import.meta.env;
const apiKey = VITE_API_KEY;

text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment