简体   繁体   中英

Using dotenv files in create-react-app project without ejecting

I work on two react projects, one of which used create-react-app and has since been ejected, the other one is new using create-react-app (not ejected). The first (ejected one) uses custom dotenv files and they work well.

I've tried adding dotenv to use customized.env.something files to the build of the second without success (it either always picks up.env.local even if I specify a different environment using crossenv, or if I call dotenv it's not found, presumably because I haven't installed it globally)?

Does anyone know if this is possible to do without ejecting, and if so, can you show how? I'd like to avoid ejecting as long as possible.

Thanks!

John

I have .env file with: NODE_PATH=./ setting in folder next to package.json in CRA project and it works. I use nothing else - no dotenv needed.

This is how I'm using dotenv with create-react-app without ejecting:

  1. install dotenv-cli (devDependencies)

  2. define custom build scripts for different environments in package.json -

    "build-dev": "dotenv -e.env.development react-scripts build",

    "build-prod": "dotenv -e.env.production react-scripts build",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM