0

This problem also occurs in angular universal:

enter image description here

I try to install node-fetch-polyfill and import it.

//npm install node-fetch-polyfill

import * as fetch from "node-fetch-polyfill"; 

ngOnInit() {
    if (typeof fetch !== 'function') {
      (global as any).fetch = fetch;
    }
}

But it still doesn't work, I don't use fetch directly in my code.

Finn
  • 1,323
  • 5
  • 24
  • 46

1 Answers1

0

Documentation said:

A light-weight module that brings window.fetch to Node.js

So I assume you would like to use the fetch only in server, so you should check whether you are on server or on client. Like this for eg:

if (typeof window !== 'undefined' && typeof fetch !== 'function') {
  (global as any).fetch = fetch;
}

BUT! The best if you use PLATFORM_ID!

import { PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformBrowser} from '@angular/common';
export class MyComponent {
    isBrowser: boolean;
    constructor(
        @Inject(PLATFORM_ID) platformId: string) {
            this.isBrowser = isPlatformBrowser(platformId);
    }
    ngOnInit() {
       if (!this.isBrowser && typeof fetch !== 'function') {
        ...
     }
tdev
  • 136
  • 4