Skip to content

Vite 6 PWA build doesn't seem to resolve the correct file #19087

@jacquesg

Description

@jacquesg

Describe the bug

I've tried to upgrade from v5 to v6, but for some reason, the PWA build seems to not quite do what it did before.

This is the output for v6 in the repro repository

> [email protected] build /Users/jacquesg/github/sw-repro
> tsc && vite build

vite v6.0.6 building for production...
✓ 35 modules transformed.
dist/manifest.webmanifest                          0.47 kB
dist/index.html                                    0.70 kB │ gzip:  0.38 kB
dist/assets/react-CHdo91hT.svg                     4.13 kB │ gzip:  2.05 kB
dist/assets/index-BtyAWewE.css                     1.79 kB │ gzip:  0.86 kB
dist/assets/workbox-window.prod.es5-B9K5rw8f.js    5.72 kB │ gzip:  2.35 kB
dist/assets/index-BY4XzQdA.js                    147.58 kB │ gzip: 47.88 kB
✓ built in 242ms

PWA v0.21.1
Building src/sw.ts service worker ("es" format)...
vite v6.0.6 building for production...
[plugin:vite:resolve] [plugin vite:resolve] Module "util" has been externalized for browser compatibility, imported by "/Users/jacquesg/github/sw-repro/node_modules/.pnpm/[email protected]/node_modules/broadcast-channel/dist/esnode/methods/node.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
[plugin:vite:resolve] [plugin vite:resolve] Module "fs" has been externalized for browser compatibility, imported by "/Users/jacquesg/github/sw-repro/node_modules/.pnpm/[email protected]/node_modules/broadcast-channel/dist/esnode/methods/node.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
[plugin:vite:resolve] [plugin vite:resolve] Module "crypto" has been externalized for browser compatibility, imported by "/Users/jacquesg/github/sw-repro/node_modules/.pnpm/[email protected]/node_modules/broadcast-channel/dist/esnode/methods/node.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
[plugin:vite:resolve] [plugin vite:resolve] Module "os" has been externalized for browser compatibility, imported by "/Users/jacquesg/github/sw-repro/node_modules/.pnpm/[email protected]/node_modules/broadcast-channel/dist/esnode/methods/node.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
[plugin:vite:resolve] [plugin vite:resolve] Module "events" has been externalized for browser compatibility, imported by "/Users/jacquesg/github/sw-repro/node_modules/.pnpm/[email protected]/node_modules/broadcast-channel/dist/esnode/methods/node.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
[plugin:vite:resolve] [plugin vite:resolve] Module "net" has been externalized for browser compatibility, imported by "/Users/jacquesg/github/sw-repro/node_modules/.pnpm/[email protected]/node_modules/broadcast-channel/dist/esnode/methods/node.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
[plugin:vite:resolve] [plugin vite:resolve] Module "path" has been externalized for browser compatibility, imported by "/Users/jacquesg/github/sw-repro/node_modules/.pnpm/[email protected]/node_modules/broadcast-channel/dist/esnode/methods/node.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.
✓ 70 modules transformed.
dist/sw.mjs  41.99 kB │ gzip: 13.30 kB

For v5:

> [email protected] build /Users/jacquesg/github/sw-repro
> tsc && vite build

vite v5.4.11 building for production...
✓ 39 modules transformed.
dist/manifest.webmanifest                          0.47 kB
dist/index.html                                    0.70 kB │ gzip:  0.38 kB
dist/assets/react-CHdo91hT.svg                     4.13 kB │ gzip:  2.05 kB
dist/assets/index-BtyAWewE.css                     1.79 kB │ gzip:  0.86 kB
dist/assets/workbox-window.prod.es5-B9K5rw8f.js    5.72 kB │ gzip:  2.35 kB
dist/assets/index-B4lB10bN.js                    146.84 kB │ gzip: 47.51 kB
✓ built in 255ms

PWA v0.21.1
Building src/sw.ts service worker ("es" format)...
vite v5.4.11 building for production...
✓ 46 modules transformed.
dist/sw.mjs  10.91 kB │ gzip: 3.91 kB

From what I can tell, vite seems to be picking the node files instead for the browser files, despite the package.json for broadcast-channel having the following. The errors in the console I believe are related due to trying to use the incompatible node files instead of the browser version.

  "name": "broadcast-channel",
  "version": "7.0.0",
  "description": "A BroadcastChannel that works in New Browsers, Old Browsers, WebWorkers, NodeJs, Deno and iframes",
  "exports": {
    ".": {
      "node": {
        "types": "./types/index.d.ts",
        "import": "./dist/esnode/index.js",
        "default": "./dist/es5node/index.js"
      },
      "deno": {
        "types": "./types/index.d.ts",
        "import": "./dist/esbrowser/index.js",
        "default": "./dist/lib/index.js"
      },
      "browser": {
        "types": "./types/index.d.ts",
        "import": "./dist/esbrowser/index.js",
        "default": "./dist/lib/index.es5.js"
      },
      "default": {
        "types": "./types/index.d.ts",
        "default": "./dist/lib/index.es5.js"
      }
    },
    "./package.json": "./package.json"
  },

Reproduction

https://github.com/jacquesg/sw-repro

Steps to reproduce

pnpm i && pnpm build && pnpm preview

Open http://localhost:4173

The error console should show something like the following:

image

System Info

System:
    OS: macOS 15.2
    CPU: (14) arm64 Apple M3 Max
    Memory: 2.55 GB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.8.0 - ~/.nvm/versions/node/v22.8.0/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.8.2 - ~/.nvm/versions/node/v22.8.0/bin/npm
    pnpm: 9.12.2 - ~/.nvm/versions/node/v22.8.0/bin/pnpm
  Browsers:
    Brave Browser: 131.1.73.104
    Chrome: 131.0.6778.205
    Safari: 18.2
  npmPackages:
    @vitejs/plugin-react: ^4.2.1 => 4.3.4 
    @vitejs/plugin-react-swc: ^3.7.2 => 3.7.2 
    vite: ^6.0.6 => 6.0.6

Used Package Manager

pnpm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions