前提・実現したいこと
Typescript react firebase でチャットボット を作成中
firebase functionsでチャットの構造体(json)をfirestoreにデプロイしたいです。
ソースコード
import * as functions from 'firebase-functions'; ←ここでエラー発生 外部からインポートできませんみたいなことを言われています。 import * as admin from "firebase-admin"; admin.initializeApp(); const db = admin.firestore(); const sendResponse = (response: functions.Response, statusCode: number, body: any) => { response.send({ statusCode, body: JSON.stringify(body) }); }; export const addDataset = functions.https.onRequest(async (req: any, res: any) => { if (req.method !== 'POST') { sendResponse(res, 405, {error: "Invalid Request"}) } else { const dataset = req.body; for (const key of Object.keys(dataset)) { const data = dataset[key]; await db.collection('questions').doc(key).set(data) } sendResponse(res, 200, {message: 'Successfully added dataset! WooHoo!'}); } });
発生している問題・エラーメッセージ
デプロイコマンドで途中までうまく行きますが、functions preparing functions directory for uploadingのとこでError発生します。
¥firebase deploy 以下エラーコード
i deploying firestore, functions, hosting i firestore: reading indexes from firestore.indexes.json... i cloud.firestore: checking firestore.rules for compilation errors... ✔ cloud.firestore: rules file firestore.rules compiled successfully i functions: ensuring required API cloudfunctions.googleapis.com is enabled... i functions: ensuring required API cloudbuild.googleapis.com is enabled... ✔ functions: required API cloudbuild.googleapis.com is enabled ✔ functions: required API cloudfunctions.googleapis.com is enabled ✔ firestore: deployed indexes in firestore.indexes.json successfully i firestore: latest version of firestore.rules already up to date, skipping upload... i functions: preparing functions directory for uploading... Error: Error occurred while parsing your function triggers. /Users/junsuke/Applications/VSCdev/ts-chatbot-lesson/functions/src/index.ts:11 import * as functions from 'firebase-functions'; ^^^^^^ SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:979:16) at Module._compile (internal/modules/cjs/loader.js:1027:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:19:15 at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:57:3) at Module._compile (internal/modules/cjs/loader.js:1063:30)
試したこと
スタックオーバーフローに載っているものは全て試しました。
■追記 package.json tsconfig.jsonを追記しました
package.json
{ "name": "functions", "type":"module", "scripts": { "lint": "tslint --project tsconfig.json", "build": "tsc", "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": "12" }, "main": "src/index.ts", "dependencies": { "firebase-admin": "^9.2.0", "firebase-functions": "^3.11.0" }, "devDependencies": { "typescript": "^3.8.0", "firebase-functions-test": "^0.2.0" }, "private": true }
tsconfig.json
{ "compilerOptions": { "module": "commonjs", "noImplicitReturns": true, "noUnusedLocals": true, "outDir": "lib", "sourceMap": true, "strict": true, "target": "es2017" }, "compileOnSave": true, "include": [ "src" ] }
回答1件
あなたの回答
tips
プレビュー