Firebase-tools: Functions only deploy erroring, referencing parent application node_modules folder

Created on 19 Apr 2018  ·  21Comments  ·  Source: firebase/firebase-tools

Version info

firebase: 3.18.4
node: 8.10.0
npm: 5.6.0

Platform Information

Windows

Steps to reproduce

firebase deploy --only functions

Expected behavior

Firebase cloud functions will deploy successfully.

Actual behavior

Receiving an error because the cloud functions build seems to be referencing the node_modules folder from the root level of the application. @types/d3 and @types/googlemaps are included in my applications package.json file and not my functions package.json file.

```PS [PATH ON LOCAL MACHINE]> firebase deploy --only functions

=== Deploying to 'PROJECT_NAME'...

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

functions@ lint [PATH ON LOCAL MACHINE]\functions
tslint --project tsconfig.json

Running command: npm --prefix "$RESOURCE_DIR" run build

functions@ build [PATH ON LOCAL MACHINE]\functions
tsc

../node_modules/@types/d3-axis/index.d.ts(50,36): error TS2304: Cannot find name 'SVGSVGElement'.
../node_modules/@types/d3-axis/index.d.ts(50,52): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(32,23): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(39,27): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(50,27): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(63,27): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(63,77): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(70,27): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(81,32): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(94,32): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(94,72): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(98,23): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(121,28): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(126,23): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(138,30): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(164,36): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(191,45): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-brush/index.d.ts(223,38): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-chord/index.d.ts(360,16): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-chord/index.d.ts(368,22): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-dispatch/index.d.ts(8,37): error TS2304: Cannot find name 'EventTarget'.
../node_modules/@types/d3-dispatch/index.d.ts(18,36): error TS2304: Cannot find name 'EventTarget'.
../node_modules/@types/d3-drag/index.d.ts(19,38): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-drag/index.d.ts(24,36): error TS2304: Cannot find name 'HTMLElement'.
../node_modules/@types/d3-drag/index.d.ts(24,50): error TS2304: Cannot find name 'SVGSVGElement'.
../node_modules/@types/d3-drag/index.d.ts(24,66): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-drag/index.d.ts(390,37): error TS2304: Cannot find name 'Window'.
../node_modules/@types/d3-drag/index.d.ts(402,36): error TS2304: Cannot find name 'Window'.
../node_modules/@types/d3-fetch/index.d.ts(18,42): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(18,64): error TS2304: Cannot find name 'Blob'.
../node_modules/@types/d3-fetch/index.d.ts(27,44): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(41,12): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(82,11): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(100,12): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(145,11): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(157,42): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(157,64): error TS2304: Cannot find name 'Document'.
../node_modules/@types/d3-fetch/index.d.ts(167,74): error TS2304: Cannot find name 'HTMLImageElement'.
../node_modules/@types/d3-fetch/index.d.ts(179,72): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(189,41): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(189,63): error TS2304: Cannot find name 'Document'.
../node_modules/@types/d3-fetch/index.d.ts(199,42): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(212,12): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(254,11): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(266,41): error TS2304: Cannot find name 'RequestInit'.
../node_modules/@types/d3-fetch/index.d.ts(266,63): error TS2304: Cannot find name 'XMLDocument'.
../node_modules/@types/d3-selection/index.d.ts(17,24): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(17,49): error TS2304: Cannot find name 'Document'.
../node_modules/@types/d3-selection/index.d.ts(17,60): error TS2304: Cannot find name 'Window'.
../node_modules/@types/d3-selection/index.d.ts(34,20): error TS2304: Cannot find name 'Document'.
../node_modules/@types/d3-selection/index.d.ts(36,27): error TS2304: Cannot find name 'Node'.
../node_modules/@types/d3-selection/index.d.ts(36,34): error TS2304: Cannot find name 'Node'.
../node_modules/@types/d3-selection/index.d.ts(37,28): error TS2304: Cannot find name 'Node'.
../node_modules/@types/d3-selection/index.d.ts(37,44): error TS2304: Cannot find name 'Node'.
../node_modules/@types/d3-selection/index.d.ts(37,51): error TS2304: Cannot find name 'Node'.
../node_modules/@types/d3-selection/index.d.ts(38,39): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(39,42): error TS2304: Cannot find name 'NodeListOf'.
../node_modules/@types/d3-selection/index.d.ts(39,53): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(45,32): error TS2304: Cannot find name 'HTMLElement'.
../node_modules/@types/d3-selection/index.d.ts(45,46): error TS2304: Cannot find name 'SVGSVGElement'.
../node_modules/@types/d3-selection/index.d.ts(45,62): error TS2304: Cannot find name 'SVGGElement'.
../node_modules/@types/d3-selection/index.d.ts(110,110): error TS2304: Cannot find name 'HTMLElement'.
../node_modules/@types/d3-selection/index.d.ts(142,113): error TS2304: Cannot find name 'HTMLElement'.
../node_modules/@types/d3-selection/index.d.ts(873,43): error TS2304: Cannot find name 'HTMLElement'.
../node_modules/@types/d3-selection/index.d.ts(977,61): error TS2304: Cannot find name 'TouchList'.
../node_modules/@types/d3-selection/index.d.ts(989,64): error TS2304: Cannot find name 'TouchList'.
../node_modules/@types/d3-selection/index.d.ts(1013,29): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1025,15): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1033,18): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1040,15): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1040,35): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1108,33): error TS2304: Cannot find name 'Window'.
../node_modules/@types/d3-selection/index.d.ts(1108,42): error TS2304: Cannot find name 'Document'.
../node_modules/@types/d3-selection/index.d.ts(1108,53): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1108,63): error TS2304: Cannot find name 'Window'.
../node_modules/@types/d3-selection/index.d.ts(1122,44): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1133,45): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1154,46): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1163,49): error TS2304: Cannot find name 'Element'.
../node_modules/@types/d3-selection/index.d.ts(1163,97): error TS2304: Cannot find name 'NodeListOf'.
../node_modules/@types/d3-shape/index.d.ts(304,16): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(312,22): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(757,16): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(765,22): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(935,16): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(943,22): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(1230,16): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(1238,22): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(1536,16): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(1544,22): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(1650,15): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(1686,15): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(2016,16): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(2024,22): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(2196,16): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(2204,22): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(2273,19): error TS2304: Cannot find name 'CanvasPathMethods'.
../node_modules/@types/d3-shape/index.d.ts(2358,16): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-shape/index.d.ts(2366,22): error TS2304: Cannot find name 'CanvasRenderingContext2D'.
../node_modules/@types/d3-transition/index.d.ts(572,65): error TS2304: Cannot find name 'HTMLElement'.
../node_modules/@types/d3-transition/index.d.ts(583,104): error TS2304: Cannot find name 'HTMLElement'.
../node_modules/@types/d3-zoom/index.d.ts(20,37): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(33,29): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(37,19): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(54,28): error TS2304: Cannot find name 'Node'.
../node_modules/@types/googlemaps/index.d.ts(788,30): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(799,36): error TS2304: Cannot find name 'Node'.
../node_modules/@types/googlemaps/index.d.ts(812,26): error TS2304: Cannot find name 'Node'.
../node_modules/@types/googlemaps/index.d.ts(1136,20): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1137,22): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1138,18): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1139,22): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1140,23): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1141,23): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1142,29): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1143,24): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1220,21): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1225,25): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1236,17): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1653,32): error TS2304: Cannot find name 'Node'.
../node_modules/@types/googlemaps/index.d.ts(1668,64): error TS2304: Cannot find name 'Document'.
../node_modules/@types/googlemaps/index.d.ts(1668,75): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1669,27): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1692,64): error TS2304: Cannot find name 'Document'.
../node_modules/@types/googlemaps/index.d.ts(1692,75): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1693,27): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1709,64): error TS2304: Cannot find name 'Document'.
../node_modules/@types/googlemaps/index.d.ts(1709,75): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1710,27): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1941,32): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1942,28): error TS2304: Cannot find name 'Node'.
../node_modules/@types/googlemaps/index.d.ts(2394,36): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(2398,29): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(2455,37): error TS2304: Cannot find name 'HTMLInputElement'.
../node_modules/@types/googlemaps/index.d.ts(2616,40): error TS2304: Cannot find name 'HTMLDivElement'.
../node_modules/@types/googlemaps/index.d.ts(2663,37): error TS2304: Cannot find name 'HTMLInputElement'.
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: tsc
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! [PATH ON LOCAL MACHINE]\AppData\Roaming\npm-cache_logs\2018-04-19T16_10_21_812Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2```

