← Back to Angular, page 7

angular-0134 · Medium mid

How do you configure an Angular app without NgModules, using bootstrapApplication, ApplicationConfig and provide functions?

bootstrapApplication takes the root component and an ApplicationConfig whose providers array holds provide functions such as provideRouter, provideHttpClient and provideZonelessChangeDetection. Features are passed as extra arguments (withComponentInputBinding, withInterceptors). Configs can be merged for server rendering or tests.

Full answer

Without modules, the app is started in main.ts:

bootstrapApplication(AppComponent, appConfig)

appConfig has type ApplicationConfig and contains providers. Each concern has a provide... function, and optional behaviour is added with with... feature functions:

  • provideRouter(routes, withComponentInputBinding(), withViewTransitions(), withPreloading(PreloadAllModules)).
  • provideHttpClient(withFetch(), withInterceptors([authInterceptor])). In the newest Angular versions fetch is the default backend and withFetch() is deprecated, so you only add withInterceptors(...) there.
  • provideZonelessChangeDetection() (stable from 20.2) or provideZoneChangeDetection({ eventCoalescing: true }).
  • provideBrowserGlobalErrorListeners() for global errors.
  • provideClientHydration(withEventReplay()) for server rendering.
  • provideAppInitializer(() => ...) for start-up work.
  • { provide: ErrorHandler, useClass: ... } and your own tokens.

Environment injector: these providers go in the root environment injector, so services are shared by the whole app. Providers on components or routes add narrower scopes.

Why this design: each feature is a function that can be tree-shaken, features compose through arguments, and there is no hidden global module state. It also makes testing easy: TestBed.configureTestingModule({ providers: [provideRouter([]), provideHttpClient()] }).

Combining configs: mergeApplicationConfig(appConfig, serverConfig) joins providers. For server rendering the server file bootstraps with mergeApplicationConfig(appConfig, { providers: [provideServerRendering()] }).

Other entry points: createApplication(config) creates an app without a root component, which is used for Angular Elements, and ApplicationRef.bootstrap can attach several roots. If you still have NgModules from a library, use importProvidersFrom(SomeModule) inside providers.

Keep appConfig small and ordered logically: routing, HTTP, change detection, UI, and observability. Order of unrelated providers does not matter, but for the same token the last one wins.

typescript
import { ApplicationConfig, ErrorHandler, mergeApplicationConfig, provideBrowserGlobalErrorListeners, provideZonelessChangeDetection } from '@angular/core';
import { provideHttpClient, withFetch, withInterceptors } from '@angular/common/http';
import { Routes, provideRouter, withComponentInputBinding, withViewTransitions } from '@angular/router';
import { HttpInterceptorFn } from '@angular/common/http';

const authInterceptor: HttpInterceptorFn = (req, next) => next(req);

class AppErrorHandler implements ErrorHandler {
  handleError(error: unknown): void {
    console.error(error);
  }
}

export const routes: Routes = [];

export const appConfig: ApplicationConfig = {
  providers: [
    provideZonelessChangeDetection(),
    provideBrowserGlobalErrorListeners(),
    provideRouter(routes, withComponentInputBinding(), withViewTransitions()),
    provideHttpClient(withFetch(), withInterceptors([authInterceptor])),
    { provide: ErrorHandler, useClass: AppErrorHandler },
  ],
};

// A second config, merged with the first (for example the server part of SSR).
export const serverConfig: ApplicationConfig = { providers: [] };
export const mergedConfig = mergeApplicationConfig(appConfig, serverConfig);

// main.ts:  bootstrapApplication(AppComponent, appConfig).catch(console.error);

Angular 20.3, type-checked. A Karma test checks the provider count and that the router and HTTP client providers from the list can be injected in TestBed. The full bootstrap was not run.

Key points

  • `bootstrapApplication(Root, appConfig)` starts a standalone app. Providers come from `provide...` and `with...` functions.
  • They go into the root environment injector. They are tree-shakable and easy to test.
  • `mergeApplicationConfig` combines configs, for example for SSR.
  • Use `importProvidersFrom` for legacy modules.

Common mistakes

  • Providing the same token twice and not realising the last wins.
  • Putting feature functions outside the matching provide function, for example `withInterceptors` alone.
  • Importing a legacy module's providers without `importProvidersFrom`.

Follow-ups

  • How do you test a service that needs the router and HTTP client?
  • What does `createApplication` give you that `bootstrapApplication` does not?
  • How does the server entry reuse the browser config?

Tips

  • Name the "provide plus with" pattern.
  • Show you know zoneless is a provider.
  • Mention testing and merging configs.

Evaluation rubric

  • Describes bootstrapApplication and ApplicationConfig.
  • Names common provide and with functions.
  • Knows merge, legacy module and testing options.