简体   繁体   English

在 firebase 配置中反应环境变量错误“意外令牌”

[英]React environment variables error 'Unexpected token' in firebase config

I'm running a react project with a backend in firebase and I'm trying to setup a.env file for the configuration settings.我正在运行一个带有 firebase 后端的反应项目,我正在尝试为配置设置设置一个 .env 文件。

When I run 'npm start', I get a parsing error: Unexpected token, expected "," after the word 'process' inside the 'const firebaseConfig' settings.当我运行“npm start”时,我得到一个解析错误:Unexpected token, expected "," 在“const firebaseConfig”设置中的“process”一词之后。

在此处输入图像描述

Here are the pertinent files:以下是相关文件:

firebase-config.js firebase-config.js

import { initializeApp } from "firebase/app";
import { getFirestore } from "@firebase/firestore";

const firebaseConfig = {
    apiKey: {process.env.REACT_APP_API_KEY} //error occurs at the dot '.' after the word 'process'
    authDomain: {process.env.REACT_APP_AUTHDOMAIN}
    projectId: {process.env.REACT_APP_PROJECTID}
    storageBucket: {process.env.REACT_APP_STORAGE_BUCKET}
    messagingSenderId: {process.env.REACT_APP_MESSAGING_SENDER_ID}
    appId: {process.env.REACT_APP_APP_ID}
    measurementId: {process.env.REACT_APP_MEASUREMENT_ID}

};

const app = initializeApp(firebaseConfig);
export const db = getFirestore(app)

index.js index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

reportWebVitals();

.env (in the root folder) .env(在根文件夹中)

REACT_APP_API_KEY="<the actual firebase config settings here>"
REACT_APP_AUTHDOMAIN="<the actual firebase config settings here>"
REACT_APP_PROJECTID="<the actual firebase config settings here>"
REACT_APP_STORAGE_BUCKET="<the actual firebase config settings here>"
REACT_APP_MESSAGING_SENDER_ID="<the actual firebase config settings here>"
REACT_APP_APP_ID="<the actual firebase config settings here>"
REACT_APP_MEASUREMENT_ID="<the actual firebase config settings here>"

package.json package.json

{
  "name": "react_tut",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@firebase/firestore": "^3.4.2",
    "dotenv": "^10.0.0",
    "env-cmd": "^10.1.0",
    "firebase": "^9.6.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "^5.0.0",
    "web-vitals": "^2.1.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Thanks in advance.提前致谢。

JavaScript's ES6 syntax lets you do this: JavaScript 的 ES6 语法允许你这样做:

const a = 4;
const x = { z: { a } };

but not this:但不是这个:

const a = { b: 4 };
const x = { z: { a.b } };

To get it to work, do为了让它工作,做

const a = { b: 4 };
const x = { z: { b: a.b } };

Solution (stuff should not be in curly brackets):解决方案(东西不应该在大括号中):

const firebaseConfig = {
    apiKey: process.env.REACT_APP_API_KEY,
    authDomain: process.env.REACT_APP_AUTHDOMAIN,
    projectId: process.env.REACT_APP_PROJECTID,
    storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
    messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
    appId: process.env.REACT_APP_APP_ID,
    measurementId: process.env.REACT_APP_MEASUREMENT_ID

};

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

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