Logo

dev-resources.site

for different kinds of informations.

How to use `.env` file v:20.6.0 `dotenv` npm package do not use.

Published at
12/27/2023
Categories
node
javascript
dotenv
programming
Author
zobaidulkazi
Author
12 person written this
zobaidulkazi
open
How to use `.env` file v:20.6.0 `dotenv` npm package do not use.

package.json file


  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon --env-file=.env index.js",
    "start": "node --env-file=.env index.js",
    "prod": "node --env-file=.env.prod index.js",
    "local": "node --env-file=.env.local index.js",
    "build": "node --env-file=.env build.js",
    "clean": "node --env-file=.env clean.js"
  },
Enter fullscreen mode Exit fullscreen mode

Create A new Project

mkdir test_env
Enter fullscreen mode Exit fullscreen mode

cd test_env

open terminal and write this commend

npm init -y
Enter fullscreen mode Exit fullscreen mode
touch .env

Enter fullscreen mode Exit fullscreen mode

write code .env file

# write code .env file
PORT = 3333
MONGO_DB_URL = mongodb+srv://<username>:<password>@<cluster>.mongodb.net/<database>?retryWrites=true
PRIVATE_KEY = <private_key>
Enter fullscreen mode Exit fullscreen mode
touch index.js
Enter fullscreen mode Exit fullscreen mode
import express from "express";
import mongoose from "mongoose";

const app = express();


app.listen(process.env.PORT | 3000, () => {
    console.log(` Listening on port ${process.env.PORT}`)
})

mongoose.connect(process.env.MONGO_DB_URL, {
    dbName: "test",
    authSource: "admin"
})

.then(() => {
    console.log("Connected to MongoDB");
})
.catch((err) => {
    console.log(err);
})

Enter fullscreen mode Exit fullscreen mode
dotenv Article's
30 articles in total
Favicon
Load Environment Variables using dotenv-local
Favicon
Hashicorp Vault Agent Tutorial: Generating .env from Vault Secrets
Favicon
Flutter Web | Build with .env File
Favicon
Learn .env in Express.js for Beginners (Effortless Setup)
Favicon
How to Hide Only API Keys Instead of Entire Files on GitHub and From Its Commit History
Favicon
Practical Introduction to Environment Variables Using Node.js
Favicon
From dotenv to dotenvx: Next Generation Config Management
Favicon
How to use `.env` file v:20.6.0 `dotenv` npm package do not use.
Favicon
Community Spotlight: David Cochrum
Favicon
Node.js 20.6.0 includes built-in support for .env files
Favicon
What is a .env.vault file
Favicon
Environment variables and configuration anti patterns in Node.js applications
Favicon
Dotenv: Python app environment variable vs. Linux environment variable
Favicon
Node.js includes built-in support for .env files
Favicon
How does python-dotenv simplify Configuration Management?
Favicon
Env::Dot
Favicon
How do you set up .env variables in your NextJS project ?
Favicon
Using ENV file in React & Webpack
Favicon
A simple trick for your dotenv files
Favicon
dotenv and typescript
Favicon
Environment variables & Its best practices
Favicon
Password Manage your environment and secrets with bitwarden
Favicon
5 reasons why your .env environment variables don't work
Favicon
Creating a DotEnv Loader in PHP
Favicon
NextJS - Get rid of DotENV
Favicon
Setting-up a Django project for production
Favicon
Stop using Dotenv in your front-end
Favicon
Supercharge your .env powered projects!
Favicon
Ways to load env variables for your script
Favicon
Doppler: The Triumph and Tragedy of .env Files

Featured ones: