Angular-cli: Service Worker with ng build --prod error

Created on 6 Dec 2017  Â·  6Comments  Â·  Source: angular/angular-cli

> ng build --prod
Date: 2017-12-06T21:13:24.157Z
Hash: 3fb405a90a2041f6cbd3
Time: 116738ms
chunk {0} main.ff8fe03a5f3f52a1a90b.bundle.js (main) 1.47 MB [initial] [rendered]
chunk {1} polyfills.17a4fcb6e694c489ccd3.bundle.js (polyfills) 136 kB [initial] [rendered]
chunk {2} styles.5d12b9488551c2f2986e.bundle.css (styles) 31 kB [initial] [rendered]
chunk {3} inline.af36f1bf44c2ee9116c0.bundle.js (inline) 1.45 kB [entry] [rendered]

An error occured during the build:
TypeError: Cannot read property 'files' of undefined
    at Generator.<anonymous> (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:255:85)
    at step (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:71:23)
    at Object.next (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:52:53)
    at myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:45:71
    at new Promise (<anonymous>)
    at __awaiter (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:41:12)
    at myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:249:108
    at Array.map (<anonymous>)
    at Generator.<anonymous> (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:249:78)
    at step (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:71:23)
    at Object.next (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:52:53)
    at myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:45:71
    at new Promise (<anonymous>)
    at __awaiter (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:41:12)
    at Generator.processAssetGroups (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:244:16)
    at Generator.<anonymous> (myapp/node_modules/@angular/service-worker/bundles/service-worker-config.umd.js:224:51)
Cannot read property 'files' of undefined

> ng -v

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / â–³ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/

Angular CLI: 1.6.0-rc.2
Node: 8.9.1
OS: darwin x64
Angular: 5.0.5
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker

@angular/cli: 1.6.0-rc.2
@angular-devkit/build-optimizer: 0.0.34
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.39
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.0-rc.2
@schematics/angular: 0.1.9
typescript: 2.6.2
webpack: 3.10.0


If I set "serviceWorker": true in angular-cli.json I get the error above.

If I run "ng build" without --prod it works fine however my bundle size goes to 13Mb. If I set the serviceWorker to false it builds OK and my bundle is only 3Mb, however I have no PWA capability. It'd be great to have a small bundle + PWA goodness.

1 (urgent) broken

Most helpful comment

Sorry, just realized my own stupidity. The JSON was all wrong, after fixing it I can build fine:

{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "resources": {
      "files": [
        "/favicon.ico",
        "/index.html"
      ],
      "versionedFiles": [
        "/*.bundle.css",
        "/*.bundle.js",
        "/*.chunk.js"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/**"
      ],
      "urls": [
        "https://fonts.googleapis.com/**",
        "https://fonts.gstatic.com/**",
        "https://code.ionicframework.com/**"
      ]
    }
  }],
  "dataGroups": [
    {
      "name": "api",
      "urls": ["/chat"],
      "cacheConfig": {
        "strategy": "freshness",
        "maxSize": 50,
        "maxAge": "30m",
        "timeout": "5s"
      }
    }
  ]
}

All 6 comments

Hi @awsed,

How did you create your application? If you simply set "serviceWorker": true, you need to also create a src/ngsw-config.json file as described in our docs.

Sure, here's my src/ngsw-config.json:

{
    "index": "/index.html",
    "assetGroups": [{
      "name": "app",
      "installMode": "prefetch",
      "resources": {
        "files": [
          "/favicon.ico",
          "/index.html"
        ],
        "versionedFiles": [
          "/*.bundle.css",
          "/*.bundle.js",
          "/*.chunk.js"
        ]
      }
    }, {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "resources": {
        "files": [
          "/assets/**"
        ],
        "urls": [
          "https://fonts.googleapis.com/**",
          "https://fonts.gstatic.com/**",
          "https://code.ionicframework.com/**"
        ]
      }
    },
    {
      "dataGroups": [
        {
          "name": "api",
          "urls": ["/chat"],
          "cacheConfig": {
            "strategy": "freshness",
            "maxSize": 50,
            "maxAge": "30m",
            "timeout": "5s"
          }
        }
      ]
    }
    ]
  }

I'm having a similar issue when running an ng build --prod command, but only on VSTS. I get the following error:

Error: ENOENT: no such file or directory, scandir 'd:\a\1\s\node_modules\.bin\dist' 2017-12-12T11:04:44.6686490Z at Error (native) 2017-12-12T11:04:44.6686736Z at Object.fs.readdirSync (fs.js:953:18) 2017-12-12T11:04:44.6686865Z at CliFilesystem.syncList (d:\a\1\s\node_modules\@angular\cli\utilities\service-worker\index.js:18:28) 2017-12-12T11:04:44.6687002Z at CliFilesystem.list (d:\a\1\s\node_modules\@angular\cli\utilities\service-worker\index.js:14:37) 2017-12-12T11:04:44.6687201Z at Generator.<anonymous> (d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:257:66) 2017-12-12T11:04:44.6687341Z at step (d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:71:23) 2017-12-12T11:04:44.6687681Z at Object.next (d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:52:53) 2017-12-12T11:04:44.6687853Z at d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:45:71 2017-12-12T11:04:44.6687988Z at __awaiter (d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:41:12) 2017-12-12T11:04:44.6688120Z at d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:249:108 2017-12-12T11:04:44.6688260Z at Array.map (native) 2017-12-12T11:04:44.6688379Z at Generator.<anonymous> (d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:249:78) 2017-12-12T11:04:44.6688526Z at step (d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:71:23) 2017-12-12T11:04:44.6689205Z at Object.next (d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:52:53) 2017-12-12T11:04:44.6689371Z at d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:45:71 2017-12-12T11:04:44.6689523Z at __awaiter (d:\a\1\s\node_modules\@angular\service-worker\bundles\service-worker-config.umd.js:41:12)

When I don't set the --prod flag, my build works just fine. This only started happening after updating Angular to 5.1 (including Service Worker)

Not sure if it's useful but I get the error when running:

>./node_modules/.bin/ngsw-config dist src/ngsw-config.json
(node:82212) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): TypeError: Cannot read property 'files' of undefined
(node:82212) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Sorry, just realized my own stupidity. The JSON was all wrong, after fixing it I can build fine:

{
  "index": "/index.html",
  "assetGroups": [{
    "name": "app",
    "installMode": "prefetch",
    "resources": {
      "files": [
        "/favicon.ico",
        "/index.html"
      ],
      "versionedFiles": [
        "/*.bundle.css",
        "/*.bundle.js",
        "/*.chunk.js"
      ]
    }
  }, {
    "name": "assets",
    "installMode": "lazy",
    "updateMode": "prefetch",
    "resources": {
      "files": [
        "/assets/**"
      ],
      "urls": [
        "https://fonts.googleapis.com/**",
        "https://fonts.gstatic.com/**",
        "https://code.ionicframework.com/**"
      ]
    }
  }],
  "dataGroups": [
    {
      "name": "api",
      "urls": ["/chat"],
      "cacheConfig": {
        "strategy": "freshness",
        "maxSize": 50,
        "maxAge": "30m",
        "timeout": "5s"
      }
    }
  ]
}

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sysmat picture sysmat  Â·  3Comments

rajjejosefsson picture rajjejosefsson  Â·  3Comments

5amfung picture 5amfung  Â·  3Comments

delasteve picture delasteve  Â·  3Comments

rwillmer picture rwillmer  Â·  3Comments