创建反应应用程序不拿起.env文件? [英] create react app not picking up .env files?

查看:57
本文介绍了创建反应应用程序不拿起.env文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用创建反应应用来引导我的应用。

I am using create react app to bootstrap my app.

我添加了两个 .env 文件 .env.development .env.production 放在根中。

I have added two .env files .env.development and .env.production in the root.

我的 .env.development 包括:

API_URL=http://localhost:3000/api
CALLBACK_URL=http://localhost:3005/callback

当我使用 react-scripts start运行我的应用程序时并退出 process.env 它弹出

{ NODE_ENV: "development", PUBLIC_URL: "" }

我尝试了不同的方法,但是它只是没有在我的开发文件中获取veriables,我在做什么错了?!

I've tried different things, but its just not picking up the veriables in my development file, what am I doing wrong?!

目录结构是:

/.env.development
/src/index.js

Package.json脚本是:

Package.json script is:

"start": "export PORT=3005; npm-run-all --parallel server:start client:start",
    "client:start": "export PORT=3005; react-scripts start",
    "server:start": "node server.js",
    "build": "react-scripts build",

编辑:

@jamcreencia正确指出了我的变量应该加上 REACT_APP 作为前缀。

@jamcreencia correctly pointed out my variables should be prefixed with REACT_APP.

编辑2

如果我将文件命名为 .env ,它可以正常工作,但是如果我使用 .env.development,则不可以。 / code>或 .end.production

It works okay if I name the file .env but not if I use .env.development or .end.production

推荐答案

create-react-app ,您需要在变量名前加上 REACT_APP _ 才能访问它。

With create-react-app, you need to prefix REACT_APP_ to the variable name to be able to access it.

示例:

REACT_APP_API_URL=http://localhost:3000/api
REACT_APP_CALLBACK_URL=http://localhost:3005/callback

在此处查看更多信息

这篇关于创建反应应用程序不拿起.env文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