Installing Angular/Fire into Angular 17

5.7k Views Asked by At

I have an Angular 17 app and need to add Angular/Fire, the Angular Firebase version to it.

When i try to install it with npm install @angular/fire I am faced with a dependency error npm ERR! Could not resolve dependency: npm ERR! peer @angular/common@"^16.0.0" from @angular/[email protected] as the new version of Angular Fire hasn't been released yet.

I stumbled across this thread - https://github.com/angular/angularfire/issues/3459.

I am wondering how to install Angular Fire into 17? Should i use the next version or use ---force. And where is it initialised now that we no longer have app.module.ts? I used to in app.module.ts but I assume it would be done in app.config.ts or in main?

4

There are 4 best solutions below

0
Cobra On

This is becasue angular fire was not released for Angular 17.

Try: npm i @angular/[email protected]

1
Robgit28 On

I have spent a day or so looking for a work around and haven't found anything. For future travellers over the next week or two - if you're looking for a way to deploy an Angular 17 app to Firebase Hosting while working with the current version of Angular/Fire (16) or the pre-release version then it will not work together.

I tried -

  1. Getting Angular Fire 16 to work with Angular 17 - didn't work
  2. Tried using the pre-release version of Angular Fire 17 to work with Angular 17 for deployment to hosting and it didn't work. I tried ng add @angular/fire@next
  3. I then tried changing the package.json file and adding "overrides": { "@angular/fire": { "@angular/common": "16.0.1", "@angular/core": "16.0.1", "@angular/platform-browser": "16.0.1", "@angular/platform-browser-dynamic": "16.0.1" } } and risked running into runtime errors and conflicts and used npm install --force and I still can't deploy to hosting.

The next stable version of Angular/Fire should be released in the next couple of weeks but the Thanksgiving period may slow this down.

Also when installing and initialising Angular/Fire everything was set up in my app.config.ts file, which I used to have to do myself in NgModule. You'll still need to set up a separate set of env. folders.

0
Yuyinitos On

This seems to work with standalone in Angular 17.

app.config.ts

import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { provideRouter } from '@angular/router';

import { provideFirebaseApp, getApp, initializeApp } from '@angular/fire/app';
import { getFirestore, provideFirestore } from '@angular/fire/firestore';

import { routes } from './app.routes';
import { provideAnimations } from '@angular/platform-browser/animations';
import { environment } from '../environments/environment.prod';

export const appConfig: ApplicationConfig = {
  providers: [provideRouter(routes), provideAnimations(), importProvidersFrom([
    provideFirebaseApp(() => initializeApp(environment.firebase)),
    provideFirestore(() => getFirestore()),
  ])]
};

bookings.component.ts

import { Component } from '@angular/core';
import { inject } from '@angular/core';
import { Firestore, collectionData, collection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-bookings',
  standalone: true,
  imports: [],
  templateUrl: './bookings.component.html',
  styleUrl: './bookings.component.scss'
})
export class BookingsComponent {
  item$: Observable<any[]>;
  firestore: Firestore = inject(Firestore);

  constructor() {
    const itemCollection = collection(this.firestore, 'guests');
    this.item$ = collectionData(itemCollection);
  }
}

package.json

    {
  "name": "back",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^17.0.0",
    "@angular/cdk": "^17.0.1",
    "@angular/common": "^17.0.0",
    "@angular/compiler": "^17.0.0",
    "@angular/core": "^17.0.0",
    "@angular/fire": "^16.0.0",
    "@angular/forms": "^17.0.0",
    "@angular/material": "^17.0.1",
    "@angular/platform-browser": "^17.0.0",
    "@angular/platform-browser-dynamic": "^17.0.0",
    "@angular/router": "^17.0.0",
    "firebase": "^10.7.0",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.14.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^17.0.5",
    "@angular/cli": "^17.0.5",
    "@angular/compiler-cli": "^17.0.0",
    "@types/jasmine": "~5.1.0",
    "jasmine-core": "~5.1.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.2.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.1.0",
    "typescript": "~5.2.2"
  }
}
0
Hamed On

Personally I don't like to use --fore or anything else to force the application to work!
Instead I've found a solution that might be useful!
You can use ng add @angular/fire ,then this is Angular that decide which version of Angular Firebase will be installed in your project. I think it's gonna be a very good solution for maintainability!