Ada*_*m D 5 node.js firebase typescript google-cloud-functions
我在使用 ES 模块系统部署从 Typescript 编译的 Firebase 函数时遇到问题。
索引.ts
import * as functions from "firebase-functions";
import myGreeting from "./my-greeting";
export const helloWorld = functions.https.onRequest((request, response) => {
response.send(myGreeting);
});
Run Code Online (Sandbox Code Playgroud)
我的问候语.ts
export default "HELLO WORLD";
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"module": "ES2020",
"noImplicitReturns": true,
"noUnusedLocals": true,
"moduleResolution": "node",
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "ES2020"
},
"compileOnSave": true,
"include": [
"src"
]
}
Run Code Online (Sandbox Code Playgroud)
包.json
{
"name": "functions",
"scripts": {
"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"
},
"type": "module",
"engines": {
"node": "16"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^10.2.0",
"firebase-functions": "^3.21.0"
},
"devDependencies": {
"typescript": "^4.6.4"
},
"private": true
}
Run Code Online (Sandbox Code Playgroud)
这构建得很好,但是 TypeScript 编译器当然不会.js在输出文件中包含 import 语句的扩展./lib/index.js。
然后,当我尝试部署这些功能时,出现以下错误:
Error: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/my-project/functions/lib/my-greeting' imported from /my-project/functions-test/functions/lib/index.js
我需要能够导入一个 npm 库,该库使用从 TypeScript 编译的 ES 模块,不包含 .js 扩展名(它没有 commonjs 版本)。由于我使用节点 16 运行时来执行我理解的函数,我应该能够运行针对 ES2020 的 ES 模块,但是有没有办法--experimental-specifier-resolution=node在 Firebase 函数运行时中设置标志?
如何让函数在 Node 16 Firebase 云运行时中部署/运行,而不需要部署为 commonjs 模块?
在上面的例子中我可以改变
import myGreeting from "./my-greeting";
Run Code Online (Sandbox Code Playgroud)
到
import myGreeting from "./my-greeting.js";
Run Code Online (Sandbox Code Playgroud)
这将是一个非常不切实际的工作流程,但除此之外,问题是我使用的 ES 模块库具有这些未修改的导入语句,是从 TypeScript 构建的,所以我绝对不想进入那里并更改所有import 语句以包含.js扩展名。
"module": "CommonJS"使用tsconfig.jsontsconfig.json如果我将compilerOptions 更改为"module": "CommonJS"并将其取出"type": "module",我可以很好地部署上面的示例package.json,但这不是一个选项,因为我需要使用仅作为 ES 模块提供的库,它没有CommonJS 版本已发布。
小智 0
您需要修改源(index.ts)中的导入
import myGreeting from "./my-greeting";
Run Code Online (Sandbox Code Playgroud)
到
import myGreeting from "./my-greeting.js";
Run Code Online (Sandbox Code Playgroud)
据我所知,TSC 不会修改导入语句。
| 归档时间: |
|
| 查看次数: |
416 次 |
| 最近记录: |