typescript - 由於依賴問題,Firebase 函數建置失敗
我剛剛建立了一個新的 Firebase 專案並設定了函式。
然後我添加了一個簡單的觸發函數,並想要部署它。
當我執行建置命令 npm run build (tsc) 時,我收到以下錯誤訊息:
> [email build {{PROJECT_PATH}}\firebase\functions
> tsc
../../../../../../../node_modules/@types/express-serve-static-core/index.d.ts:504:18 - error TS2430: Interface 'Response<ResBody, StatusCode>' incorrectly extends interface 'ServerResponse'.
Types of property 'req' are incompatible.
Type 'Request<ParamsDictionary, any, any, ParsedQs> | undefined' is not assignable to type 'IncomingMessage'.
Type 'undefined' is not assignable to type 'IncomingMessage'.
504 export interface Response<ResBody = any, StatusCode extends number = number> extends http.ServerResponse, Express.Response {
~~~~~~~~
../../../../../../../node_modules/@types/readable-stream/index.d.ts:19:15 - error TS2417: Class static side 'typeof _Readable' incorrectly extends base class static side 'typeof Readable'.
The types of 'Stream.Readable.Duplex' are incompatible between these types.
Property 'isDisturbed' is missing in type 'typeof _Readable.Duplex' but required in type 'typeof import("stream").Duplex'.
19 declare class _Readable extends stream.Readable {
~~~~~~~~~
node_modules/@types/node/stream.d.ts:59:20
59 static isDisturbed(stream: Readable | NodeJS.ReadableStream): boolean;
~~~~~~~~~~~
'isDisturbed' is declared here.
../../../../../../../node_modules/@types/readable-stream/index.d.ts:68:11 - error TS2720: Class 'Duplex' incorrectly implements class '_Readable'. Did you mean to extend '_Readable' and inherit its members as a subclass?
Type 'Duplex' is missing the following properties from type '_Readable': readableAborted, readableDidRead
68 class Duplex extends Writable implements /*extends*/_Readable, stream.Duplex {
~~~~~~
../../../../../../../node_modules/@types/readable-stream/index.d.ts:68:11 - error TS2720: Class '_Readable.Duplex' incorrectly implements class 'import("stream").Duplex'. Did you mean to extend 'import("stream").Duplex' and inherit its members as a subclass?
Type 'Duplex' is missing the following properties from type 'Duplex': readableAborted, readableDidRead
68 class Duplex extends Writable implements /*extends*/_Readable, stream.Duplex {
~~~~~~
../../../../../../../node_modules/@types/readable-stream/index.d.ts:111:11 - error TS2720: Class '_Readable.PassThrough' incorrectly implements class 'import("stream").PassThrough'. Did you mean to extend 'import("stream").PassThrough' and inherit its members as a subclass?
Type 'PassThrough' is missing the following properties from type 'PassThrough': readableAborted, readableDidRead
111 class PassThrough extends Transform implements stream.PassThrough {
~~~~~~~~~~~
../../../../../../../node_modules/@types/readable-stream/index.d.ts:173:11 - error TS2720: Class '_Readable.Transform' incorrectly implements class 'import("stream").Transform'. Did you mean to extend 'import("stream").Transform' and inherit its members as a subclass?
Type 'Transform' is missing the following properties from type 'Transform': readableAborted, readableDidRead
173 class Transform extends Duplex implements stream.Transform {
我該如何解決這些問題?這些依賴項都沒有在我的程式碼中直接使用。
(例如,我將我的程式碼縮減到單一函數,但錯誤沒有改變)
我的函數程式碼:
index.ts
export {
authUserCreatedTrigger
} from './user/auth-user.trigger';
config.ts
export const functionsRegion = 'europe-west1';
export const firestoreSettings = {timestampsInSnapshots: true};
export enum FirebaseCollection {
User = 'user',
}
auth-user.trigger.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const firestore = admin.firestore();
firestore.settings(firestoreSettings);
const onCreateHandler = async (user: admin.auth.UserRecord, context: functions.EventContext) => {
// Email is an required field
if (!user.email) {
return null;
}
// Disable user on default
user = await admin.auth().updateUser(user.uid, {
disabled: true,
});
// Split up user object
const {uid, displayName, photoURL, email, phoneNumber, disabled} = user;
// Get firestore user document ref
const userDocumentRef = firestore.collection(FirebaseCollection.User).doc(uid);
// Write new user to firestore
return userDocumentRef.set({
uid,
displayName,
photoURL,
email,
emailVerified: false,
phoneNumber,
disabled,
deletedAt: null,
createdAt: admin.firestore.Timestamp.now(),
});
};
export const authUserCreatedTrigger = functions
.region(functionsRegion)
.auth
.user()
.onCreate(onCreateHandler);
答案#1
我在將 firebase init 完全清除後導入現有專案時遇到了完全相同的問題。你看到的錯誤是 node_modules 內的 @types/* 錯誤。
這意味著你有以下選擇:
快速而粗糙
在你的 tsconfig.json 中,在 compilerOptions 區塊中加入 "skipLibCheck": true:
{
"compilerOptions": {
// (...) firebase standard init
"skipLibCheck": true
},
// other stuff
}
艱難的方式
閱讀你的錯誤訊息並安裝缺失的 @types/** 套件。在完全清潔且新的安裝中可以從以下開始:
@types/node@types/express- (...)
只要注意這些行 ../../../../../../../node_modules/@types/
