[英]Using dotenv module with React
我的應用程序dependencies
中有dotenv 。 按照說明,我有一個.env
文件,里面是NODE_ENV=development
。
在我的main.js
文件中,我正在運行一個簡單的require('dotenv').config();
但是當我訪問process.env
,我希望有一個對象,並且我得到{}
我在這里做錯了什么? 謝謝
dotenv
僅在服務器dotenv
作用。 要將.env
與webpack .env
在客戶端中使用,請使用dotenv-webpack 。
yarn add dotenv-webpack -D
或npm install dotenv-webpack --save
將其添加到您的Webpack配置文件。
// webpack.config.js const Dotenv = require('dotenv-webpack'); module.exports = { ... plugins: [ new Dotenv({ path: './.env', // Path to .env file (this is the default) safe: true // load .env.example (defaults to "false" which does not use dotenv-safe) }) ] ... };
將.env
添加到您的.gitignore
文件
注意:您的.env文件可以包含敏感信息。 因此,
dotenv-webpack
只會將在代碼中明確引用的環境變量暴露給最終的包。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.