I’m implementing a service to publish messages to a Google Cloud Pub/Sub topic.
import { PubSub } from '@google-cloud/pubsub'
import type { NotificationData } from './models/notification'
class NotificationService {
private pubsubClient: PubSub
private topicName = 'notifications'
constructor() {
this.pubsubClient = new PubSub()
}
async publishMessage(messageData: NotificationData) {
const data = JSON.stringify(messageData)
const dataBuffer = Buffer.from(data)
try {
const messageId = await this.pubsubClient
.topic(this.topicName)
.publishMessage({ data: dataBuffer })
console.log(`Message published (id: ${messageId})`)
} catch (error) {
console.error('Error publishing message:', error)
}
}
}
export const notificationService = new NotificationService()
When I import and use the service in the front-end (vue3) component, the app fails to load with the following error:
Uncaught TypeError: Class extends value undefined is not a constructor or null
at node_modules/@google-cloud/paginator/build/src/resource-stream.js (@google-cloud_pubsub.js?v=ccdc38b3:46169:49)
at __require2 (chunk-AUZ3RYOM.js?v=ccdc38b3:18:50)
at node_modules/@google-cloud/paginator/build/src/index.js (@google-cloud_pubsub.js?v=ccdc38b3:46236:29)
at __require2 (chunk-AUZ3RYOM.js?v=ccdc38b3:18:50)
at node_modules/@google-cloud/pubsub/build/src/pubsub.js (@google-cloud_pubsub.js?v=ccdc38b3:63297:23)
at __require2 (chunk-AUZ3RYOM.js?v=ccdc38b3:18:50)
at node_modules/@google-cloud/pubsub/build/src/index.js (@google-cloud_pubsub.js?v=ccdc38b3:85020:20)
at __require2 (chunk-AUZ3RYOM.js?v=ccdc38b3:18:50)
at @google-cloud_pubsub.js?v=ccdc38b3:85077:16@google-cloud_pubsub.js?v=ccdc38b3:46169
This happens before the component using the service is even rendered. I can’t find anyone with the same issue anywhere in the library’s issues or elsewhere on stackoverflow.