title: Why does using the "camelcase-keys" package cause a Firebase "initializeApp()" error?
body: <p>I am trying to build some Firebase Cloud Functions, and also use the npm package <a href="https://www.npmjs.com/package/camelcase-keys" rel="nofollow noreferrer">camelcase-keys</a> to transform the keys of an API response.</p>
  <p>But whenever use that imported package I get this error:</p>
  <blockquote>
  <p>Error: Failed to load function definition from source: Failed to
  generate manifest from function source: Error: The default Firebase
  app already exists. This means you called initializeApp() more than
  once without providing an app name as the second argument. In most
  cases you only need to call initializeApp() once. But if you do want
  to initialize multiple apps, pass a second argument to initializeApp()
  to give each app a unique name.</p>
  </blockquote>
  <p>This error is strange to me because I am following the advice <a href="https://stackoverflow.com/a/52743902/1526396">here</a> and only calling <code>admin.initializeApp()</code> once at my function entry point (<code>index.ts</code>).</p>
  <p>Here is what the file structure looks like, and a copy of the code I am using.</p>
  <p>Instead of including all the code, I have only included the salient parts (imports, exports, and admin.initializeApp). Let me know if more code pasted here would be helpful:</p>
  <pre><code>functions/
  ├─ src/
  │  ├─ index.ts
  │  ├─ dataForSeo.ts
  </code></pre>
  <p><strong>index.ts</strong></p>
  <pre class="lang-TypeScript prettyprint-override"><code>import 'source-map-support/register';
  import * as functions from 'firebase-functions';
  import * as admin from 'firebase-admin';
  admin.initializeApp();
  
  export const sydneyFunctions = functions.region('australia-southeast1');
  export const db = admin.firestore();
  
  export {
    dataForSeoSerpGoogleOrganicDailyEnqueue,
    dataForSeoSerpGoogleOrganicTask,
    dataForSeoSerpGoogleOrganicPostbackUrl,
    dataForSeoOnPageStartCrawl,
    dataForSeoOnPageEnqueuePingbackUrl,
    dataForSeoOnPageGetResultTask,
  } from './dataForSeo';
  </code></pre>
  <p><strong>dataForSeo.ts</strong></p>
  <pre class="lang-TypeScript prettyprint-override"><code>import camelcaseKeys from 'camelcase-keys';
  import * as functions from 'firebase-functions';
  import * as admin from 'firebase-admin';
  import axios from 'axios';
  import { getFunctions } from 'firebase-admin/functions';
  import { DataForSeoGoogleOrganicPostTask, Serp, SerpItem } from './types';
  import { getSecretVersion } from './googleSecretManager';
  import { db, sydneyFunctions } from './index';
  
  // Example usage
  const test = camelcaseKeys(
    { 'foo-bar': true, nested: { unicorn_rainbow: true } },
    { deep: true }
  );
  functions.logger.info(test);
  
  ...
  // Other functions below here
  </code></pre>
  <p><strong>UPDATE:</strong></p>
  <p>Instead of using the <code>camelcase-keys</code> package I tried creating a recursive function using <code>lodash</code> that does the same thing.</p>
  <p>This works without error:</p>
  <pre><code>import { camelCase, isPlainObject } from 'lodash';
  
  const camelizeKeys = &lt;
    T extends Record&lt;string, unknown&gt; | Record&lt;string, unknown&gt;[]
  &gt;(
    obj: T
  ): T =&gt; {
    if (Array.isArray(obj)) {
      return &lt;T&gt;obj.map((v) =&gt; camelizeKeys(v));
    } else if (isPlainObject(obj)) {
      return &lt;T&gt;Object.keys(obj).reduce(
        (result, key) =&gt; ({
          ...result,
          [camelCase(key)]: camelizeKeys(&lt;T&gt;obj[key]),
        }),
        {}
      );
    }
    return obj;
  };
  </code></pre>
  <p>While this custom function is a work-around, I would still like to get the <code>camelcase-keys</code> package working.</p>