question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storybook 5 for React Native: "Can't find variable: document"

See original GitHub issue

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

  1. yarn storybook
  2. yarn haul
  3. react-native run-ios
  4. see error
Screenshot 2019-06-13 at 12 49 25 AM

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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
jaridmargolincommented, Jun 21, 2019

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 the browser and react-native fields within its package.json (more information here). Unexpectedly, when compiling, emotion-theming and @emotion/core are resolving to utilize the browserdist files.

This is misleading because the Haul config appears to correctly set resolve.mainFields to ['react-native', 'browser', 'main'], which defines that react-native should take prescedence.

Solutions

It turns out that there is another field, aliasFields, which webpack adds a default option for when running with the web or webworker target (Haul sets target: 'webworker'). The aliasFields will be resolved regardless of what is set in mainFields.

The question now becomes, is it correct for webpack to resolve aliasFields regardless of if the aliasFields exist in mainFields? If the answer is yes, then Haul needs to update it’s config to add react-native to aliasFields. 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:

import { createWebpackConfig } from "haul";

export default {
  webpack: env => {
    const config = createWebpackConfig({
      entry: './index.js',
    })(env);

    // The fix :)
    config.resolve.aliasFields = ['react-native', 'browser']

    return config;
  }
};
1reaction
apexskiercommented, Jun 14, 2019

@SerhiyZheliznjak I’m using @storybook/react-native@5.1.0-alpha.8, @storybook/react@5.1.4, and haul@1.0.0-rc.15 successfully.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found