Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase function Error: Cannot find module when serving locally, out of the blue on previously working project

I haven't touched any firebase related files and I can't seem to figure out why exactly this has just started occuring, but I keep getting:

We were unable to load your functions code. (see above)
   - It appears your code is written in Typescript, which must be compiled before emulation.
   - You may be able to run "npm run build" in your functions directory to resolve this.

my firebase config file:

{
    "functions": {
        "predeploy": "npm --prefix \"$RESOURCE_DIR\" run build",
        "source": "server"
    }
}

and my folder structure:

enter image description here

My tsconfig:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist"
  },
  "exclude": ["node_modules", "test", "**/*spec.ts"],
  "include": ["src/**/*", "src"]
}

like image 243
SebastianG Avatar asked Oct 30 '19 14:10

SebastianG


1 Answers

Found the issue in my tsconfig.json

Firebase will look in the package.json's main property to find your function code, in my case I had:

  "main": "dist/index.js",

but index.js is outside the SRC folder and to get vscode to shut up about the tsconfig file I added that 'includes' property there that broke things.

Removing the includes/excludes from there works perfectly.

This mistake was easily identifiable from my screenshot and tsconfig that I had in my first post.

like image 91
SebastianG Avatar answered Oct 18 '22 21:10

SebastianG