Firebase 存储在 Express 服务器环境中不起作用
Posted
技术标签:
【中文标题】Firebase 存储在 Express 服务器环境中不起作用【英文标题】:Firebase Storage Not working in Express Server Environment 【发布时间】:2016-09-20 12:36:21 【问题描述】:我目前正在构建一个应用程序并尝试测试我使用 Express 创建的生产服务器环境。该应用程序在通过 webpack-dev-server 提供服务时运行良好,但是当我的 Express server.js
被 webpack 捆绑并运行 node server.bundle.js
时,我收到以下有关 Firebase 存储的错误。
/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:533
var storage = _firebaseConfig2.default.storage();
^
TypeError: _firebaseConfig2.default.storage is not a function
Node 最终只是切换到运行我的开发环境。
下面是我的package.json
,其中包含我的依赖项和 npm 脚本,后面是来自我的终端的日志。
我在 '_firebaseConfig2.default.storage is not a function' 上所做的所有 google/*** 搜索几乎都没有出现。
Package.json
"name": "mtbparks-member-app",
"version": "0.1.0",
"main": "index.html",
"scripts":
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:dev": "webpack-dev-server --inline --content-base build/ --history-api-fallback && postcss --use autoprefixer -d build src/styles.css",
"start:prod": "npm run build && node server.bundle.js",
"build:client": "webpack && postcss --use autoprefixer -d build src/styles.css",
"build:server": "webpack --config webpack.server.config.js",
"build": "npm run build:client && npm run build:server",
"test": "echo \"Error: no test specified\" && exit 1"
,
"license": "ISC",
"devDependencies":
"autoprefixer": "^6.3.6",
"babel-core": "^6.8.0",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.9.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"eslint": "^2.9.0",
"eslint-loader": "^1.3.0",
"eslint-plugin-react": "^5.1.1",
"http-server": "^0.9.0",
"postcss-cli": "^2.5.2",
"postcss-loader": "^0.9.1",
"webpack": "^1.13.0",
"webpack-dev-server": "^1.14.1"
,
"dependencies":
"compression": "^1.6.2",
"express": "^4.13.4",
"firebase": "3.0.2",
"foundation-sites": "^6.2.1",
"if-env": "^1.0.0",
"re-base": "^1.5.1",
"react": "^15.0.2",
"react-dom": "^15.0.2",
"react-router": "^2.4.0"
日志
=> NODE_ENV=production npm start
> mtbparks-member-app@0.1.0 start /Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp
> if-env NODE_ENV=production && npm run start:prod || npm run start:dev
> mtbparks-member-app@0.1.0 start:prod /Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp
> npm run build && node server.bundle.js
> mtbparks-member-app@0.1.0 build /Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp
> npm run build:client && npm run build:server
> mtbparks-member-app@0.1.0 build:client /Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp
> webpack && postcss --use autoprefixer -d build src/styles.css
Hash: cf9ca8bbc1315fc1c9db
Version: webpack 1.13.0
Time: 14624ms
Asset Size Chunks Chunk Names
bundle.js 855 kB 0 [emitted] main
bundle.js.map 4.59 MB 0 [emitted] main
[0] multi main 40 bytes 0 [built]
+ 538 hidden modules
> mtbparks-member-app@0.1.0 build:server /Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp
> webpack --config webpack.server.config.js
Hash: 4561926734ef8e1e5a47
Version: webpack 1.13.0
Time: 973ms
Asset Size Chunks Chunk Names
server.bundle.js 27.6 kB 0 [emitted] main
[0] multi main 40 bytes 0 [built]
+ 17 hidden modules
/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:533
var storage = _firebaseConfig2.default.storage();
^
TypeError: _firebaseConfig2.default.storage is not a function
at Object.defineProperty.value (/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:533:41)
at __webpack_require__ (/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:20:30)
at Object.defineProperty.value (/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:179:14)
at __webpack_require__ (/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:20:30)
at Object.<anonymous> (/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:83:16)
at Object.<anonymous> (/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:121:31)
at __webpack_require__ (/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:20:30)
at Object.<anonymous> (/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:48:19)
at __webpack_require__ (/Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:20:30)
at /Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/server.bundle.js:40:18
npm ERR! Darwin 14.5.0
npm ERR! argv "/Users/palcisto/.nvm/versions/node/v5.11.1/bin/node" "/Users/palcisto/.nvm/versions/node/v5.11.1/bin/npm" "run" "start:prod"
npm ERR! node v5.11.1
npm ERR! npm v3.8.6
npm ERR! code ELIFECYCLE
npm ERR! mtbparks-member-app@0.1.0 start:prod: `npm run build && node server.bundle.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the mtbparks-member-app@0.1.0 start:prod script 'npm run build && node server.bundle.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the mtbparks-member-app package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! npm run build && node server.bundle.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs mtbparks-member-app
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls mtbparks-member-app
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/npm-debug.log
> mtbparks-member-app@0.1.0 start:dev /Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp
> webpack-dev-server --inline --content-base build/ --history-api-fallback && postcss --use autoprefixer -d build src/styles.css
http://localhost:8080/
webpack result is served from /
content is served from /Users/palcisto/Sites/sandbox/MTBparks/MTBparksMemberApp/build
404s will fallback to /index.html
Hash: e38d4972b9458e39b46c
Version: webpack 1.13.0
Time: 16461ms
Asset Size Chunks Chunk Names
bundle.js 928 kB 0 [emitted] main
bundle.js.map 5.17 MB 0 [emitted] main
【问题讨论】:
【参考方案1】:同意,这似乎违背了 Firebase 的全部目的。一方面,某些地方的官方文档说“......对于 Firebase Node.JS 的某些功能是必需的......”(或类似的)但在这种情况下我们不能使用 Node.JS。
【讨论】:
【参考方案2】:此处为 Firebase 存储开发人员:
Firebase 存储目前仅支持浏览器 JS。由于Google Cloud Storage 已经提供了许多高质量的客户端库,我们不想重新实现另一个——我们建议使用GCloud-Node 库进行此类开发。您可以将其与现有存储桶和典型的 Firebase Server 开发设置说明一起使用。
【讨论】:
谢谢迈克。那问你一个问题。由于我在我的主 bundle.js 中使用包含我所有的 react 应用程序 JS 的浏览器版本,我认为它会工作。可能是因为我使用 Express 渲染 React 代码服务器端,它不再被视为浏览器 JS 吗?我对 Webpack、Express、React 甚至 Firebase 都很陌生(上周刚开始(结束))。 这很混乱,NodeJS API 应该像数据库 API 一样匹配浏览器 API。 我同意 API 的不同有点令人困惑,我们正在研究各种选项,以在未来使它们更加相似。以上是关于Firebase 存储在 Express 服务器环境中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Express 中使用带有 Firebase 功能的 webpack-hot-server-middleware
firebase node.js/express.js 添加多个firebase函数