Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Event-Driven Cloud Function written in ES module

jwu
Bronze 1
Bronze 1

I was learning how to use CloudEvents functions from the documentation example and tried rewriting it as an ES module. However, the program couldn’t properly read the event object. It only returned event.id, while the rest of the information, especially what's needed for uploading files to the bucket, was undefined. What’s the correct approach to using an ES module for a CloudEvents function?

The following code is my cloudevents function:

 

 

 

export async function fileValidator(event) {
	console.log(`Event ID: ${event.id}`);
	console.log(`Event Type: ${event.type}`);

	const file = event.data;
	console.log(`Bucket: ${file.bucket}`);
	console.log(`File: ${file.name}`);
	console.log(`Metageneration: ${file.metageneration}`);
	console.log(`Created: ${file.timeCreated}`);
	console.log(`Updated: ${file.updated}`);
}

 

 

 

1 1 347
1 REPLY 1

Hello @jwu,

Welcome to the Google Cloud Community!

To use ES Modules within your Cloud Function, you must declare "type": "module" within your package.json

{
  ...
  "type": "module",
  ...
}

Then you can use import or export statements.

If the above option doesn't work, you can contact Google Cloud Support to further look into your case. Let me know if it helped, thanks!