为什么我可以导入firebase/firestore'进入我的firebase云功能而没有安装软件包?

发布于 2025-02-09 17:34:37 字数 1655 浏览 1 评论 0原文

我正在写一些firebase云在打字稿中的功能,然后添加了import {timestamp}从'firebase/firestore'';到我的文件顶部,因为我需要timestamp我的界面。

我没有安装firebase软件包,但令我惊讶的是,它效果很好。我可以在我的打字稿接口中使用timestamp而不会出错。

这就是我的package.json的外观:

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "build:watch": "tsc --watch",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@google-cloud/secret-manager": "^4.0.0",
    "axios": "^0.27.2",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.18.0",
    "googleapis": "^103.0.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.12.0",
    "@typescript-eslint/parser": "^5.12.0",
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.25.4",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^4.5.4"
  },
  "private": true
}

如您所见,未安装firebase软件包。

为什么我能够在不安装的情况下导入它?

它会以某种方式“预加载”?

编辑:

按照注释中的要求,我添加文件夹结构:

”在此处输入图像说明”

I am writing some Firebase Cloud Functions in TypeScript and I added import { Timestamp } from 'firebase/firestore'; to the top of my file because I needed Timestamp for one of my interfaces.

I do not have the firebase package installed, but to my surprise it worked just fine. I could use Timestamp in my TypeScript interface without error.

This is what my package.json looks like:

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "build:watch": "tsc --watch",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@google-cloud/secret-manager": "^4.0.0",
    "axios": "^0.27.2",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.18.0",
    "googleapis": "^103.0.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.12.0",
    "@typescript-eslint/parser": "^5.12.0",
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.25.4",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^4.5.4"
  },
  "private": true
}

As you can see the firebase package is not installed.

Why am I able to import it without having it installed?

Does it come 'pre-loaded' somehow?

EDIT:

As requested in the comments, I am adding the folder structure:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2025-02-16 17:34:37

基于应用程序目录的屏幕截图,您具有node_modules的多个初始化。在您的情况下,您在根文件夹上有一个package.json,该文件夹使firebase软件包安装在 top级 < /em>您的应用程序。这只是意味着它继承了整个项目,您可以在 上使用firebase软件包,任何 directory/firectory/fileser/文件夹。另一方面,package.json src文件夹只能使用 SRC 文件夹。有关下面插图的更多详细信息。

例如:

- function
  |_ node_modules
  |_ src
  |  |_ node_modules
  |  |_ package.json (package B)
  |_ package.json (package A)

package.json(软件包B):

{
  "dependencies": {
    "express": "^4.17.2",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.16.0",
    "firestore-export-import": "^0.17.0"
  }
}

package.json(软件包A):

{
  "dependencies": {
    "firebase": "^9.6.4",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.16.0",
    "firestore-export-import": "^0.17.0"
  }
}

在上面的示例中,软件包A具有firbase,但是没有Express库同时,软件包B具有express,但没有firebase库。在src文件夹中,您可以从软件包a package b 中导入所有库在功能文件夹中,您只能导入软件包

Based on the screenshot of your application directory, you have multiple initialisation of node_modules. In your case, you have a package.json on the root folder that makes the firebase package installed on the top-level of your application. That just means it's inherited to the whole project and that you can use the firebase package on any directory/folders inside the root directory and its subdirectories. On the other hand, the package.json inside the src folder can only be used inside the src folder. More details on the illustration below.

e.g:

- function
  |_ node_modules
  |_ src
  |  |_ node_modules
  |  |_ package.json (package B)
  |_ package.json (package A)

package.json (package B):

{
  "dependencies": {
    "express": "^4.17.2",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.16.0",
    "firestore-export-import": "^0.17.0"
  }
}

package.json (package A):

{
  "dependencies": {
    "firebase": "^9.6.4",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.16.0",
    "firestore-export-import": "^0.17.0"
  }
}

In the examples above, package A has the firebase but doesn't have the express library meanwhile, the package B has the express but doesn't have the firebase library. Inside the src folder, you can import all the libraries installed from the package A and package B but outside the src folder, specifically, the root of the function folder, you can only import package A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文