Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: Cannot find name 'setImmediate'

In my .netcore app I upgraded my Typescript version from 2.4.1 to 3.2.4 and now I am receiving the error Cannot find name 'setImmediate' at compile time. The implementation I have below is basically straight from the Microsoft guide on pre-rendering.

export default createServerRenderer(params => {
    const providers = [
        { provide: INITIAL_CONFIG, useValue: { document: '<app></app>', url: params.url } },
        { provide: APP_BASE_HREF, useValue: params.baseUrl },
        { provide: 'BASE_URL', useValue: params.origin + params.baseUrl },
    ];

    return platformDynamicServer(providers).bootstrapModule(AppModule).then(moduleRef => {
        const appRef: ApplicationRef = moduleRef.injector.get(ApplicationRef);
        const state = moduleRef.injector.get(PlatformState);
        const zone = moduleRef.injector.get(NgZone);

        return new Promise<RenderResult>((resolve, reject) => {
            zone.onError.subscribe((errorInfo: any) => reject(errorInfo));
            appRef.isStable.pipe(first(isStable => isStable)).subscribe(() => {

                setImmediate(() => { // <!-- error here
                    resolve({
                        html: state.renderToString(),
                        globals: {

                        }
                    });
                    moduleRef.destroy();
                });

            });
        });
    });
});

I saw that there was a bug raised for this, however it is marked as resolved and merged. I also tried installing the setImmediate package and importing that, however that didn't work either. Anyone know how to resolve this ?

like image 714
Zze Avatar asked Jan 28 '26 09:01

Zze


1 Answers

Non webpack:

Installing npm install --save-dev @types/node and then importing import '@types/node'; at the start of the file resolved this issue for me.


Webpack:

If you are using webpack then this will not work. Instead npm install --save-dev @types/node node then you will have to:

Go into your .tsconfig and add 'node':

{
  "compilerOptions": {
      "types": [..., "node" ],
      ...
   }
}

do not import '@types/node'; this is for the reasons stated here:

@types packages are stored in the @types directory but they must never be manually imported. Rather, you need to import the dependency they correspond to from where that dependency is installed. @types packages provide the type declarations for dependencies that do not otherwise provide them.

like image 164
Zze Avatar answered Jan 30 '26 23:01

Zze