Build errors when leveraging `wxt/storage`: `You must add the 'storage' permission to your manifest to use 'wxt/storage'`
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/storagemodule 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
storagepermission towxt.config.tsmanifest 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... */
});
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.
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.
I'm gonna close this as a duplicate, but I improved the error message.
Released error message improvements in https://github.com/wxt-dev/wxt/releases/tag/v0.14.7
I'm gonna close this as a duplicate, but I improved the error message.
![]()
Awesome. Thanks!
@aklinker1 Hi, today I noticed that my extension suddenly displayed this error. What can I do?
@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?
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 @aklinker1 Same one issue. \entrypoints\background\index.ts
but after build background.js is
@Blon168 Yes, the error message is included in the bundle. Are you seeing an error at runtime or somewhere else?
@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 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.
@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