gpt4 book ai didi

npm - webpack导入firebase不起作用

转载 作者:行者123 更新时间:2023-12-04 00:30:17 25 4
gpt4 key购买 nike

我在让 firebase 3.0.1 工作时遇到问题。我感觉这与我的 webpack 设置有关。我的文件在下面。使用 webpack 开发服务器运行我的应用程序时,出现错误:

Uncaught TypeError: firebase.initializeApp is not a function



有趣的是,如果我把 debugger;var firebase = require('firebase'); 之后的断点它似乎是一个空对象。

webpack.config.js
const webpack = require("webpack");

module.exports = {
entry: './src/index.js',
output: {
path: 'public',
filename: 'bundle.js'
},
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader?presets[]=es2015&presets[]=react'
}]
},
plugins: process.env.NODE_ENV === 'production' ? [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin()
] : []
};

包.json
{
"name": "burn",
"version": "1.0.0",
"description": "burn messaging",
"main": "index.js",
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:dev": "webpack-dev-server --inline --content-base public --history-api-fallback",
"start:prod": "webpack && firebase deploy"
},
"author": "James Gilchrist <james@burn.today>",
"license": "ISC",
"dependencies": {
"compression": "^1.6.2",
"express": "^4.13.4",
"firebase": "^3.0.1",
"if-env": "^1.0.0",
"react": "^15.0.2",
"react-dom": "^15.0.2",
"react-router": "^2.4.0"
},
"devDependencies": {
"babel-core": "^6.9.0",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"webpack": "^1.13.0",
"webpack-dev-server": "^1.14.1"
}
}

index.js
var firebase = require('firebase');

var config = {
apiKey: "AIzaSyA9gUmSBu4SZ4P9H_4lXuN1ouD_GBKq3aw",
authDomain: "burn-56840.firebaseapp.com",
databaseURL: "https://burn-56840.firebaseio.com",
storageBucket: "burn-56840.appspot.com"
};
firebase.initializeApp(config);

最佳答案

我有 same problem ,不过有一个简单的解决方法:

var firebase = require('firebase/app');

这样你就得到了“真正的”firebase 模块。但是,您现在必须要求您需要的每个模块,以便正确加载,如下所示:
var firebase = require('firebase/app');
// all 3 are optional and you only need to require them at the start
require('firebase/auth');
require('firebase/database');
require('firebase/storage');

在我看来,当前的初始化代码有问题,看看它应该工作的源代码;但话又说回来,有点像你,我正在使用 browserify,并且没有在它之外进行测试,所以它可能是相关的。

关于npm - webpack导入firebase不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37337146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com