storybook: Storybook 5 for React Native: "Can't find variable: document"
Describe the bug
I’m running into this issue “Can’t find variable: document” error when launching react-native app with storybook. I have commented out addon.js in index.js but it is still returning the same error.
To Reproduce git clone https://github.com/yongching/storybook-bug
- yarn storybook
- yarn haul
- react-native run-ios
- see error
Code Snippet package.json
{
"name": "StorybookBug",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"haul": "haul --platform ios --eager ios",
"storybook": "start-storybook -p 7007"
},
"dependencies": {
"react": "16.8.3",
"react-dom": "16.6.1",
"react-hot-loader": "4.8.0",
"react-native": "0.59.0"
},
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/runtime": "^7.4.5",
"@emotion/core": "^10.0.10",
"@storybook/addon-actions": "^5.1.3",
"@storybook/addon-links": "^5.1.3",
"@storybook/addons": "^5.1.3",
"@storybook/react-native": "^5.1.3",
"@storybook/react-native-server": "^5.1.3",
"babel-jest": "^24.8.0",
"babel-loader": "^8.0.6",
"emotion-theming": "^10.0.10",
"haul": "^1.0.0-rc.15",
"jest": "^24.8.0",
"metro-react-native-babel-preset": "^0.54.1",
"react-test-renderer": "16.8.3"
},
"jest": {
"preset": "react-native"
}
}
Expected behavior It should run without any issues.
System:
- OS: [e.g. iOS, MacOS]
- Device: [e.g. iPhoneX, Macbook Pro 2015]
- Framework: [e.g. react-native 0.59.0]
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 19 (7 by maintainers)
I’ve been able to track this pesky error down.
I am confident this is not an issue with Storybook, but instead, it is with webpack or Haul. The behavior I am seeing from webpack is what I would deem unexpected, but it is perhaps intended. I need to dig a little further to determine where exactly the fix should be introduced.
What is happening?
emotionutilizes thebrowserandreact-nativefields within its package.json (more information here). Unexpectedly, when compiling,emotion-themingand@emotion/coreare resolving to utilize thebrowserdist files.This is misleading because the Haul config appears to correctly set
resolve.mainFieldsto['react-native', 'browser', 'main'], which defines thatreact-nativeshould take prescedence.Solutions
It turns out that there is another field,
aliasFields, which webpack adds a default option for when running with theweborwebworkertarget (Haul setstarget: 'webworker'). ThealiasFieldswill be resolved regardless of what is set inmainFields.The question now becomes, is it correct for webpack to resolve
aliasFieldsregardless of if thealiasFieldsexist inmainFields? If the answer is yes, then Haul needs to update it’s config to addreact-nativetoaliasFields. If the answer is no, then webpack needs to be patched.Temporary Workaround
In the meantime, ya’ll can work around this by modifying your Haul config:
@SerhiyZheliznjak I’m using
@storybook/react-native@5.1.0-alpha.8,@storybook/react@5.1.4, andhaul@1.0.0-rc.15successfully.@alexakasanjeev, thank you for raising that point. Someone accidentally added the import to the template. Created PR https://github.com/storybookjs/storybook/pull/7096 to fix it.
I cloned https://github.com/yongching/storybook-bug this repo and I did run
yarn hauland it worked fine, also I explained in the comments above that if you are using react-native-server then you don’t need to add action addon, it will do automaticallyHi @SerhiyZheliznjak I opened a similar issue #6910 , please look into it.
Hi yongching, I cloned your repo and did all the steps (except I ran
react-native run-android), in my computer it is working fine, there is no document error. Could you try doing thisrm -rf node_modulesto delete node_modules folderyarn installand then your steps…Explanation: why you need to comment addon.js import Basically If you are using
@storybook/react-native-serveryou don’t need to import action addon.react-native-serverwill do it automatically (explained in this comment) so in yourstorybook/index.jscomment the line which is importing addon, and it will work fine.If it’s still unclear let me know.
@benoitdion I just did a quick experiment using metro bundler, it has a similar issue. Here’s the repo, https://github.com/yongching/storybook-with-metro.
Hi! Got the same issue, not using haul. @apexskier could you tell the version that is working?
haven’t played with the bug repro repo yet but based on what I’m seeing it looks like it may be an emotion issue with haul 😦
@apexskier @yongching I wonder if this is because you’re using haul. Are you able to reproduce with the metro bundler?