Environment Variables from .env file in NextJS

Search for a command to run...

No comments yet. Be the first to comment.
Recently I started building tools that will help me automate my app deployment process easier and cheaper, to do that I knew CLIs are definitely going to be involved one way or another. So I started building two CLIs: Simp CLI: It's like Makefile ju...

Let's jump into it... fast. You got here because you want to setup Dokku and the other ways don't work... let's goooo I will setup for Dokku only Point your domain to the VPS of choice Spin up new VPS server here: Digital Ocean Linode Buy a dom...

So what is Dokku, another bit, another TL;DR; Let's jump into it. You might know Heroku, (my opinion) the greatest platform to ever make it easy to deploy server and client-side applications so fast and easy. Well Heroku is a business and they have t...

Okay for this one I highly recommend, the speeds is just amazing. I loved it. I am gonna do a comparison real quick, between browser-image-compression and react-image-file-resizer. browser-image-compressionreact-image-file-resizer Really fastCo...

I recently got interested in compressing images on the client-side for a couple of reasons: Reduce my cloudinary bandwidth and storage usage Users were complaining about long waits while uploading images So I ventured to look for a library that wil...

If you started building your application with next and you want it to access environment variables in your .env file here is away.
.env fileThere are a number of reasons why you should have a .env file when working on an application, they are:
.env file can be used to store critical and sensitive credentials such as access_tokens, secret_keys or api_keys thus ensuring they are not exploited by a hacker prying on your repo..env file will you help in sorting out varying app configurations in different deploys(development, production or staging) such as:Normally accessing environment variables with Node you use process.env and in your terminal, you will have to export your environment variable to be accessed by Node. Example:
$ export PRODUCTION_URL="https://werick.codes/"
I can now access the environment variable with Node
process.env.PRODUCTION_URL
Also for a Node application to get all variables in the .env file automatically without the export command in the terminal you will need to install a dotenv library.
Install dotenv
$ npm install dotenv
//or
$ yarn add dotenv
Open the next.config.js, type this in your editor
const webpack = require("webpack");
// Initialize doteenv library
require("dotenv").config();
module.exports = {
webpack: config => {
// Fixes npm packages that depend on `fs` module
config.node = {
fs: 'empty'
}
/**
* Returns environment variables as an object
*/
const env = Object.keys(process.env).reduce((acc, curr) => {
acc[`process.env.${curr}`] = JSON.stringify(process.env[curr]);
return acc;
}, {});
/** Allows you to create global constants which can be configured
* at compile time, which in our case is our environment variables
*/
config.plugins.push(new webpack.DefinePlugin(env));
return config
}
}
Thats all you need for next to environment variables without exporting them on the terminal.
You can read more about reduce() method here and about the DefinePlugin() method here