admin管理员组文章数量:1180462
I built a project using React at the front and Strapi (based on Node.js) at the back.
On the client side, I'm trying to access my api url like so:
const apiUrl = process.env.API_URL || 'http://localhost:1337'
const strapi = new Strapi(apiUrl);
But process.env.API_URL is undefined. If I log process.env all I get is an object containing:
NODE_ENV: "development"
PUBLIC_URL: ""
How can I access my api_url ? I'm guessing there is a file where I should define API_URL myself?
I built a project using React at the front and Strapi (based on Node.js) at the back.
On the client side, I'm trying to access my api url like so:
const apiUrl = process.env.API_URL || 'http://localhost:1337'
const strapi = new Strapi(apiUrl);
But process.env.API_URL is undefined. If I log process.env all I get is an object containing:
NODE_ENV: "development"
PUBLIC_URL: ""
How can I access my api_url ? I'm guessing there is a file where I should define API_URL myself?
Share Improve this question asked Jun 5, 2019 at 14:21 sir-haversir-haver 3,59210 gold badges56 silver badges108 bronze badges 3 |5 Answers
Reset to default 22Assuming you are using create react app, Make sure you add the prefix REACT_APP like REACT_APP_API_URL
. In your project src create a file .env.development
or .env.production
and add the your value with a key with the prefix
In client side there is no such concept as environment variables. So, if you didn't do anything to solve this issue, it won't be defined.
Create react app has an option to pass environment variables from .env file. If you are using it you can check this document: https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables
If you don't have react-create-app, you can use dotenv npm package.
In essence, what dotenv and react-create-app does, will be declaring those variables for you in build time. 'Build time' here is important, because it's not like a server. You just build 1 application for 1 environment. You cannot use same build to deploy to different environments.
It is important to understand what is happening behind the scenes.
Your project can consume variables declared in your environment as if they were declared locally in your JS files.
By default you will have NODE_ENV
defined for you, and any other environment variables starting with REACT_APP_
.
You must create custom environment variables beginning with REACT_APP_
.
Any other variables except NODE_ENV
will be ignored to avoid accidentally exposing a private key on the machine that could have the same name.
Changing any environment variables will require you to restart the development server if it is running.
These environment variables will be defined for you on process.env
.
For example, having an environment variable named REACT_APP_NOT_SECRET_CODE
will be exposed in your JS as process.env.REACT_APP_NOT_SECRET_CODE
.
Read more in here and in here.
process.env
is a Object
and if you want add a property to an object.
process.env['API_URL'] = 'http://blabla.com:3000';
console.log(process.env);
Output
{
...
API_URL: "http://blabla.com:3000",
...
TERM_PROGRAM_VERSION: '1.34.0',
LANG: 'en_US.UTF-8'
}
For those who are using Next.js Client Components: prefix your environment variables with NEXT_PUBLIC_
.
https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables
本文标签: javascriptprocessenvAPIURL is undefinedStack Overflow
版权声明:本文标题:javascript - process.env.API_URL is undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738139071a2065605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
API_URL
envvar? – keul Commented Jun 5, 2019 at 14:25