为什么我可以导入firebase/firestore'进入我的firebase云功能而没有安装软件包?
我正在写一些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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于应用程序目录的屏幕截图,您具有
node_modules
的多个初始化。在您的情况下,您在根文件夹上有一个package.json
,该文件夹使firebase
软件包安装在 top级 < /em>您的应用程序。这只是意味着它继承了整个项目,您可以在 上使用firebase
软件包,任何 directory/firectory/fileser/文件夹。另一方面,package.json
src
文件夹只能使用 SRC 文件夹。有关下面插图的更多详细信息。例如:
package.json
(软件包B):package.json
(软件包A):在上面的示例中,软件包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 apackage.json
on the root folder that makes thefirebase
package installed on the top-level of your application. That just means it's inherited to the whole project and that you can use thefirebase
package on any directory/folders inside the root directory and its subdirectories. On the other hand, thepackage.json
inside thesrc
folder can only be used inside thesrc
folder. More details on the illustration below.e.g:
package.json
(package B):package.json
(package A):In the examples above, package A has the
firebase
but doesn't have theexpress
library meanwhile, the package B has theexpress
but doesn't have thefirebase
library. Inside thesrc
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.