Storybook 5 for React Native: "Can't find variable: document"
See original GitHub issueDescribe 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]
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:19 (7 by maintainers)
Top Results From Across the Web
React Native: can't find variable document - Stack Overflow
I am trying to add EventListner to document object but getting error as cant find variable document . Below is my code import...
Read more >Frequently Asked Questions - Storybook - JS.ORG
Storybook is a frontend workshop for building UI components and pages in isolation. Thousands of teams use it for UI development, testing, and...
Read more >Environment variables - Storybook - JS.ORG
You can use environment variables in Storybook to change its behavior in different “modes”. If you supply an environment variable prefixed with STORYBOOK_ ......
Read more >Component Story Format (CSF) - Storybook
Every component story file consists of a required default export and one or more named exports. CSF is supported in all frameworks except...
Read more >Configure Storybook
Thousands of teams use it for UI development, testing, and documentation. ... The main.js configuration file is a preset and, as such, has...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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?
emotion
utilizes thebrowser
andreact-native
fields within its package.json (more information here). Unexpectedly, when compiling,emotion-theming
and@emotion/core
are resolving to utilize thebrowser
dist files.This is misleading because the Haul config appears to correctly set
resolve.mainFields
to['react-native', 'browser', 'main']
, which defines thatreact-native
should take prescedence.Solutions
It turns out that there is another field,
aliasFields
, which webpack adds a default option for when running with theweb
orwebworker
target (Haul setstarget: 'webworker'
). ThealiasFields
will be resolved regardless of what is set inmainFields
.The question now becomes, is it correct for webpack to resolve
aliasFields
regardless of if thealiasFields
exist inmainFields
? If the answer is yes, then Haul needs to update it’s config to addreact-native
toaliasFields
. 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.15
successfully.