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 :
- Uninstalled
dotenv
and reinstalled it - Deleted
node_modules
folder and reinstalled all dependencies - Moved
.env
file underservice
folder where theclient
file exists - Used the
REACT_APP_*
prefix for my env variables in.env
file - 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!
没有评论:
发表评论