wxt: Uncaught (in promise) ReferenceError: window is not defined

Describe the bug

Tried with 0.19.2 and 0.18.15.

This error persists both in dev and prod builds.

image

Reproduction

Archive.zip

Steps to reproduce

Just run the extension form attached archive and see.

System Info

System:
    OS: macOS 14.5
    CPU: (11) arm64 Apple M3 Pro
    Memory: 76.47 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.4.0 - ~/.nvm/versions/node/v22.4.0/bin/node
    npm: 10.8.1 - ~/.nvm/versions/node/v22.4.0/bin/npm
    pnpm: 9.6.0 - ~/.nvm/versions/node/v22.4.0/bin/pnpm
    bun: 1.1.18 - ~/.bun/bin/bun
  Browsers:
    Chrome: 127.0.6533.72
    Safari: 17.5
  npmPackages:
    vite: ^5.3.5 => 5.3.5 
    wxt: ^0.18.2 => 0.18.15 

Used Package Manager

pnpm

Validations

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

So just to clearify, the problem was dynamic imports in a background marked as type: "module", right?

Solutions:

  1. Don’t use dynamic imports
  2. Don’t use type: "module"

If we need to keep this open, feel free to reopen!

Right. I’ve checked that using dynamic imports in the background(with type: "module") causes the same issue also in my project.

This is a trade-off.

  • Can’t avoid dynamic imports => avoid using type: "module" in the background
  • Want to use type: "module" in the background => avoid using dynamic imports

@1natsu172, thank you. Yes, I plan to use only the synchronous implementation. i18n-utils.async.ts is generated automatically, but I don’t use it.

@amal Sorry, I was misread your message. If you keep i18next and set type: "module", it will output chunks in ESM format. Without type: "module", it defaults to CJS format, resolving as static objects, and thus, the error doesn’t occur.

bundled with type: "module"(ESM) image

bundled without type: "module"(CJS) image

Dynamic import isn’t support in the background script, it might be better to use synchronous implementations instead.

I have encountered this error, which is actually an error of vite dynamic import failure. Let me guess, you used dynamic import in the background.

FYI - Background does not support the use of dynamic import. https://github.com/w3c/webextensions/issues/212