TechBuzzPro

Angular ng0950 Fix: Required Inputs Read Too Early

· 5 min read · Apps & Browsers

In this article
  1. What Is Angular Error NG0950?
  2. Why Does This Error Happen?
  3. How to Fix NG0950
  4. Common Scenarios and Their Fixes
  5. Why Required Inputs Are Stricter
  6. Preventing NG0950 in Your Code
  7. Frequently Asked Questions

If Angular throws error NG0950 (“Required input is read too early”), the fix is to stop reading a required @Input() before Angular has assigned it—usually by moving the read into ngOnInit() or a setter, or by using computed() instead of a field initializer. This error appears in Angular 16.1+ when a required input is accessed during component construction or field initialization, before the framework has had a chance to set it. Below is a complete explanation of why it happens, how to fix it, and how to avoid it in the future.

What Is Angular Error NG0950?

NG0950 is a runtime error introduced with Angular’s stricter input validation for required inputs. When you declare an input as @Input({ required: true }), Angular guarantees that the input will be set before the component’s ngOnInit() hook runs. However, if you try to read that input earlier—for example, in a field initializer, in the constructor, or in a property getter that runs during construction—Angular throws NG0950 because the value is not yet available.

The error message typically looks like:

NG0950: Required input 'name' is read too early.

This is part of Angular’s effort to catch bugs where components rely on input values before they are actually provided, which often leads to undefined or unexpected behavior.

Why Does This Error Happen?

Angular creates a component instance and then assigns inputs in a specific order. The constructor runs first, then property initializers, then the inputs are set (if they are bound in the template), and finally lifecycle hooks like ngOnInit() are called. Required inputs are guaranteed to be set before ngOnInit(), but not before the constructor or field initializers.

Consider this common mistake:

@Component({...})
export class MyComponent {
  @Input({ required: true }) name!: string;
  greeting = `Hello, ${this.name}!`; // NG0950
}

Here, this.name is read when the field greeting is initialized—before Angular has assigned name . Even if the parent template binds , the order of operations means the field initializer runs first.

Another common trigger is using an input inside a getter that is called during construction, or in a constructor body:

constructor() {
  console.log(this.name); // NG0950
}

How to Fix NG0950

The fix depends on where you are reading the input. Here are the most common solutions:

1. Move the read into ngOnInit()

If you only need the input once when the component initializes, move the logic into ngOnInit():

@Component({...})
export class MyComponent implements OnInit {
  @Input({ required: true }) name!: string;
  greeting!: string;

  ngOnInit() {
    this.greeting = `Hello, ${this.name}!`;
  }
}

2. Use an input setter

If you need to react to the input immediately when it is set, use a setter:

private _name = '';
@Input({ required: true }) set name(value: string) {
  this._name = value;
  this.greeting = `Hello, ${value}!`;
}
greeting = '';

3. Use computed() for derived values

If you are using signals, you can create a computed signal that reads the input lazily:

name = input.required<string>();
greeting = computed(() => `Hello, ${this.name()}!`);

This works because the computed is not evaluated until something reads greeting(), which happens after the input is set.

4. Use afterNextRender() or ngAfterViewInit() for DOM-dependent reads

If you need to read the input after the view is rendered, use ngAfterViewInit() or afterNextRender():

ngAfterViewInit() {
  console.log(this.name); // safe
}

Common Scenarios and Their Fixes

Here are a few typical situations where NG0950 appears and how to resolve them:

  • Using input in a field initializer – Move the initialization to ngOnInit() or use a setter.
  • Using input in a constructor – Move the logic to ngOnInit() or ngAfterViewInit().
  • Using input in a getter that is called during construction – Make the getter lazy (e.g., return a computed signal or a method called later).
  • Using input in a host binding or attribute binding – These are evaluated before inputs are set, so use a setter or ngOnInit() to update a property that the binding uses.

Why Required Inputs Are Stricter

Angular introduced required inputs to catch missing bindings at compile time. When you mark an input as required, the compiler will warn you if a parent template does not provide it. However, this strictness extends to runtime: Angular enforces that the input is set before ngOnInit(), and reading it earlier is considered a bug. This is a deliberate design choice to prevent subtle errors where a component might use an undefined value.

Preventing NG0950 in Your Code

To avoid this error, follow these best practices:

  • Never read inputs in field initializers or constructors.
  • Use ngOnInit() for one-time initialization that depends on inputs.
  • Use input setters when you need to react to input changes immediately.
  • Prefer signals with computed() for derived state that depends on inputs.
  • If you must read an input in a getter, ensure the getter is not called before ngOnInit() (e.g., by calling it from a template binding that runs after inputs are set).

Frequently Asked Questions

Can I ignore NG0950 and just use a non-required input?

Yes, you can remove required: true from the input decorator. Then Angular will not throw NG0950, but you lose the compile-time guarantee that the input is provided. You will need to handle undefined values yourself, which is often less safe.

Does NG0950 appear in production builds?

Yes, it is a runtime error, so it will appear in production if the condition is triggered. However, the Angular compiler may also warn during development if it can statically detect the issue.

Why does my input setter not throw NG0950?

Input setters are called by Angular when the input is assigned, which happens after construction. So reading the value inside a setter is safe. The error only occurs when you read the input before that assignment.

Can I use ngOnChanges() to read required inputs?

Yes, ngOnChanges() is called before ngOnInit() and after inputs are set, so it is safe to read inputs there. It is useful if you need to react to changes, but for one-time initialization, ngOnInit() is simpler.

Is NG0950 related to signals?

It can be. If you use signal-based inputs (input.required()), the same rule applies: you cannot read the signal value before it is set. However, using computed() avoids the issue because the read is deferred until the computed is evaluated.

What if I need to read the input in a host listener?

Host listeners are called after the component is initialized, so reading inputs there is safe. The error only occurs during the initialization phase (constructor, field initializers, and property bindings that run before inputs are set).