![](/img/trans.png)
[英]How do I deploy to firebase functions without npm dependency conflict?
[英]How do I use `import` for npm modules in Firebase Cloud Functions?
我的 JavaScript Firebase Cloud Functions 正在運行 npm 模塊,這些模塊是通過require
導入的。 現在我想使用 npm 通過import
安裝的模塊,而不是require
。 Firebase 模擬器拋出這個錯誤:
SyntaxError: Cannot use import statement outside a module
Node.js文檔說:
import
聲明
import
語句可以引用 ES 模塊或 CommonJS 模塊。import
語句只允許在 ES 模塊中使用,但 CommonJS 支持動態import()
表達式來加載 ES 模塊。導入 CommonJS 模塊時,
module.exports
object 作為默認導出提供。 命名導出可能可用,由 static 分析提供,以方便更好的生態系統兼容性。要求
CommonJS 模塊
require
始終將其引用的文件視為 CommonJS。不支持使用
require
加載 ES 模塊,因為 ES 模塊是異步執行的。 相反,使用import()
從 CommonJS 模塊加載 ES 模塊。
如果我理解正確,Node 模塊可以是ES
或CommonJS
, import
處理這兩種類型,並且require
只處理CommonJS
。
該文檔還建議我的 Cloud Function 也需要是 ES 模塊才能使用import
。 錯誤信息應該說:
SyntaxError: 無法在ES模塊外使用 import 語句
這似乎是問題所在:我的 Cloud Functions 不在 ES 模塊中。 如何為我的 Cloud Functions 制作 ES 模塊?
重現錯誤
以下是重現錯誤的方法。 按照官方文檔創建一個新目錄並安裝 Firebase:
npm install -g firebase-tools
firebase init
Select Emulators
。
Select Create a new project
。
Select Functions Emulator
和Firestore Emulator
。
仿真器設置
接受默認端口。 下載模擬器。
功能設置
Select TypeScript
。 不要使用 ESLint。 安裝依賴項。
模擬函數的執行
firebase emulators:start
這是默認的index.ts
。
import * as functions from "firebase-functions";
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
response.send("Hello from Firebase!");
});
我還嘗試了TypeScript 模塊文檔中提供的模塊:
import * as functions from "firebase-functions";
export default function helloWorld() {
console.log("Hello, world!");
}
修復 index.ts 的路徑
functions/package.json
中的第一個錯誤是:
functions/lib/index.js does not exist, can't deploy Cloud Functions
通過打開functions/package.json
並更改來修復此問題
"main": "lib/index.js",
到
"main": "src/index.ts",
模塊錯誤
下一個錯誤是
Cannot use import statement outside a module
這就是我被困的地方。 這似乎是說我的 Firebase Cloud Functions 不在 ES 模塊中。
package.json
這個問題說把"type": "module",
in functions/package.json
:
{
"name": "functions",
"type": "module",
"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"
},
"engines": {
"node": "16"
},
"main": "src/index.ts",
"dependencies": {
"firebase-admin": "^11.2.0",
"firebase-functions": "^4.0.1",
"got": "^12.5.2"
},
"devDependencies": {
"typescript": "^4.7.4"
},
"private": true
}
這並不能解決錯誤。
tsconfig.json
我打開tsconfig.json
並將"module": "CommonJS",
更改為"module": "ESNext",
並將"target": "es2017"
更改為"target": "ESNext"
。 這個問題解釋了什么是ESNext
。
模擬器繼續拋出錯誤。 這是我的tsconfig.json
文件:
{
"compilerOptions": {
"module": "ESNext",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "ESNext"
},
"compileOnSave": true,
"include": [
"src"
]
}
推薦tsconfig.json
TypeScript 手冊推薦這個tsconfig.json
:
{
"compilerOptions": {
"target": "ES2015",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Recommended"
}
這會引發相同的錯誤。
配置錯誤
默認的package.json
和tsconfig.json
中顯然存在不止一個錯誤配置。 如果有人能告訴我如何正確配置這些,我會向firebase-tools
發出拉取請求。
我正在使用節點 18.1.0。 Firebase推薦節點16。
Firebase 有關 處理依賴項的雲函數文檔說, require
使用 JavaScript 雲函數並import
TypeScript 雲函數。
使用 Node.js
require()
function 加載您已安裝的任何 Node.js 模塊。 您還可以使用require()
function 導入與 function 一起部署的本地文件。如果您在 TypeScript 中編寫函數,請以相同的方式使用
import
語句加載您已安裝的任何 Node.js 模塊。
如果 Node.js 文檔是正確的,那沒有意義。 require()
不能加載任何 Node 模塊,它只能處理CommonJS
模塊。 關於 TypeScript 的段落似乎說您不能將import
與 JavaScript Cloud Functions 一起使用。
ts節點
ts-node 有幫助嗎?
我對為 ES 模塊配置 TypeScript tsconfig.json
的回答? 也回答了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.