尝试设置.env文件时,“require is not defined”错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试设置.env文件时,“require is not defined”错误相关的知识,希望对你有一定的参考价值。
我正在尝试设置一个.env
文件,以便在vanilla JS项目中将凭据存储到我的Firebase数据库中。我已经关注了几个教程和dotenv的文档,但我仍然得到一个错误说Uncaught ReferenceError: require is not defined
。我错过了什么?
这是我在<script>
文件中的index.html
tags中所拥有的内容。凭证在.env
。
require('dotenv').config();
// Initialize Firebase
var config = {
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
databaseURL: process.env.DATABASE_URL,
projectId: process.env.PROJECT_ID,
storageBucket: process.env.STORAGE_BUCKET,
messagingSenderId: process.env.MESSAGING_SENDER_ID
};
firebase.initializeApp(config);
答案
如果你想在browserify等浏览器使用工具中使用require,浏览器没有require函数
dotenv是节点模块,需要fs模块读取.env文件,你不能在浏览器中使用它
以上是关于尝试设置.env文件时,“require is not defined”错误的主要内容,如果未能解决你的问题,请参考以下文章
在 elasticsearch.yml 配置文件中使用 env 时如何设置默认值?
rustdiesel-cli 为不同环境设置多个 env 文件