Menu

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

open
nobody
None
2025-05-22
2025-05-22
Anonymous
No

Originally created by: 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 -

https://github.com/GoogleChrome/workbox/blob/e26d8d7507f9412ba029922f3d9920e68710f2cf/packages/workbox-cli/src/lib/read-config.ts#L16

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?

Discussion


Log in to post a comment.