3

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Peter Toth
  • 678
  • 6
  • 23
  • The Firebase CLI only packages and deploys files within the functions folder. Everything else is unavailable when Cloud Functions tries to run `npm install` on the project on its own servers, which is why you see those errors. – Doug Stevenson May 02 '23 at 13:47
  • I see, however my types are in the lib folder, as such they should also be deployed once the project built. So it should have access to those types but it doesn't... – Peter Toth May 02 '23 at 15:19
  • No, your "functions" folder is the root of the deployment for Cloud Functions. lib is one folder up, and therefore all the sibling folders to functions are unused. To put it another way "types" is not under "functions", so nothing happens with it at all during deployment. – Doug Stevenson May 02 '23 at 15:27
  • 1
    Is there a recommended way to do this? – Jeff Padgett May 02 '23 at 19:42
  • I think you misunderstand @DougStevenson, the file structure I am showing are the files already built for `functions/lib`. – Peter Toth May 03 '23 at 07:48
  • 1
    It's not real clear because you're not showing one entire tree of files, where package.json exists within that, and the dir where you run the deploy. It would be more helpful if you construct a minimal repo with unambiguous instructions that anyone could follow to easily reproduce what you observe. – Doug Stevenson May 03 '23 at 13:51

1 Answers1

2

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.

Anis Abboud
  • 1,328
  • 2
  • 16
  • 23