DEV Community

Harshad Prajapati
Harshad Prajapati

Posted on

Node.js and Express.js with env variable

Hey There!

Just a sharing my knowledge on below
Topic: Node.js, Express.js

Where I used
Package dotenv

Great! It looks like to know more about this...

I got one requirement, Where I have run my Node.js App with env variable.

So with the help of dotenv package, I achieved this

Where you have to follow these steps;

Add Package
npm install dotenv --save (will add package on your project)
Change in package.json file
node -r dotenv/config your_script.js (change your package.json start or dev script what you want trigger)
Add .env file
add env file in the root of your project and add your favorite variable

That's it run npm start (or npm run dev as per your script).

You will see that your app running with provided .env file's variable

So while working with this I extracted basic code to run Node.js app with env and pushed it to Github.

https://github.com/iamharshad/beginner-node-express-env-port

Top comments (1)

Collapse
 
iamjaydev profile image
iamjaydev

I've found this as helpful
Thanks 🙏