typescript - Hur man ställer in ESM för Firebase-funktioner?
Vår serverapp bör använda den senaste ipfs-http-client eftersom det är ett officiellt paket för att komma åt IPFS, men projektet behöver använda ESM för det från v57.0.0. Jag har lagt ner ganska mycket tid och det har varit frustrerande... Jag behöver lite hjälp.
Jag har hänvisat:
- https://firebase.google.com/docs/functions/get-started?gen=1st
- https://github.com/ipfs/js-ipfs/blob/master/docs/upgrading/v0.62-v0.63.md#esm
- https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
- https://www.typescriptlang.org/docs/handbook/esm-node.html
- och många dokument och Stack Overflow
men jag fick ett fel när jag körde firebase lokalt:
⬢ funktioner: Misslyckades med att ladda funktionsdefinition från källan: FirebaseError: Misslyckades med att ladda funktionsdefinition från källan: Misslyckades med att generera manifest från funktionskällan: TypeError: admin.initializeApp är inte en funktion
Detta är ett hinder för att köra testet, distribuera också, i slutändan kan vi inte gå vidare.
Jag har skapat ett rent och litet projekt från början för att säkerställa vad orsaken är som följer:
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,firestore",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "18"
},
"exports": "./lib/index.js",
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^11.8.0",
"firebase-functions": "^4.3.1",
"ipfs-http-client": "^60.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-plugin-import": "^2.25.4",
"firebase-functions-test": "^3.1.0",
"typescript": "^4.9.0"
},
"private": true,
"type": "module"
}
tsconfig.json
{
"compilerOptions": {
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
// ensures output is ESM
// "module": "es2020", // NOT work
// "module": "es2022", // NOT work
"module": "esnext", // NOT work
// "moduleResolution": "node16" // Compile error
// "moduleResolution": "nodenext" // Compile error
"moduleResolution": "node", // NOT work
// support modern features like private identifiers
// "target": "es2017", // NOT work
// "target": "es2020", // NOT work
"target": "es2022", // NOT work
"esModuleInterop": true,
},
"compileOnSave": true,
"include": [
"src"
]
}
functions/src/index.ts
import {onRequest} from "firebase-functions/v2/https";
import * as logger from "firebase-functions/logger";
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import {create, IPFSHTTPClient} from "ipfs-http-client";
admin.initializeApp();
/**
* create an ipfs client
* @return {IPFSHTTPClient}
*/
function createIpfsClient(): IPFSHTTPClient {
const endpoint = new URL("https://ipfs.infura.io:5001");
const auth =
`Basic ${Buffer.from("IPFS_USERNAME:IPFS_PASSWORD").toString("base64")}`;
return create({
host: endpoint.host,
port: Number(endpoint.port),
protocol: endpoint.protocol,
headers: {
authorization: auth,
},
});
}
export const createIpfs = functions.https.onRequest(async (_req, _res) => {
const client = createIpfsClient();
logger.info(client);
});
Miljö
- os: macOS 13.3.1 (a) (22E772610a)
- nod: v18.14.0
- firebase-verktyg: 12.0.0
Steg för att reproducera
cd functions
yarn install
yarn serve
Svar #1
import {onRequest} from "firebase-functions/v2/https";
import * as logger from "firebase-functions/logger";
import * as functions from "firebase-functions";
import { initializeApp } from "firebase-admin/app"; // *** HERE
import {create, IPFSHTTPClient} from "ipfs-http-client";
initializeApp(); // *** HERE
/**
* create an ipfs client
* @return {IPFSHTTPClient}
*/
function createIpfsClient(): IPFSHTTPClient {
const endpoint = new URL("https://ipfs.infura.io:5001");
const auth =
`Basic ${Buffer.from("IPFS_USERNAME:IPFS_PASSWORD").toString("base64")}`;
return create({
host: endpoint.host,
port: Number(endpoint.port),
protocol: endpoint.protocol,
headers: {
authorization: auth,
},
});
}
export const createIpfs = functions.https.onRequest(async (_req, _res) => {
const client = createIpfsClient();
logger.info(client);
});
