Skip to content

5.0.4 Does not compile in node environment / typescript definitions.. #880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
swftvsn opened this issue May 30, 2018 · 22 comments
Closed

5.0.4 Does not compile in node environment / typescript definitions.. #880

swftvsn opened this issue May 30, 2018 · 22 comments

Comments

@swftvsn
Copy link

swftvsn commented May 30, 2018

[REQUIRED] Describe your environment

  • Operating System version: High Sierra
  • Firebase SDK version: 5.0.4
  • Firebase Product: core

[REQUIRED] Describe the problem

Steps to reproduce:

Can't compile Node project. Compiles fine with 4.13.1.

Update: just tried this again, and 5.0.0 works, but 5.0.1, 5.0.2, 5.0.3 and 5.0.4 all fail with that message.

Relevant Code:

node_modules/firebase/index.d.ts(578,36): error TS2304: Cannot find name 'ServiceWorkerRegistration'.
@google-oss-bot
Copy link
Contributor

Hey there! I couldn't figure out what this issue is about, so I've labeled it for a human to triage. Hang tight.

@colincapurso
Copy link

I have the same problem. Had to go back to [email protected] to be able to deploy my functions.

@curvecode
Copy link

I also have same problem when upgrade to [email protected] when compile for my probject.

@henriquecf
Copy link

I am getting an error: error TS2339: Property 'auth' does not exist on type 'FirebaseNamespace'

@forest
Copy link

forest commented Jun 5, 2018

+1

1 similar comment
@kglowacki
Copy link

+1

@casperstorm
Copy link

I had the same problem:

error TS2304: Cannot find name 'ServiceWorkerRegistration'.
578 useServiceWorker(registration: ServiceWorkerRegistration): void;

A fix, as mentioned above, is to downgrade firebase to 5.0.0.

@WCarrollSTO
Copy link

After updating to 5.1.0, the error still exists, but the line number is different.

node_modules/firebase/index.d.ts:600:36 - error TS2304: Cannot find name 'ServiceWorkerRegistration'.

@mmermerkaya
Copy link
Contributor

A workaround is to add "dom" to the "lib" compiler option in your tsconfig.json.

@jshcrowthe This is not an issue with Messaging. Our packages assume that DOM types exist. The problem is that importing firebase/app imports typings of all packages.

Some of our packages (like Messaging) only work in the browser. These packages (or their types) should not be imported in a Node environment.

@mmermerkaya mmermerkaya assigned jshcrowthe and unassigned mmermerkaya Jul 2, 2018
@WCarrollSTO
Copy link

Some of our packages (like Messaging) only work in the browser. These packages (or their types) should not be imported in a Node environment.

@mmermerkaya , Is there a list of these packages somewhere? Messaging makes sense, but it would be nice to know which ones to avoid.

@mmermerkaya
Copy link
Contributor

There's this: https://github.com/firebase/firebase-js-sdk/blob/master/ENVIRONMENTS.md

It's missing Firestore and Functions at the moment, but there's already an issue about that: #914.

@WCarrollSTO
Copy link

@mmermerkaya , thank you very much! That will be very useful for us.

@shawnmitchell
Copy link

shawnmitchell commented Aug 3, 2018

A workaround is to add "dom" to the "lib" compiler option in your tsconfig.json.

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

So when you say add "dom" I'm not sure what exactly you mean. Can someone point me in the right direction? Thanks in advance!

The specific firebase method I'm looking to use in my cloud function is firebase.auth().sendSignInLinkToEmail(email, actionCodeSettings); If there's a more narrow way to pull that in that would avoid pulling in the whole library that would be awesome too.

@WCarrollSTO
Copy link

@shawnmitchell
You would want to change:
"lib": ["es6"],
To:
"lib": ["es6", "dom"],

@jshcrowthe jshcrowthe assigned Feiyang1 and unassigned jshcrowthe Sep 14, 2018
htbkoo added a commit to htbkoo/hey-hkul-hours-native-ns-vue that referenced this issue Jan 13, 2019
… error due to missing type (reference: firebase/firebase-js-sdk#880 (comment))

htbkoo/hey-hkul-hours-native-ns-vue
@carnesen
Copy link

carnesen commented Apr 15, 2019

The suggested/promoted workaround here of adding "dom" to compilerOptions.lib is NOT safe. That would be telling the compiler that any and all dom type references are valid in my code, which is clearly not the case since Node.js is not a browser environment. If this library is meant to be used as a Node.js TypeScript library, then it should not depend on ambient dom-specific type definitions. A safer workaround for TypeScript 3.0 users is to import firebase as follows:

/// <reference lib="dom" />
import * as firebase from 'firebase';

That first line is a "triple-slash lib directive" (https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html) first available in TypeScript 3.0. It's like adding dom to compilerOptions.lib but only for that one file. Note that the triple-slash directive must appear before any statements or it gets interpreted as just a normal comment.

To fix this problem officially and permanently we should remove "dom" from the tsconfig libs in this project and instead add the line /// <reference lib="dom" /> to the top of https://github.com/firebase/firebase-js-sdk/blob/6b53e0058483c9002d2fe56119f86fc9fb96b56c/packages/messaging-types/index.d.ts and other files with naked dom-type references.

@lookfirst
Copy link

Just curious why @carnesen 's suggestion hasn't been adopted yet. =(

It has also come up in this stack overflow...

https://stackoverflow.com/questions/51073986/cannot-find-name-serviceworkerregistration-error-when-creating-a-firebase-cl

@seankwalker
Copy link

Hi @mmermerkaya , this is still an issue. I understand that importing firebase/app imports all the packages, incl. those which should not be imported in a non-browser environment (as you mentioned). However, it is required to import "firebase/app" in order to initialize the app, right?

@carnesen
Copy link

I've since learned that /// <reference lib="dom" /> is not type-safe either because the included definitions are not scoped to the module containing the reference; they are added to the global ambient definitions. I no longer recommend that as a solution.

@hsubox76
Copy link
Contributor

Hi @mmermerkaya , this is still an issue. I understand that importing firebase/app imports all the packages, incl. those which should not be imported in a non-browser environment (as you mentioned). However, it is required to import "firebase/app" in order to initialize the app, right?

If I understand your question correctly, no, this is not the case. Importing firebase/app does not import all packages, only the JS SDK core code. Any packages you wish to use need to be imported in additional, separate import statements. Importing firebase, however, does import all packages.

@Feiyang1
Copy link
Member

@seankwalker A workaround is using scoped packages, e.g. @firebase/app, @firebase/auth.
The root of your problem is that firebase package aggregates all products, and we use a single typing file to describe them. We have plan to address this issue in the next major rewrite. For now, can you please try the workaround?

@seankwalker
Copy link

seankwalker commented Feb 13, 2020

thanks @Feiyang1 that worked perfectly! Sorry, I hadn't seen the @firebase/... imports in the docs.

@hsubox76
Copy link
Contributor

These issues don't seem applicable after the v9 modular rewrite, feel free to open a new issue for similar errors in v9 and above.

@firebase firebase locked and limited conversation to collaborators Jul 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests