wxt icon indicating copy to clipboard operation
wxt copied to clipboard

Build errors when leveraging `wxt/storage`: `You must add the 'storage' permission to your manifest to use 'wxt/storage'`

Open kboucher opened this issue 2 years ago • 13 comments

Describe the bug

Build error when attempting to utilize the wxt/storage module: Error: You must add the 'storage' permission to your manifest to use 'wxt/storage' (even after adding storage to manifest.permissions in wxt.config.ts).

To Reproduce

I am not including the prescribed minimal reproduction because the steps to reproduce are enough to encounter the problem, and because I already have the solution. However, I thought posting this issue might be helpful to others who may encounter this problem.

Steps to reproduce the bug:

  • Create a new WXT project
  • Run yarn dev
  • Import wxt/storage module into background.ts: import { storage } from "wxt/storage";
  • Define storage item in background.ts (outside of defineBackground)
  import { storage } from "wxt/storage";

  const isBugStorageItem = storage.defineItem<boolean>(
    "local:isBug",
    {
      defaultValue: false,
    },
  );

  export default defineBackground(() => {
    /* ...other stuff here... */
  });
  • Stop and start dev server
  • Observe build error: Error: You must add the 'storage' permission to your manifest to use 'wxt/storage'
  • Add storage permission to wxt.config.ts manifest section:
  manifest: {
    permissions: [
      "storage",
    ],
  },
  • Start dev server
  • Observe build error: Error: You must add the 'storage' permission to your manifest to use 'wxt/storage'

Expected behavior

A successful build is expected after adding the storage permission to the manifest.

Solution

As mentioned above this problem is encountered when attempting to reference storage from outside of the defineBackground definition object.

Note: I did not encounter this problem when referencing storage outside of React modules (Ex: In content, popup, or sidepanel entrypoints.)

So, to be explicit, moving the reference to storage into the body of the defineBackground definition object solved this issue:

export default defineBackground(() => {
  const isBugStorageItem = storage.defineItem<boolean>(
    "local:isBug",
    {
      defaultValue: false,
    },
  );
  
  /* ...other stuff here... */
});

kboucher avatar Jan 25 '24 19:01 kboucher

Thanks for the report! You can move the storage definition to another file and it will work.

Basically, you can't run any code outside the main function of the entrypoint files. Related to #336, see https://github.com/wxt-dev/wxt/issues/336#issuecomment-1883132507 for more details.

aklinker1 avatar Jan 26 '24 16:01 aklinker1

The entrypoint is imported during the build process in a node environment, but it doesn't know that. It sees that chrome.storage is undefined, so it thinks you're missing the storage permission.

aklinker1 avatar Jan 26 '24 16:01 aklinker1

I'm gonna close this as a duplicate, but I improved the error message.

Screenshot 2024-01-27 at 12 19 48 AM

aklinker1 avatar Jan 27 '24 06:01 aklinker1

Released error message improvements in https://github.com/wxt-dev/wxt/releases/tag/v0.14.7

aklinker1 avatar Jan 27 '24 17:01 aklinker1

I'm gonna close this as a duplicate, but I improved the error message.

Screenshot 2024-01-27 at 12 19 48 AM

Awesome. Thanks!

kboucher avatar Jan 29 '24 00:01 kboucher

@aklinker1 Hi, today I noticed that my extension suddenly displayed this error. What can I do?

Image

FJRG2007 avatar Mar 07 '26 03:03 FJRG2007

@FJRG2007 Looks like this is being reported at runtime? Are you trying to use it in a main world content script or some other context without access to the extension APIs?

aklinker1 avatar Mar 07 '26 14:03 aklinker1

Now that I think about it, it could be because it might be injecting itself into code on the web. I'll check it later to see if that's the case or if it's something else.

FJRG2007 avatar Mar 08 '26 14:03 FJRG2007

@FJRG2007 @aklinker1 Same one issue. \entrypoints\background\index.ts

Image

but after build background.js is

Image

Blon168 avatar Mar 09 '26 06:03 Blon168

@Blon168 Yes, the error message is included in the bundle. Are you seeing an error at runtime or somewhere else?

aklinker1 avatar Mar 17 '26 23:03 aklinker1

@Blon168 Yes, the error message is included in the bundle. Are you seeing an error at runtime or somewhere else?

in background.js after build.

Blon168 avatar Mar 19 '26 11:03 Blon168

@Blon168 Can you create a new issue with a reproduction for me to look into? Hard to help in this case if I can't see your full codebase.

aklinker1 avatar Mar 19 '26 13:03 aklinker1

@Blon168 Can you create a new issue with a reproduction for me to look into? Hard to help in this case if I can't see your full codebase.

Yes, done https://github.com/wxt-dev/wxt/issues/2203

Blon168 avatar Mar 19 '26 15:03 Blon168