当使用 firebase 运行 代码时,我从 chokidar 收到了这个错误

I got this Error from chokidar when running code with firebase

我正在 运行 运行我的 firebase 项目并在终端中收到此错误

This dependency was not found:

要安装它,你可以运行: npm install --save firebase 来自 chokidar (C:) 的错误:错误:EBUSY:资源繁忙或锁定,lstat 'C:\DumpStack.log.tmp

在我的 firebaseInit.js

里面
import firebase from 'firebase';

const firebaseConfig = {
    apiKey: "xxxxxxxxxxxxxx",
    authDomain: "xxxxxxxxxxxxxxxxxxx",
    projectId: "xxxxxxxxxxxxxxxxxxxxxxxx",
    storageBucket: "xxxxxxxxxxxxxxxxxx",
    messagingSenderId: "xxxxxxxxxxxxxxxxxxxxxx",
    appId: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
    measurementId: "xxxxxxxxxxxxxxxxxx"
};

export default firebase.initializeApp(firebaseConfig);

这是我的 package.json

{
"name": "mevnproject",
"version": "0.1.0",
"private": true,
"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
},
"dependencies": {
    "axios": "^0.25.0",
    "core-js": "^3.6.5",
    "firebase": "9.6.11",
    "socket.io": "^4.4.1",
    "vue": "^2.6.11",
    "vue-chat-scroll": "^1.4.0",
    "vue-google-charts": "^0.3.3",
    "vue-router": "^3.5.3",
    "vue2-google-maps": "^0.10.7",
    "vuetify": "^2.6.2",
    "vuex": "^3.6.2",
    "vuex-persistedstate": "^4.1.0"
},
"devDependencies": {
    "@mdi/font": "^6.5.95",
    "@mdi/js": "^6.5.95",
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.2.2",
    "material-design-icons-iconfont": "^6.1.1",
    "vue-template-compiler": "^2.6.11"
},
"eslintConfig": {
    "root": true,
    "env": {
        "node": true
    },
    "extends": [
        "plugin:vue/essential",
        "eslint:recommended"
    ],
    "parserOptions": {
        "parser": "babel-eslint"
    },
    "rules": {}
},
"browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
]

}

我尝试卸载并重新安装 firebase 但仍然出现相同的错误。

编辑:在我的组件中,脚本如下所示

<script>
import { db } from "../../firebaseInit";

export default {
  data() {
    return {
      message: null,
    };
  },
  methods: {
    saveMessage() {
      db.firestore()
        .collection("chat")
        .add({
          message: this.message,
        })
        .then(() => {
          console.log("Document Written");
        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
};
</script> 

从您的 package.json 文件中我们可以看到您使用的是 Firebase SDK 版本 9。因此您需要调整 firebaseInit.js 文件。我建议只导出您需要的服务,即您的情况下的 Firestore(请参阅下面的评论)。

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
    apiKey: "xxxxxxxxxxxxxx",
    authDomain: "xxxxxxxxxxxxxxxxxxx",
    projectId: "xxxxxxxxxxxxxxxxxxxxxxxx",
    storageBucket: "xxxxxxxxxxxxxxxxxx",
    messagingSenderId: "xxxxxxxxxxxxxxxxxxxxxx",
    appId: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
    measurementId: "xxxxxxxxxxxxxxxxxx"
};

const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);

export { db };

然后,在你的组件中你喜欢:

import { db } from '../firebaseInit';
import {  collection, addDoc } from 'firebase/firestore';  // Example

export default {
  data() {
    return {
      message: null,
    };
  },
  methods: {
    saveMessage() {
      addDoc(collection(db, 'chat'), 
      {
        message: this.message,
      })
        .then(() => {
          console.log("Document Written");
        })
        .catch((error) => {
          console.error(error);
        });
    },
  },
};

如果您需要同时使用多个服务,请按如下操作(举例):

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getAuth } from "firebase/auth";
import { getStorage } from "firebase/storage";

const firebaseConfig = {
    apiKey: "xxxxxxxxxxxxxx",
    authDomain: "xxxxxxxxxxxxxxxxxxx",
    projectId: "xxxxxxxxxxxxxxxxxxxxxxxx",
    storageBucket: "xxxxxxxxxxxxxxxxxx",
    messagingSenderId: "xxxxxxxxxxxxxxxxxxxxxx",
    appId: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
    measurementId: "xxxxxxxxxxxxxxxxxx"
};

const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
const auth = getAuth(firebaseApp);
const storage = getStorage(firebaseApp);

export { db, storage, auth };

并在组件中

import { db, auth } from '../firebaseInit';
import { doc, getDoc } from 'firebase/firestore';  // Example
import { signInWithEmailAndPassword } from '@firebase/auth';  // Example

doc 中有更多详细信息。