node.js - 语法错误 : Cannot find name 'setImmediate'

标签 node.js typescript

在我的 .netcore 应用程序中,我将我的 Typescript 版本从 2.4.1 升级到 3.2.4,现在我收到错误 Cannot find name 'setImmediate ' 在编译时。我下面的实现基本上直接来自 Microsoft guide关于预渲染。

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();
                });

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

我看到那里was a bug raised for this ,但是它被标记为已解决和合并。我还尝试安装 setImmediate 包并导入它,但这也不起作用。任何人都知道如何解决这个问题?

最佳答案

非 webpack:

安装 npm install --save-dev @types/node 然后在文件开头导入 import '@types/node'; 解决了这个问题我。


网络包:

如果您使用的是 webpack,那么这将不起作用。相反,npm install --save-dev @types/node node 那么你将不得不:

进入您的 .tsconfig 并添加 'node':

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

不要 import '@types/node'; 这是出于 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.

关于node.js - 语法错误 : Cannot find name 'setImmediate' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54805607/

相关文章:

node.js - 将 ReadStream 从谷歌云存储上的文件传递到 POST FORM

javascript - forEach 循环内的异步 findOne() 操作

javascript - 为什么 React CLI 不将模板安装为 typescript ?

javascript - 为什么我无法访问主题上的管道方法?

javascript - nodeJS 和 MySql 中的连接池问题

javascript - 将服务器端多个 HTTP 请求快速路由到第 3 方 API

reactjs - 使用 props 有条件地设置样式组件中的属性,以使用 TypeScript 对主题对象进行索引?

Angular 无法编译 Typescript 的映射类型修饰符

javascript - 从服务器 discord.js 获取一个随机用户

javascript - 带有 setInterval 的 Angular 2 Observable 未使用异步管道在模板中更新