0

I'm using sharp to convert images with a cloud function. Visual studio code, typescript.

I import it as:

import * as sharp from 'sharp';

I use it like this:

     export const generateThumbs = functions.storage
  .object()
  .onFinalize(async object => {
    const bucket = gcs.bucket(object.bucket);
    const filePath = object.name;
    const fileName = filePath.split('/').pop();
    const bucketDir = dirname(filePath);

    const workingDir = join(tmpdir(), 'thumbs');
    const tmpFilePath = join(workingDir, 'source.png');

    if (fileName.includes('thumb@') || !object.contentType.includes('image')) {
      console.log('exiting function');
      return false;
    }

    // 1. Ensure thumbnail dir exists
    await fs.ensureDir(workingDir);

    // 2. Download Source File
    await bucket.file(filePath).download({
      destination: tmpFilePath
    });

    // 3. Resize the images and define an array of upload promises
    const sizes = [64, 128, 256];

    const uploadPromises = sizes.map(async size => {
      const thumbName = `thumb@${size}_${fileName}`;
      const thumbPath = join(workingDir, thumbName);

      // Resize source image
      await sharp(tmpFilePath)
        .resize(size, size)
        .toFile(thumbPath);

      // Upload to GCS
      return bucket.upload(thumbPath, {
        destination: join(bucketDir, thumbName)
      });
    });

    // 4. Run the upload operations
    await Promise.all(uploadPromises);

    // 5. Cleanup remove the tmp/thumbs from the filesystem
    return fs.remove(workingDir);
  });

(code from fireship.io)

This is my package.json file

"dependencies": {
    "@firebase/app": "^0.4.22",
    "@firebase/app-types": "^0.4.7",
    "@google-cloud/storage": "^4.1.0",
    "@sendgrid/client": "^6.4.0",
    "@sendgrid/contact-importer": "^6.4.0",
    "@sendgrid/mail": "^6.4.0",
    "@std/esm": "^0.26.0",
    "@types/algoliasearch": "^3.34.5",
    "@types/react": "^16.9.11",
    "@types/react-dom": "^16.9.3",
    "algolia-firebase-functions": "^3.2.0",
    "algoliasearch": "^3.35.1",
    "async.ensureasync": "^0.5.2",
    "async.queue": "^0.5.2",
    "bottleneck": "^2.19.5",
    "debug": "^4.1.1",
    "express": "^4.17.1",
    "firebase": "^7.2.3",
    "firebase-admin": "^8.7.0",
    "firebase-functions": "^3.3.0",
    "flag": "^4.4.0",
    "fs-extra": "^8.1.0",
    "geofirex": "0.0.6",
    "images-downloader": "^1.0.3",
    "lodash.chunk": "^4.2.0",
    "react-redux": "^7.1.1",
    "request": "^2.88.0",
    "rxfire": "^3.8.8",
    "rxjs": "^6.5.3",
    "sharp": "^0.23.2",
    "tmp": "^0.1.0"
  },

Since I use the sharp package I get this message when I run: firebase deploy --only functions:myFunctionName

There was an unknown problem while trying to parse function triggers. Please ensure you are using Node.js v6 or greater.

I'm using node 12.3.1

Thanks for the help!

Karel Debedts
  • 5,226
  • 9
  • 30
  • 70
  • 1
    Please edit the question to show in more detail the output of the CLI, and the entire, minimal code that causes the problem. https://stackoverflow.com/help/minimal-reproducible-example – Doug Stevenson Dec 06 '19 at 15:46
  • Thanks, I edited it, that was the only error ... – Karel Debedts Dec 06 '19 at 16:48
  • Are you sure there's not more to the CLI output than just that one line? – Doug Stevenson Dec 06 '19 at 17:25
  • Yes, it's really weird... This is the CLI output: ✔ functions: Finished running predeploy script. i functions: ensuring necessary APIs are enabled... ✔ functions: all necessary APIs are enabled i functions: preparing functions directory for uploading... Error: There was an unknown problem while trying to parse function triggers. Please ensure you are using Node.js v6 or greater. – Karel Debedts Dec 06 '19 at 17:26
  • I saw this post : https://stackoverflow.com/questions/56675535/firebase-deploy-functions-with-sharp-library-fails-in-google-cloud-build/56743491#comment104646022_56743491 It looks like my problem, but I have no idea how to implement the solution or don't now if it will work... – Karel Debedts Dec 06 '19 at 17:27
  • 2
    I suggest contacting Firebase support directly for troubleshooting assistance. https://support.google.com/firebase/contact/support – Doug Stevenson Dec 06 '19 at 17:27

0 Answers0