Search code examples
node.jstypescriptgoogle-cloud-functionsfirebase-tools

Firebase Functions with sub path imports outside of project


I have a firebase functions project with typescript. In this project I use types outside of the project with sub path imports, the result of this is that the build files are skewed.

instead of main:lib/index.js I have main:lib/functions/src/index.js

functions/lib:

petertoth@Peters-MBP-2 lib % tree .
.
├── functions
│   └── src
│       ├── index.js
│       ├── index.js.map
│       ├── journalLogs.type.js
│       ├── journalLogs.type.js.map
│       └── util
│           ├── audiFiles.js
│           ├── audiFiles.js.map
│           ├── db.js
│           ├── db.js.map
│           ├── getJournalSettings.js
│           ├── getJournalSettings.js.map
│           ├── prompt.js
│           ├── prompt.js.map
│           ├── storage.js
│           └── storage.js.map
└── types
    ├── firebase
    │   ├── CreateFullRecording.request.js
    │   ├── CreateFullRecording.request.js.map
    │   ├── generateJournal.requests.js
    │   └── generateJournal.requests.js.map
    ├── firestore
    │   ├── JournalSettingsDoc.js
    │   └── JournalSettingsDoc.js.map
    └── openai
        ├── language.js
        └── language.js.map

package.json:

{
  "name": "functions",
 ...
 "imports": {
    "#types/*": ["../types/*"]
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/functions/src/index.js",
  "private": true
  ...
}

tsconfig:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "moduleResolution": "nodenext",
    "paths": {
      "#types/*": ["../types/*"]
    }
  },
  "baseUrl": ".",
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

this works well locally, and I can serve it wonderfully. However when I try to deploy: firebase deploy --only functions I'm getting the following error:

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run build

> build
> tsc

✔  functions: Finished running predeploy script.
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  functions: required API cloudfunctions.googleapis.com is enabled
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
✔  functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing ./functions/ directory for uploading...
i  functions: packaged XX (98.13 KB) for uploading
✔  functions: ./functions/ folder uploaded successfully
i  functions: updating Node.js 16 function generateJournal(europe-west1)...
i  functions: updating Node.js 16 function migrageJournal(europe-west1)...
i  functions: updating Node.js 16 function getCollectionNames(europe-west1)...
i  functions: updating Node.js 16 function createFullRecording(europe-west1)...
Build failed: > build
> tsc

src/index.ts(22,44): error TS2307: Cannot find module '#types/firebase/CreateFullRecording.request' or its corresponding type declarations.
src/index.ts(25,40): error TS2307: Cannot find module '#types/firebase/generateJournal.requests' or its corresponding type declarations.
src/util/getJournalSettings.ts(1,36): error TS2307: Cannot find module '#types/firestore/JournalSettingsDoc' or its corresponding type declarations.
src/util/prompt.ts(1,36): error TS2307: Cannot find module '#types/firestore/JournalSettingsDoc' or its corresponding type declarations.
src/util/prompt.ts(2,30): error TS2307: Cannot find module '#types/openai/language' or its corresponding type declarations.
src/util/prompt.ts(15,31): error TS18046: 'journalBullet' is of type 'unknown'.
src/util/prompt.ts(16,34): error TS18046: 'journalBullet' is of type 'unknown'.
src/util/prompt.ts(48,10): error TS7053: Element implicitly has an 'any' type because expression of type 'LanguageCode' can't be used to index type '{ en: string; da: string; sv: string; no: string; de: string; }'.; Error ID: 1a2262f3

I think there is something wrong with the config of the packaging of files to upload. Just a hunch though and I do not know how to debug this more.


Solution

  • Update your Firebase CLI: npm install -g firebase-tools

    Google Cloud Functions now runs npm run build during deployment, which seems to cause the issue.

    However, as per this comment by colerogers, the Firebase team patched their CLI to disable this feature in firebase-tools v11.27.0.

    If you upgrade to at least that version, this error should go away without any additional work.