title: TypeError when using Google Vision API in Firebase Cloud Functions
body: <p>I'm trying to get a sample for the use of the Google Vision API within Firebase Cloud Functions to work but it fails.</p>
  
  <p>I'm using the unmodified sample provided on Github: <a href="https://github.com/firebase/functions-samples/tree/master/moderate-images" rel="nofollow noreferrer">https://github.com/firebase/functions-samples/tree/master/moderate-images</a></p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is my source file:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>/**
   * Copyright 2016 Google Inc. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for t`he specific language governing permissions and
   * limitations under the License.
   */
  'use strict';
  
  const functions = require('firebase-functions');
  const mkdirp = require('mkdirp-promise');
  const gcs = require('@google-cloud/storage')();
  const vision = require('@google-cloud/vision')();
  const spawn = require('child-process-promise').spawn;
  const path = require('path');
  const os = require('os');
  const fs = require('fs');
  
  /**
   * When an image is uploaded we check if it is flagged as Adult or Violence by the Cloud Vision
   * API and if it is we blur it using ImageMagick.
   */
  exports.blurOffensiveImages = functions.storage.object().onChange(event =&gt; {
    const object = event.data;
    const file = gcs.bucket(object.bucket).file(object.name);
  
    // Exit if this is a move or deletion event.
    if (object.resourceState === 'not_exists') {
      return console.log('This is a deletion event.');
    }
  
    // Check the image content using the Cloud Vision API.
    return vision.detectSafeSearch(file).then(data =&gt; {
      const safeSearch = data[0];
      console.log('SafeSearch results on image', safeSearch);
  
      if (safeSearch.adult || safeSearch.violence) {
        return blurImage(object.name, object.bucket, object.metadata);
      }
    });
  });
  
  /**
   * Blurs the given image located in the given bucket using ImageMagick.
   */
  function blurImage(filePath, bucketName, metadata) {
    const tempLocalFile = path.join(os.tmpdir(), filePath);
    const tempLocalDir = path.dirname(tempLocalFile);
    const bucket = gcs.bucket(bucketName);
  
    // Create the temp directory where the storage file will be downloaded.
    return mkdirp(tempLocalDir).then(() =&gt; {
      console.log('Temporary directory has been created', tempLocalDir);
      // Download file from bucket.
      return bucket.file(filePath).download({destination: tempLocalFile});
    }).then(() =&gt; {
      console.log('The file has been downloaded to', tempLocalFile);
      // Blur the image using ImageMagick.
      return spawn('convert', [tempLocalFile, '-channel', 'RGBA', '-blur', '0x8', tempLocalFile]);
    }).then(() =&gt; {
      console.log('Blurred image created at', tempLocalFile);
      // Uploading the Blurred image.
      return bucket.upload(tempLocalFile, {
        destination: filePath,
        metadata: {metadata: metadata} // Keeping custom metadata.
      });
    }).then(() =&gt; {
      console.log('Blurred image uploaded to Storage at', filePath);
      fs.unlinkSync(tempLocalFile);
      console.log('Deleted local file', filePath);
    });
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I've done the following steps:</p>
  
  <ul>
  <li>Created a working Firebase project</li>
  <li>Activated the Vision API and the billing for the project</li>
  <li>Initialized the Firebase Functions localy on my PC</li>
  <li>Installed needed npm modules with <code>npm install</code></li>
  <li>Tried to deploy with <code>firebase deploy</code></li>
  </ul>
  
  <p>Then i got this error:</p>
  
  <pre><code>i  deploying functions
  i  functions: ensuring necessary APIs are enabled...
  +  functions: all necessary APIs are enabled
  i  functions: preparing functions directory for uploading...
  
  Error: Error occurred while parsing your function triggers.
  
  TypeError: require(...) is not a function
      at Object.&lt;anonymous&gt; (C:\Users\xxxxxx\FirebaseTest\functions\index.js:21:47)
      at Module._compile (module.js:570:32)
      at Object.Module._extensions..js (module.js:579:10)
      at Module.load (module.js:487:32)
      at tryModuleLoad (module.js:446:12)
      at Function.Module._load (module.js:438:3)
      at Module.require (module.js:497:17)
      at require (internal/module.js:20:19)
      at C:\Users\Tobias\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:18:11
      at Object.&lt;anonymous&gt; (C:\Users\xxxxxx\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:38:3)
  </code></pre>
  
  <p>So he is complaining about this line:</p>
  
  <pre><code>const vision = require('@google-cloud/vision')();
  </code></pre>
  
  <p>My package.json looks like this:</p>
  
  <pre><code>{
    "name": "functions",
    "description": "Cloud Functions for Firebase",
    "scripts": {
      "serve": "firebase serve --only functions",
      "shell": "firebase experimental:functions:shell",
      "start": "npm run shell",
      "deploy": "firebase deploy --only functions",
      "logs": "firebase functions:log"
    },
    "dependencies": {
      "@google-cloud/storage": "^1.5.2",
      "@google-cloud/vision": "^0.14.0",
      "child-process-promise": "^2.2.1",
      "firebase-admin": "^5.8.1",
      "firebase-functions": "^0.8.1",
      "mkdirp": "^0.5.1",
      "mkdirp-promise": "^5.0.1"
    },
    "private": true
  }
  </code></pre>
  
  <p><strong>Nice to know:</strong> Other attempts, for example to try out Cloud Storage triggers in other functions, are working pretty well with my Firebase project. Only the Vision API gives me that much trouble.</p>
  
  <p>Can someone please give me a hint what went wrong with my setup?</p>
  
  <p>Thank you!</p>