我试图隐藏我的API密钥,当我提交到GitHub,我已经通过论坛的指导,特别是下面的帖子:

我如何隐藏一个API密钥在创建反应应用程序?

我做了修改并重新启动Yarn。我不确定我做错了什么-我添加了一个。env文件到我的项目的根(我命名为process.env),在文件中我只是把REACT_APP_API_KEY = 'my-secret-api-key'。

我想这可能是我在App.js中添加关键字的方式,我尝试了多种格式,包括不使用模板文字,但我的项目仍然不会编译。

performSearch = (query = 'germany') => { 查询https://api.unsplash.com/search/photos?query=获取(“$ {}client_id = $ {REACT_APP_API_KEY}’) .then(response => response.json()) .then(responseData => { this.setState ({ 结果:responseData.results, 加载:假 }); }) .catch(error => { console.log('读取和解析数据出错',Error); }); }


当前回答

Install dotenv as devDependencies: npm i --save-dev dotenv Create a .env file in the root directory: my-react-app/ |- node-modules/ |- public/ |- src/ |- .env |- .gitignore |- package.json |- package.lock.json. |- README.md Update the .env file like below & REACT_APP_ is the compulsory prefix for the variable name. REACT_APP_BASE_URL=http://localhost:8000 REACT_APP_API_KEY=YOUR-API-KEY [ Optional but Good Practice ] Now you can create a configuration file to store the variables and export the variable so can use it from others file. For example, I've create a file named base.js and update it like below: export const BASE_URL = process.env.REACT_APP_BASE_URL; export const API_KEY = process.env.REACT_APP_API_KEY; Or you can simply just call the environment variable in your JS file in the following way: process.env.REACT_APP_BASE_URL

其他回答

在CREATE REACT APP中使用环境变量的步骤(不含dotenv包)

Create a new file called .env in the root folder of the project (NOT inside src folder but one level up. Remember, it should be at the same level as package.json (THIS IS VERY IMPORTANT!!) Define your variables like so (Note that every variable you define should start with REACT_APP_) Example : .env file REACT_APP_ACCESS_KEY=8Sh9ZLwZevicWC-f_lmHvvyMu44cg3yZBU Note: You don't have to enclose the value in "" or '' Now you can use the variable in any of your components like so const apiKey = process.env.REACT_APP_ACCESS_KEY The name should match the key given in the .env file Now before you try this out, always remember to restart the local server. Once you run npm start it works. This step applies whenever you make changes to the .env file. We generally forget this step so it might not work. Optionally, check if .env entry is present in .gitignore file. If the entry of .env exists in .gitignore then your .env file will not be pushed to github(This is the reason why we use .env in the first place).

每个人都没有定义的解决方案是把你的。env文件放在根目录,例如:

项目名称/ src .env

不要在src文件夹中创建它。在应用程序的根目录中创建它。

它认为你在src文件夹中创建了文件,因为我也只在那里创建了它…然后我才意识到这是错误的,所以我在src的外部创建了.env文件。它会起作用的。

四个步骤

NPM安装dotenv 接下来,添加以下行到您的应用程序。 要求(' dotenv ') . config () 然后在应用程序的根目录下创建一个.env文件,并将变量添加到其中。

// contents of .env

REACT_APP_API_KEY = 'my-secret-api-key'

最后,将.env添加到你的.gitignore文件中,这样Git就会忽略它,它永远不会出现在GitHub上。

如果你正在使用Create React App (Create - React - App),那么你只需要第3步和第4步,但请记住,变量需要以REACT_APP_开始才能工作。

参考:添加自定义环境变量

注意-在.env文件中添加变量后,需要重新启动应用程序。

参考:使用dotenv包创建环境变量

Webpack用户

如果你正在使用Webpack,你可以安装和使用dotenv-webpack插件。要做到这一点,请遵循以下步骤:

安装包

yarn add dotenv-webpack

创建一个.env文件

// .env
API_KEY='my secret api key'

将它添加到webpack.config.js文件中:

// webpack.config.js
const Dotenv = require('dotenv-webpack');

module.exports = {
  ...
  plugins: [
    new Dotenv()
  ]
  ...
};

在代码中使用它

process.env.API_KEY

有关更多信息和配置信息,请访问dotenv-webpack。

Install dotenv as devDependencies: npm i --save-dev dotenv Create a .env file in the root directory: my-react-app/ |- node-modules/ |- public/ |- src/ |- .env |- .gitignore |- package.json |- package.lock.json. |- README.md Update the .env file like below & REACT_APP_ is the compulsory prefix for the variable name. REACT_APP_BASE_URL=http://localhost:8000 REACT_APP_API_KEY=YOUR-API-KEY [ Optional but Good Practice ] Now you can create a configuration file to store the variables and export the variable so can use it from others file. For example, I've create a file named base.js and update it like below: export const BASE_URL = process.env.REACT_APP_BASE_URL; export const API_KEY = process.env.REACT_APP_API_KEY; Or you can simply just call the environment variable in your JS file in the following way: process.env.REACT_APP_BASE_URL