Most helpful comment

@laurenzlong Thanks again! That helped me track down what I needed to do to solve my issue.

To anyone else running into this issue - I was able to get the functions to build successfully by modifying the functions build script to use tsc --skipLibCheck instead of tsc from within functions/package.json. This tells the typescript compiler to skip type checking of all declaration files.

All 21 comments

If you ran npm run build inside your functions folder, does it succeed?

If you ran npm run build inside your functions folder, does it succeed?

It does not. Same error.

Then it's something to do with your TypeScript set up or how you import modules inside your code. It's not a bug with firebase-tools.

@laurenzlong Ok, thanks for taking the time to help me out on this one. I guess I'm wondering why the firebase cloud functions even cares about which packages I include in my application. I've created a simplified demonstration on the error that I'm receiving. The cloud functions build fine until I include some type definitions such as @types/googlemaps. I would think that the cloud functions should build regardless of any type definition packages I include in the root level of this demonstration.

"npm run build" uses regular "tsc" to build your files. This has nothing to do with Firebase or Cloud Functions.

@laurenzlong Thanks again! That helped me track down what I needed to do to solve my issue.

To anyone else running into this issue - I was able to get the functions to build successfully by modifying the functions build script to use tsc --skipLibCheck instead of tsc from within functions/package.json. This tells the typescript compiler to skip type checking of all declaration files.

