1

I'm trying to add Angular SSR and I've created the following cloud function:

import * as functions from 'firebase-functions';
import * as path from 'path';

const universal = require(path.resolve(__dirname, '../../dist/server/main')).app;

export const ssr = functions.https.onRequest(universal);

package.json

{
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@types/jsdom": "^11.0.4",
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.0",
    "path": "^0.12.7",
  },
  "devDependencies": {
    "typescript": "^3.8.3",
    "firebase-functions-test": "^0.2.1"
  }
}

When I build then deploy my function I get an error:

!  functions[ssr(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module '/dist/server/main'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/srv/lib/ssr.js:5:19)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)

My folder structure is:

dist
 |-server
 |-browser
functions
 |-lib

How can I fix it?

2 Answers 2

2

I'm guessing that your code is trying to access files outside of the "functions" folder. That is to say "../../dist/server/main" is reaching up and outside the boundary of that folder. Only the contents of the functions folder are deployed by the Firebase CLI. Everything else outside of that will not be accessible by the function after deployment. If you want that content available, you will have to copy it in there. And yes, that maybe means you have to duplicate that file if you use it on both the frontend and backend.

Sign up to request clarification or add additional context in comments.

Comments

1

My problem with this error message was that I refactored a filename from 'TestStoreResult' to 'testStoreResult' but the typescript compiler did not change the filename from a capital to lowercase letter.

Deleting the lib folder and redeploying fixed my issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.