2021年1月1日星期五

Environment variables undefined using dotenv

I have a file which connects to Contentful using my specific space and accessToken. Recently, I decided to hide the keys into an .env file using the dotenv package. This is what my file looks like :

import {createClient} from 'contentful';  require('dotenv').config();  //console.log('space = ', process.env.CONTENTFUL_SPACE);    const client = createClient({    space: process.env.CONTENTFUL_SPACE,    accessToken: process.env.CONTENTFUL_TOKEN  });    export default client;  

For some reason, the env variables are coming out as undefined. Currently, my .env file is under the root dir. This is the file structure of my project :

project   client    src     service      client.js <-- The file I pasted above   node_modules   .env   package-lock.json   package.json  

Things I've tried :

  1. Uninstalled dotenv and reinstalled it
  2. Deleted node_modules folder and reinstalled all dependencies
  3. Moved .env file under service folder where the client file exists
  4. Used the REACT_APP_* prefix for my env variables in .env file
  5. Specified the path in the config call

All of these solutions came from other stackoverflow questions (please don't mark this as a duplicate). I've been stuck on this for a few days circling through the solutions listed and looking for others but still haven't figured out why I'm not able to access my env variables!

https://stackoverflow.com/questions/65535850/environment-variables-undefined-using-dotenv January 02, 2021 at 11:46AM

没有评论:

发表评论