@dylanjmcdonald or put it in the tsconfig.json file:

    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    "skipLibCheck": true,
    /* Strict Type-Checking Options */
    "strict": true,

Thanks for the heads up, btw 👍

But what if you don't want to skip checking the declaration? In one of my cloud functions I use Objects.entries() which is es2017. I set it in tsconfig.json but because it's skipping it it throws an error when the function is executed. Any other way around this?

I fixed the same problem by adding:
"typeRoots": [
"./functions/node_modules/@types"
]
to the tsconfig.json in functions folder

This happens with the stock autogenerated typescript when functions are initialized for me.

Just the autogenerated code and imports that are exactly like they are in the examples.

It seems to be because tsc will look for types up your process tree and tries to typecheck everything in the @types files but nothing else so things go to shit?

I think the most correct solution to this is to do as Gongevangen suggested. add "node_modules/@types" to typeRoots so that the module resolution algorithm doesn't go nuts up the tree.

I got the same issus thanks to @dylanjmcdonald I have solved it by using tsc --skipLibCheck

@dylanjmcdonald tsc --skipLibCheck in ./functions/package.json worked for me, thanks!

I fixed by adding "dom" to compilerOptions.lib in ./functions/tsconfig.json .

{
  "compilerOptions": {
    "lib": ["es6", "dom"],
    "module": "commonjs",
    "noImplicitReturns": true,
    "outDir": "lib",
    "sourceMap": true,
    "target": "es6"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

This should work also. I used @yamitzky solution and worked for a function deploy. The below is usable as per a google dev's code I viewed.
{ "compilerOptions": { "lib": ["es6"], "module": "commonjs", "noImplicitReturns": true, "outDir": "lib", "sourceMap": true, "target": "es6", "skipLibCheck": true, "typeRoots": ["./functions/node_modules/@types"] }, "compileOnSave": true, "include": [ "src" ] }

It's great that there are work-arounds for this this, but one of these solutions should really be in the .tsconfig generated by firebase init. Does anyone know how we can get this issue reopened?

You could re-open this issue, but I can tell you that we probably won't be adding dom to the lib property of the template tsconfig.json. It's meant as a minimum to get a developer off the ground and that default doesn't need dom. If you feel strongly otherwise, please feel free to open a pull request on this repo changing the templates, but I don't think it's a change we would make.

Thanks sir

Make sure that your .ts file is same name as .js in lib and also include it in package.json

Just to clarify @gongevangen 's solution works but make sure to place the "typeRoots" key inside "compilerOptions":

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "typeRoots": [
      "./functions/node_modules/@types"
    ],
  },
  "compileOnSave": true,
  "include": [
    "src"
  ],
}

Kinda confused as to why this wouldn't be included in the boilerplate...or even mentioned anywhere in any of the Firebase tuts.

I agree with @evanb2 - this just cost me an hour! Surely it would be worth at least mentioning it here: https://firebase.google.com/docs/functions/typescript if not including it in firebase init?

God bless you Evanb2! :)

Was this page helpful?
0 / 5 - 0 ratings