storybook: Error: exports is not defined
I’m trying to use storybook for the first time, so I decided to follow the guides. I can get it working with the examples, but as soon as i pull in my own components i get exports is not defined. It doesn’t matter if I use the “Quick Start Guide” or the “Slow Start Guide (React)” I always get the same unhelpful error.
exports is not defined
ReferenceError: exports is not defined at Object.<anonymous> (http://localhost:6006/static/preview.bundle.js:43176:23) at webpack_require (http://localhost:6006/static/preview.bundle.js:679:30) at fn (http://localhost:6006/static/preview.bundle.js:89:20) at Object.<anonymous> (http://localhost:6006/static/preview.bundle.js:43132:76) at Object.<anonymous> (http://localhost:6006/static/preview.bundle.js:43142:30) at webpack_require (http://localhost:6006/static/preview.bundle.js:679:30) at fn (http://localhost:6006/static/preview.bundle.js:89:20) at loadStories (http://localhost:6006/static/preview.bundle.js:40160:3) at ConfigApi._renderMain (http://localhost:6006/static/preview.bundle.js:41134:20) at render (http://localhost:6006/static/preview.bundle.js:41092:17) at ConfigApi.configure (http://localhost:6006/static/preview.bundle.js:41117:9) at Object.<anonymous> (http://localhost:6006/static/preview.bundle.js:40164:68) at Object.defineProperty.value (http://localhost:6006/static/preview.bundle.js:40165:30) at webpack_require (http://localhost:6006/static/preview.bundle.js:679:30) at fn (http://localhost:6006/static/preview.bundle.js:89:20) at Object.window.STORYBOOK_REACT_CLASSES (http://localhost:6006/static/preview.bundle.js:38867:18) at webpack_require (http://localhost:6006/static/preview.bundle.js:679:30) at http://localhost:6006/static/preview.bundle.js:725:39 at http://localhost:6006/static/preview.bundle.js:728:10
That error doesn’t really help much, and when I look up the error I end up at some issues from last year all saying that this problem has been patched out… I know that it’s probably my component that is exported in some way that storybook doesn’t like. But since all I’m getting is exports is not defined (along with some mess of a stacktrace) its kind of hard to debug.
I’m using typescript and I’m compiling it with just plain old tsc.
//tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"jsx": "react",
"declaration": true,
"sourceMap": true,
"outDir": "./dist",
"esModuleInterop": true
},
"include": [
"./src/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
And then importing the compiled js into storybooks.
//index.stories.jsx
import React from 'react';
import { storiesOf } from '@storybook/react';
import { action } from '@storybook/addon-actions';
import { MatrixEffect } from '../dist/index';
storiesOf('MatrixEffect', module)
.add('100x100', () =>
<MatrixEffect height={100} width={100} />
);
Version
@storybook/react3.4.0@storybook/addon-actions3.4.0babel-core6.26.0react16.3.0
What am I missing? (if there’s a way to just import the ts straight away then that would preferable. But since I haven’t found any official docs for it, this is what I’ve got so far)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 46
- Comments: 83 (20 by maintainers)
Commits related to this issue
- CHANGE exclude to a regex matching all node_modules folder https://github.com/storybookjs/storybook/issues/3346 — committed to storybookjs/storybook by ndelangen 5 years ago
I’m getting this error without using TypeScript, just vanilla JS
this issue can be fixed by adding the correct plugin in the
.babelrcfile, since thetsconfigfile is configured to generatecommonjscompatible modules, we need to use the proper pluginThis is what I have in my
.babelrcfile and everything is working fine, I have mytsconfigfile with exactly the same options and values."@babel/core"“^7.1.0”"@storybook/react"^4.0.0-alpha.2""react"“^16.4.0”Note: This solution works for another kind of modules https://babeljs.io/docs/en/next/plugins#modules
I had the exact same issue, and for me the solution was switching from using
transform-es2015-modules-commonjsto@babel/plugin-transform-modules-commonjsonbabel.config.js.before
after
I’m running into this, in the browser I get
exports is not definedbut in the terminal I get `“export ‘default’ (imported as ‘[ComponentName]’) was not found in ‘@[namespace]/[package-name]’”project/packagesthe error shows upIf I change the internal dependency’s package.json main config to the non-built source everything works
So there’s something wrong with storybook’s webpack config and importing cjs into es module code, or something …
My fix
So I realized I accidentally deleted my package.json “module” field that pointed to the ES module version of my builds, adding that back in makes everything work again. Still seems like storybook should be able to pull the cjs version, but my problem is solved 🤷🏽♂️
Maybe unrelated, but I was having this
exports is not definedissue because of my custombabel.config.js, reading https://storybook.js.org/docs/configurations/custom-babel-config/ solved my particular problem.For me the issue is caused with the recent changes to include babel-loader in 4.0.0.alpha. The default server webpack config could include your commonjs compiles (packages, workspaces): https://github.com/storybooks/storybook/blob/b2b73596f9dd97b4ba8c03340bd36f868e836772/lib/core/src/server/config/webpack.config.dev.js#L78 https://github.com/storybooks/storybook/blob/b2b73596f9dd97b4ba8c03340bd36f868e836772/lib/core/src/server/config/utils.js#L3
For me, a fix is to override the default plugin declare with a custom webpack.dev.js:
https://github.com/psychobolt/react-rollup-boilerplate/blob/d9cb9179cb7c00baab486646c504110bf7e2f50a/.storybook/webpack.config.js#L7
https://github.com/psychobolt/react-rollup-boilerplate/blob/d9cb9179cb7c00baab486646c504110bf7e2f50a/.storybook/webpack.config.js#L11
Omitting
includealso fixes the issue and has no side effects for me.We are using storybooks + lerna + typescript. What solved for us was mixing @i-like-robots with @psychobolt:
This is most likely caused by Webpack wrapping a CommonJS module with its ESM wrapper. Webpack will use an ESM wrapper if it sees any usage of
importin the module. It’s usually caused by either:To avoid the second case you’ll need to set Babel’s
sourceTypeto"unambiguous"to force it to check the module type first.https://github.com/i-like-robots/broken-webpack-bundle-test-case
Update: My original comment is hidden above but this is the base configuration we have been using to resolve these issues across multiple monorepo projects:
I spent the day on this issue, I already had the
sourceType: 'unambigous'.For my part, it was not linked to a
node_modulesfolder to ignore since it is a relative file right next to it.A workaround that works for me is to force the option
modules: 'cjs'for the@babel/preset-env.I also have this problem with
@storybook/react@next, the final solution for me was to manually add the babel plugin@babel/plugin-transform-modules-commonjs, while with thedebug: trueoption on the@babel/preset-envI see that it is already used… I don’t understand but it works.EDIT: This is not a solution because it loses the benefits of ESM modules with webpack. I need to force transform to cjs only for the storybook builds.
🎉 My
.storybook/.babelrc: 🎉I solved the issue by creating a
.storybook/weback.config.jsfile containing the following:Seems like it does boil down to the
sourceTypebabel issue. I tried adding a.babelrcfile (as suggested by @0nn0), but that seemed to replace the entire babel configuration rather than modify it, causing further issues.[UPDATED] - We have to exclude node_modules from this rule otherwise it will break the build
I have resolved it by adding this rule in storybook main.js file
Along with this, you may also need to disable the eslint validations for your dist folder, so for that, you can use below script
Not sure if this will help anyone else but we fixed this issue by running the following:
@ryanflorence I have the exact same lerna setup and the exports issue for storybook. I have a package which exposes the built version of the UI-Elements. Apologies but could you provide more details when you say,
"module" field that pointed to the ES module version of my builds, adding that back in makes everything work again.@sayjeyhi yeah, it should. This is not actually
Storybookissue. This happens just because when you work inmonorepo, you havenode_modulesnot only in your project root, but in*/packagesas well, which is not excluded by default. (I actually not sure that it shouldn’t, because itsmonorepoorganization specific. If you create yourStorybookaspackagein Lernapackagesfolder you won’t have this issue)So for my case I just did this in
.storybook/webpack.config.js:Had the same issue, also due to removing
modulefield. Adding@babel/plugin-transform-modules-commonjsto babel plugin helped, as described in this comment: https://github.com/storybooks/storybook/issues/3346#issuecomment-423719241@skeet I wondered why storybook had the message
So, after installing react-scripts, it now says
I managed to fix the issue by adding the following line to the
.babelrcfile:More info on this option: https://babeljs.io/docs/en/options#sourcetype Believe this option is only available with Babel 7 and up.
We have same issue, when r u going to fix it ?
We have also had the
exports is not definedproblem a few times and we’ve previously worked around it by overriding the Babel configuration as suggested by others.But, I recently started to look into this again and I noticed that the exclude property for the default JS rule was being resolved to an absolute path (below is a
console.log()of the generated Webpack config):I had assumed the
excludeproperty should be a RegExp so I thought it looked odd! I realised that due to the structure of our project we actually have manynode_modulesfolders so I tried updating this line to a RegExp (/node_modules/) - and it fixed it!This avoids transpiling modules which have already been transpiled - and in particular avoids the
useBuiltinsoption ofpreset-envfrom injectingcore-jspolyfills (removing theuseBuiltinsoption or setting the environment to target runtimes which do not require any polyfills can also help to avoid this issue.)So there are a few different issues at play which result in this problem:
node_modulesfolders can be unintentionally transpiled by BabeluseBuiltinsoption can injectcore-jspolyfills into your code in the wrong format for the module type (see https://github.com/babel/babel/issues/7463 and https://github.com/babel/babel/issues/9187)Unfortunately it is not very easy to extend the existing exclude option, but it is possible!
I’ll try to take a look asap
@ndelangen ok, here’s the repro repo 😂
https://github.com/busticated/storybook-monorepo-repo
you should be able to simply:
git clonenpm i && npm start…and see storybook attempt to load in the browser. open dev tools’ console and you’ll see the
exportserror.couple of notes:
npm run buildto test the prepublish buildnode@8andnpm@5to align w/ day job)"module": "src/index.jsx"fields topackages/*/packge.jsonfiles. if you revert those, you’ll see the originalexport 'default' (imported as 'Two') was not foundwebpack warning.Sounds like I have the identical issue.
@busticated I’d be happy to take a look at a monorepo-repro-repo 🙈
It’s likely that:
Unfortunately
ReferenceError: exports is not defineddoesn’t tell us anything other then, something is not as it’s supposed to be.Hey @aperkaz, I have updated the rule to exclude
node_modules, I found that storybook was launching properly in dev mode however breaking in production mode because of this change. So I had to excludenode_modulesin order to fix. You can take the latest from my updated comment above.Huzzah!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.0.0-alpha.0 containing PR #8822 that references this issue. Upgrade today to try it out!
You can find this prerelease on the
@nextNPM tag.Closing this issue. Please re-open if you think there’s still more to do.
@qrosmeli Thanks for the tip. You saved my day! 🚀
Sorry for bringing confusion, I’ve based my answer on Lerna configuration where required packages are hoisted to the root and installed there as dev dependencies. So I did’t experience the problem of parsing their
node_modules.It seems that some user have a use case where they have installed packages deeper in the tree like
/lib/components/node_moduleshttps://github.com/storybookjs/storybook/issues/3346#issuecomment-475437230 and babel-loader tries to parse them.By default storybook excludes root
node_modulesbut maybe it’s worth to exclude all of them.https://github.com/storybookjs/storybook/blob/f7367b18ec7d6e077fba5b99da89233b63c4f280/lib/core/src/server/config/utils.js#L5-L6
https://github.com/storybookjs/storybook/blob/f7367b18ec7d6e077fba5b99da89233b63c4f280/lib/core/src/server/common/babel-loader.js#L1-L13
Seems to be fixed in version 5.2
I’m using Lerna, internal packages are bundled by Webpack with output
libraryTarget: 'commonjs2'. Approach based on @JasonTheAdams comment works for me. I’ve also tested @0nn0 solution with babelrc{ "sourceType": "unambiguous" }and it also works however it requires to have.babelrcin package root.Some basic setup - maybe it will help someone 😉(Storybook: 5.1.10, Lerna: 3.16.4, Webpack: 4.39.1, Babel: 7.5.5)
file: lerna_repo/.storybook/webpack.config.js - doesn’t exist by default
file: lerna_repo/stories/index.stories.js
file: lerna_repo/packages/examplePackage/package.json
file: lerna_repo/packages/examplePackage/dist/index.js - generated by Webpack
I have created a reduced test case for this problem with information on how to avoid it. I will file an issue with Babel if necessary.
https://github.com/i-like-robots/broken-webpack-bundle-test-case
We also stumbled upon this issue and it took more more of a half a day fighting configs to figure it out what it might be. 😢
This is still happening for me in v4.0.0-alpha.20 with babel 7.0.0
If anyone is still encountering this, here’s what worked for me:
Perhaps because my Babel config is named
babel.config.jsinstead of.babelrc, it seems like Storybook did not identify it. I had to configure Storybook to explicitly read my existing Babel config (https://storybook.js.org/docs/react/configure/babel#custom-configuration), so mymain.jsnow looks like this:I also had to modify the React imports of the example from
import React from 'react'toimport * as React from 'react'but I guess it depends on your specific setup.@pixeleate Would you mind sharing your working repo?
Also want to chime in here. Using Storybook 5, Babel 7.4 (with core-js 3), TypeScript 3.4 and a monorepo. Most of these suggestions did not work 100%, but something I realized did. The very nature of monorepos is that a package is importing the “built” files from another package, not the source files, which is true when in the NPM registry/node module layer, but in development, is quite annoying. To get around this, I defined Webpack aliases that forwarded
lib/tosrc/, and everything just worked, especially since all code is now ESNext/ESM!Here’s the hack:
And to get Babel + TS working, I opted to mutate the existing babel-loader instead of adding a new one, as my local Babel config isn’t 100% compatible with Storybook’s, but their own config is.
@tmeasday Got it. I’m starting to wrap my head around this issue. What’s tricky is that, in my situation, I actually do want it to compile the child
node_modulesas that’s where I’m developing the components themselves, treating it as a separate package.I think @skeet’s suggestion, as echoed by @jcousins-ynap, is probably the best solution here. We don’t want Storybook to make assumption on how sub-packages are handled (i.e. ignoring their node_modules). If someone didn’t want the sub-modules node_modules compiled, they likely wouldn’t have installed the package dependencies to begin with.
It seems to all come down to babel + webpack’s ability to recognize CommonJS vs ES6 modules, which doesn’t seem to be perfect. Adding the
"module":field to the sub-package’spackage.jsonexplicitly informs babel that the package uses ES6 modules, which is the safest way to approach this.Thank you for your attention to this!
After applying @skeet’s suggestion (https://github.com/storybooks/storybook/issues/3346#issuecomment-459308703) the issue returned when I started referencing one package as a dependency (not peer or dev) of a few others.
Putting the
modulefield into the dependency’spackage.jsonas in @ryanflorence’s fix (https://github.com/storybooks/storybook/issues/3346#issuecomment-415982589) did the job.Just to drive by on this one without fully understanding what people’s issues are (apologies!) – here’s a snippet from my webpack config that works around this issue, maybe in a simpler way:
See https://github.com/storybooks/storybook/issues/3346#issuecomment-425516669
@0nn0 by following https://github.com/storybooks/storybook/issues/3346#issuecomment-425516669 this advice 😃
I removed the babel loader from
webpack.config.jsin the.storybookfolder and it works fine now. I don’t use Typescript though.I just ran into an issue with the same error message in a storybook after turning on yarn workspaces in a lerna project. I suspect that this is caused by package loading issues. Investigating further.
I’m getting
export 'MatrixEffect' was not found in '../dist/index'in the terminal. But I can import the module in just a plain node runtime (can’t use it ofc, but at least i know it can be imported).