Skip to content

Error running generateSW - "Please pass in a valid CommonJS module that exports your configuration" #3413

@bburns

Description

@bburns

Following the instructions here - https://developer.chrome.com/docs/workbox/modules/workbox-cli -

$ npx workbox-cli wizard
...
To build your service worker, run
workbox generateSW workbox-config.js
as part of a build process.

$ npx workbox-cli generateSW workbox-config.js
Please pass in a valid CommonJS module that exports your configuration.
module is not defined

My project's package.json has

{
  "type": "module",
}

It looks like read-config uses node's require fn -

return require(configFile) as GenerateSWOptions | InjectManifestOptions;

I renamed workbox-config.js to workbox-config.cjs - then it works okay -

$ npx workbox-cli generateSW workbox-config.js
Using configuration from workbox-config.cjs.
The service worker files were written to:
• .../public/sw.js
• .../public/sw.js.map
• .../public/workbox-5911a5e4.js
• .../public/workbox-5911a5e4.js.map
The service worker will precache 62 URLs, totaling 179 kB.

So should the wizard default to writing a .cjs file instead of .js?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions