简体   繁体   English

为什么我的环境变量显示为未定义?

[英]Why is my env variable showing up as undefined?

I'm trying to get data from a table with Supabase .我正在尝试使用Supbase从表中获取数据。 When I try store the URL and the Key on a .env file it comes back as undefined.当我尝试将 URL 和密钥存储在 .env 文件中时,它返回为未定义。

File: App.js文件:App.js

import React, { useEffect } from "react";
import ButtonsSection from "./ButtonsSection";
import Header from "./Header";
import InputsSection from "./InputsSection";
import ResultsSection from "./ResultsSection";

export default function App() {
  useEffect(() => {
    console.log(process.env.REACT_APP_SUPABASE_URL);
  }, []);

  return (
    <div>
      <Header />
      <InputsSection />
      <ButtonsSection />
      <ResultsSection />
    </div>
  );
}

File: Database.env文件:Database.env

REACT_APP_SUPABASE_URL='https://**********.supabase.co'
REACT_APP_SUPABASE_ANON_KEY='***************.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InF2bW56a2xka25hZG1wcmZtc3JqIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTU0MDQ4NDQsImV4cCI6MTk3MDk4MDg0NH0.Th_x*******************************'

assuming you are using create-react-app.假设您使用的是 create-react-app。 File name should be ".env" not "Database.env" and it should be in the root folder of the project.文件名应该是“.env”而不是“Database.env”,它应该在项目的根文件夹中。

  • Step 1 - install dotenv npm install dotenv第 1 步 - 安装 dotenv npm install dotenv
  • Step 2 - create a file with no name but with .env extension in the root directory第 2 步 - 在根目录中创建一个没有名称但扩展名为.env的文件
  • Step 3 - import 'dotenv/config' (ES6 Syntax)第 3 步 - 导入“dotenv/config”(ES6 语法)

the problem in your case seems to be that you are not importing dotenv/config and your file name is database.env which is not correct, just name it as .env您的问题似乎是您没有导入dotenv/config并且您的文件名是 database.env 这是不正确的,只需将其命名为.env

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

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