2Gen Google Cloud Functions deploy as module

25 views Asked by At

I'm trying to deploy a 2 Gen Google Cloud Function (trigger event type) as a module since I need to use import statement.

The problem is that GCP cannot find an entry point How should I modify following code?

index.js

const functions = require('@google-cloud/functions-framework');

// Register a CloudEvent callback with the Functions Framework that will
// be executed when the Pub/Sub trigger topic receives a message.
functions.cloudEvent('printPDFFromHTMLtest2', cloudEvent => {
  // The Pub/Sub message is passed as the CloudEvent's data payload.
  const base64name = cloudEvent.data.message.data;

  const name = base64name
    ? Buffer.from(base64name, 'base64').toString()
    : 'World';

  console.log(`Hello, ${name}!`);
});

package.json

{
  "dependencies": {
    "@google-cloud/functions-framework": "^3.0.0"
  }
}
1

There are 1 answers

0
Matteo Rossi On

I've managed to find a solution.

package.json

{
  "dependencies": {
    "@google-cloud/functions-framework": "^3.0.0"
  },
  "type":"module"
}

index.js

import functions from '@google-cloud/functions-framework';

export const printPDFFromHTMLtest2 = functions.cloudEvent('printPDFFromHTMLtest2', cloudEvent => {
  // The Pub/Sub message is passed as the CloudEvent's data payload.
  const base64name = cloudEvent.data.message.data;

  const name = base64name
    ? Buffer.from(base64name, 'base64').toString()
    : 'World';

  console.log(`Hello, ${name}!`);
});