在Firebase中未定义require? [英] require is not defined in Firebase?
问题描述
我正在尝试在Firebase登台环境中运行html文件.我在js代码中使用了 Firebase npm软件包.但是,在浏览器中运行时,它会引发错误未定义要求".
I am trying to run a html file in Firebase staging environment. I have used Firebase npm package in the js code. But while running in the browser it throws error "require is not defined".
HTML代码:
<html>
<body>
<h1>Hello World</h1>
</body>
<script type="text/javascript" src="./js/test.js"></script>
</html>
JS代码:
var Firebase = require('firebase');
var dataRef = new Firebase('firebase url');
console.log("Firebase : "+Firebase+" -- dataRef :: "+dataRef)
dataRef.set("Firebase Require");
请给我建议一些解决方法.
Please suggest me some solution.
推荐答案
您需要设置工具(例如 webpack)来管理您的依赖项.这样,您就可以直接在 .js
文件中要求
(或 import
es6 sintax)库.
You need to setup a tool (such as webpack) to manage your dependencies. In this way you will be able to require
(or import
es6 sintax) libraries directly in your .js
files.
可能的设置如下:
在 webpack.config.js
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js'
}
}
告诉webpack哪个是您应用的 entry
文件,并让它为您构建 bundle
.
To tell webpack which is the entry
file of your app and let it build the bundle
for you.
然后在 index.js
中使用:
import Firebase from 'firebase';
var dataRef = new Firebase('firebase url');
console.log("Firebase : "+Firebase+" -- dataRef :: "+dataRef)
dataRef.set("Firebase Require");
请参阅 https://webpack.js.org/configuration/以获得更完整的信息指南.
Refer to https://webpack.js.org/configuration/ for a more complete guide.
p.s.对于具有前端支持的每个节点依赖项都是有效的
p.s. that will be valid for every node dependecies with frontend support
这篇关于在Firebase中未定义require?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!