简体   繁体   English

在 create-react-app 项目中使用 dotenv 文件而不弹出

[英]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).我从事两个 react 项目,其中一个使用 create-react-app 并已被弹出,另一个是使用 create-react-app 的新项目(未弹出)。 The first (ejected one) uses custom dotenv files and they work well.第一个(弹出的)使用自定义 dotenv 文件,它们运行良好。

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)?我尝试将 dotenv 添加到第二个的构建中使用自定义的.env.something 文件,但没有成功(即使我使用 crossenv 指定了不同的环境,它也总是选择 .env.local,或者如果我调用 dotenv 它没有找到,大概是因为我没有在全球范围内安装它)?

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. 我在.env文件中包含:NODE_PATH =。/在CRA项目中package.json旁边的文件夹中进行设置,并且可以正常工作。 I use nothing else - no dotenv needed. 我什么也没用-不需要dotenv。

This is how I'm using dotenv with create-react-app without ejecting:这就是我在不弹出的情况下将 dotenv 与 create-react-app 一起使用的方式:

  1. install dotenv-cli (devDependencies)安装 dotenv-cli (devDependencies)

  2. define custom build scripts for different environments in package.json -在 package.json 中为不同环境定义自定义构建脚本 -

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 create-react-app + Webpack 5 不弹出 - create-react-app + Webpack 5 without ejecting 使用create-react-app代码拆分分离CSS文件而不弹出 - Separate CSS files using create-react-app code-splitting without ejecting create-react-app 使用自定义 service worker 而不弹出 - create-react-app use custom service worker without ejecting 有没有办法在不弹出的情况下为 create-react-app 更改 webpack 的 eslint - Is there a way to change the eslint of the webpack for the create-react-app without ejecting 使用 styled-jsx 而不弹出 create-react-app - Use styled-jsx without ejecting create-react-app 使用babel-preset-env进行转换,而无需从create-react-app弹出 - Transpile using babel-preset-env without ejecting from create-react-app 弹出使用create-react-app创建的React应用程序会导致什么损失? - What do you lose by ejecting a React app that was created using create-react-app? 如何在不弹出的情况下将 CopyWebpackPlugin 添加到 create-react-app? - How can I add the CopyWebpackPlugin to create-react-app without ejecting? 我可以在不弹出的情况下更改 create-react-app 中公共文件夹的名称吗? - Can I change the name of the public folder in create-react-app without ejecting? 如何在create-react-app中禁用CSS构建而不弹出? - How can I disable CSS builds in create-react-app without ejecting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM