I'm building an application with NextJS and importing .env
from dotenv
has been causing an fs
error while using require
.
I resolved this by creating a env-config.js
, storing the private variables accordingly, and then specifying env.config.js
in my .gitignore
file.
Is this secure? What's the lower level difference between .env
and .js
storage? I assume the variables are stored differently hence the purpose of the .env
file?
I am a noob so apologies in advance!
EDIT
Here is how the FS error is generated:
import {useAddress, useContract} from "@thirdweb-dev/react";
import {useEffect, useState} from "react";
// import envConfig from "../env-config"; // my work around!
require("dotenv").config()
export default function Home() {
const address = useAddress();
const editionDrop = useContract(process.env.EDITION_DROP_ADDRESS, "edition-drop").contract;
...
}
Produced:
Failed to compile
./node_modules/dotenv/lib/main.js:1:0
Module not found: Can't resolve 'fs'
Import trace for requested module:
./pages/index.js
https://nextjs.org/docs/messages/module-not-found
Removing the require("dotenv").config()
caused the error to go away hence the .js
solution to get the hidden variables