无法为 VueJS + NodeJS 安装 Firebase

Posted

技术标签:

【中文标题】无法为 VueJS + NodeJS 安装 Firebase【英文标题】:Cannot install Firebase for VueJS + NodeJS 【发布时间】:2020-07-18 14:52:33 【问题描述】:

我正在使用 vueJS 使用 Firebase 构建身份验证(登录)页面,每次运行时:npm run serve 我都有同样的错误要求我安装 Firebase:`Failed to compile with 7 errors 10:44:30上午

未找到这些依赖项:

./src/main.js 中的@firebase firebase 在 ./src/components/firebaseInit.js, ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components /Login.vue 和其他 3 人 ./src/components/firebaseInit.js 中的 firebase/firestore。 并要求我安装 firebase:npm install --save @firebase firebase firebase/firestore。我做了很多次,但仍然是同样的错误。

这是我的 package.JSON:

 
  "name": "Project",
  "version": "1.0.0",
  "description": "A Vue.js project",
  "author": "",
  "private": true,
  "scripts": 
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d dist"
  ,
  "dependencies": 
    "firebase": "^7.13.1",
    "node-pre-gyp": "^0.14.0",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1"
  ,
  "devDependencies": 
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-loader": "^7.1.1",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-stage-2": "^6.22.0",
    "chalk": "^2.0.1",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "eventsource-polyfill": "^0.9.6",
    "extract-text-webpack-plugin": "^3.0.0",
    "file-loader": "^1.1.4",
    "friendly-errors-webpack-plugin": "^1.6.1",
    "gh-pages": "^1.1.0",
    "html-webpack-plugin": "^2.30.1",
    "node-notifier": "^5.1.2",
    "optimize-css-assets-webpack-plugin": "^3.2.0",
    "ora": "^1.2.0",
    "portfinder": "^1.0.13",
    "postcss-import": "^11.0.0",
    "postcss-loader": "^2.0.8",
    "rimraf": "^2.6.0",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "uglifyjs-webpack-plugin": "^1.1.1",
    "url-loader": "^0.5.8",
    "vue-loader": "^13.3.0",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.5.2",
    "webpack": "^3.6.0",
    "webpack-bundle-analyzer": "^2.9.0",
    "webpack-dev-server": "^2.9.1",
    "webpack-merge": "^4.1.0"
  ,
  "engines": 
    "node": ">= 4.0.0",
    "npm": ">= 3.0.0"
  ,
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]

节点-v = 16.12.1

npm -v =6.14.4

有什么想法吗?

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";
import firebase from "@firebase";
import "./components/firebaseInit";

Vue.config.productionTip = false;

let app;
firebase.auth().onAuthStateChanged(function(user) 
  if (!app) 
    /* eslint-disable no-new */
    app = new Vue(
      el: "#app",
      router,
      template: "<App/>",
      components:  App 
    );
  
);

firebaseInit.js

import firebase from 'firebase';
import 'firebase/firestore';
import firebaseConfig from './firebaseConfig';
const firebaseApp = firebase.initializeApp(firebaseConfig);
export default firebaseApp.firestore();

【问题讨论】:

能否分享main.jsfirebaseInit.js的完整代码? 确保您遵循文档中的“使用模块捆绑器”设置,位于此处firebase.google.com/docs/web/setup 我刚刚添加了所需的文件 【参考方案1】:

我找到了,只需将 "firebase": "^7.13.1" 替换为 "firebase": "7.13.1". 即可。 我不知道为什么,但它奏效了!

【讨论】:

【参考方案2】:
import  initializeApp  from 'firebase/app'
import  firebaseConfig  from './config'

initializeApp(firebaseConfig)

【讨论】:

请分享有关您的安装过程的更多详细信息。您粘贴的代码是您希望如何使用该库,而不是您应该如何安装它们。 (除非你真的不知道如何安装。)

以上是关于无法为 VueJS + NodeJS 安装 Firebase的主要内容,如果未能解决你的问题,请参考以下文章

vuejs安装篇

vuejs从安装开始一起飞~

vuejs环境搭建

vuejs经验交流之-安装(NPM)

Windows 安装 Vue

nodejs安装 vue安装 创建 vue项目