在 Angular 中添加 PWA 时超出最大调用堆栈大小

Maximum call stack size exceeded when adding PWA in Angular

我正在尝试在我的应用程序中安装 service worker 但它显示 执行以下命令后出现以下错误。

ng 添加@angular/pwa --project ServiceWorkerdemo

ng add @angular/pwa --project ServiceWorkerdemo
Installing packages for tooling via npm.
npm WARN rollback Rolling back node-pre-gyp@0.10.0 failed (this is probably harmless): EPERM: operation not permitted, lstat 'path\ServiceWorkerdemo\node_modules\fsevents\node_modules'
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ @angular/pwa@0.11.3
updated 1 package and audited 47713 packages in 25.858s
found 0 vulnerabilities

Installed packages for tooling via npm.
**Maximum call stack size exceeded**

它显​​示 超出最大调用堆栈大小 错误消息。

即使我清除了缓存,它仍然再次显示相同的错误。

    >npm cache clean --force
npm WARN using --force I sure hope you know what you are doing.

**path\ServiceWorkerdemo>ng add @angular/pwa --project ServiceWorkerdemo
Installing packages for tooling via npm.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ @angular/pwa@0.11.3
updated 1 package and audited 47713 packages in 26.054s
found 0 vulnerabilities

Installed packages for tooling via npm.
Maximum call stack size exceeded

Instead of marking as duplicate kindly suggest me an answer, I have already tried clear cache option but still showing an error message. You can see in Image also.

删除节点包后。

提前致谢。

你可以查看这个 SO post 的解决方案,它说这个错误是由一个函数调用另一个函数等等引起的,直到它达到调用堆栈限制。这称为函数的递归循环。

为了修复它,请确保您的递归函数有一个能够满足的基本情况:

(function a(x) {
    // The following condition 
    // is the base case.
    if ( ! x) {
        return;
    }
    a(--x);
})(10); 

您可以查看 Chrome 开发工具的资源选项卡。

除非你使用 Angular 7 或更高版本,否则你需要执行类似

ng add @angular/pwa@v6-lts --project ...

确保安装了适用于您的 Angular 版本的正确模块版本。

(另见 https://github.com/angular/angular-cli/issues/12914

我遇到了与您 (Angular 6.1) 完全相同的错误,并且能够通过这种方式成功解决它。

我遇到了同样的问题。我能够通过指定以前的稳定版本来让它工作:

ng 添加@angular/pwa@0.8.7 --project my-project-name