将PWA添加到angular 8-模块构建失败

时间:2019-11-19 11:43:54

标签: javascript node.js angular typescript progressive-web-apps

我有angular 8应用。 在此应用程序中,我有三个项目。

我想用此命令添加i 0 1 2 3 4 5 6 7 a_i 0 1 0 0 3 4 0 2 prefix_sum 0 1 1 1 4 8 8 10 k 0 1 2 3 4 5 6 7 8 9 i = upper_bound(k) 1 4 4 4 5 5 5 5 7 7

@angular/pwa

但是在安装PWA时出现错误

  

错误   ./node_modules/@angular/service-worker/fesm2015/service-worker.js       模块构建失败:错误:ENOENT:没有此类文件或目录,请打开       'C:\ Development \ Repositories \ app \ node_modules @ angular \ service-worker \ fesm2015 \ service-worker.js'

在铬控制台上,我得到了

ng add @angular/pwa && ng build --prod

在PWA之前一切正常

编辑:我也尝试删除PWA并将其直接添加到项目中

Uncaught TypeError: Cannot read property 'id' of undefined
    at registerNgModuleType (core.js:34469)
    at core.js:34487
    at Array.forEach (<anonymous>)
    at registerNgModuleType (core.js:34483)
    at core.js:34487
    at Array.forEach (<anonymous>)
    at registerNgModuleType (core.js:34483)
    at core.js:34487
    at Array.forEach (<anonymous>)
    at registerNgModuleType (core.js:34483)

现在在项目ng add @angular/pwa --project app1 中,我得到了新文件src,在项目根目录中得到了manifest.webmanifest,在项目模块中得到了ngsw-config.json

但仍然会出现相同的错误

编辑:

这是我的文件夹结构。这就是所有角度CLI的添加方式。我需要添加另一个文件吗?

我的任何文件夹中都没有ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production })

此外,当我为gsw-worker.js打开ngsw-config.json时,我会得到

"$schema": "../../node_modules/@angular/service-worker/config/schema.json"

但是,如果我查看Unable to load schema from c:\Development\Repositories\app\app-frontend\node_modules\@angular\service-worker\config\schema.json': ENOENT: no such file or directory, open 'c:\Development\Repositories\app\app-frontend\node_modules\@angular\service-worker\config\schema.json' shema.json就在其中

node_modules

在ma.app.module.ts中的Mabey(在我的项目中)是问题所在,因为我需要注册(root -angular.json, package.json... there is no any manifest.webmanifest and ngsw-config.json) | | |----projects | |----app1 |------src | |------app | | |-app.module.ts (here is in imports ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })) | | | |-manifest.webmanifest | | ngsw-config.json tsconfig.json tsconfig.app.json tsconfig.spec.json ,但是我在我的应用程序中找不到此地方,可能是mabely angular CLI没有添加它,或者我没有。不需要这个文件吗?

4 个答案:

答案 0 :(得分:1)

请注意,添加PWA会通过以下命令将此功能添加到现有项目之一:

ng add @angular/pwa --project *project-name*

从更多角度查看官方文档,以获取更多信息:https://angular.io/guide/service-worker-getting-started

答案 1 :(得分:1)

您的应用程序中可能存在多个错误,请尝试使用

再次正确添加pwa。
ng add @angular/pwa --project *project-name*

然后用

构建
ng build --prod

这是供您参考的链接-https://angular.io/guide/service-worker-getting-started

Chrome控制台错误

您收到该错误,原因是您要从一个未定义的对象中访问ID,而该对象未定义为需要正确处理该对象

针对前let id = obj.id ? obj.id : '';

尝试先修复该错误,然后再构建它。如果它不起作用,请删除您的node_modules,然后再次安装并构建它。

希望这会有所帮助:)

答案 2 :(得分:1)

在将pwa安装到angular项目之前没什么要注意的。首先,您需要更新@ angular / cli以确保您具有最新版本

npm i -g @angular/cli

然后运行

ng new projectname

然后将pwa添加到您的项目中

ng add @angular/pwa --project *project-name*

并运行

ng build --prod

答案 3 :(得分:0)

使其生效的步骤...

  • npm uninstall @angular/pwa

  • npm cache clean

  

随机错误§   只需运行npm cache clean并重试即可解决一些奇怪的问题。   如果您在安装npm时遇到问题,请使用-verbose选项查看更多详细信息。

  • 所有ng update
  • 之后

这将解决此错误。 发生此错误是因为package.json中的某些依赖关系不是最新的,并且使pwa变得混乱。