Closed ROODAY closed 4 years ago
So I'm pretty sure it's my component library that's causing this as when I stub it out like defined here the error goes away. But then I get new errors about variables not being defined, as the objects/components I import from that library don't exist now. The thing is, I don't use the window
object anywhere in my components, but after they get built through Neutrino/webpack the built files have window (which I'm guessing is the issue).
How should I fix my component library so that window isn't called during the build stage of my gatsby app? Is there a neutrino.js/webpack configuration change I can make?
Hey, I haven't used Neutrino before but if it works during SSR rendering there should not be any issue. It's hard to debug such a repo without a small reproduction. The error says you're using window somewhere so make sure to wrap every window call in an if call or in a React hook so it's only called during client rendering.
Sorry to hear you're running into an issue. To help us best begin debugging the underlying cause, it is incredibly helpful if you're able to create a minimal reproduction. This is a simplified example of the issue that makes it clear and obvious what the issue is and how we can begin to debug it.
If you're up for it, we'd very much appreciate if you could provide a minimal reproduction and we'll be able to take another look.
Thanks for using Gatsby! 💜
Hmm I'm not really sure how a minimal reproduction would work here, because it seems that my issue isn't necessarily with Gatsby but with how webpack is building my component library (which I then use in my Gatsby app). But I made something that might help. Here's a zip that contains a default neutrino-react-components project and a gatsby-starter-default project that imports a component from the neutrino project (the zip is large though because I included node_modules
).
If you don't want the zip, here are the steps to make it yourself:
yarn create @neutrinojs/project <directory-name>
to create the neutrino starter (pick components -> React components and no linter)gatsby new gatsby-starter-default https://github.com/gatsbyjs/gatsby-starter-default
to get the gatsby starteryarn build
. Copy the contents of the build
dir into a folder in the gatsby starter node_modules
(e.g. test
)src/pages/index.js
, import the neutrino starter Example
component with import Example from "test/Example"
and place the component somewhereyarn develop
to confirm that component works. Then run yarn build
in the gatsby starter and get the issue.I think at the end of the day this is more of a Neutrino.js/Webpack issue so I get if it's out of scope for here and you want to close this issue. But if you have any insights for how to get webpack to not use window
while bundling that'd be great!
Closing this as I got a solution on stackoverflow! Tl;dr for future readers, you need to add neutrino.config.output.globalObject("this")
to your .neutrinorc.js
. E.g.
const reactComponents = require('@neutrinojs/react-components');
module.exports = {
use: [
reactComponents(),
(neutrino) => {
neutrino.config.output.globalObject("this");
}
],
};
Description
When I run build I see the above error. I know the typical fix is to wrap window such that it's not called during build time, but in this case my code isn't calling window:
I'm not sure why I'm getting errors from @babel/runtime, and I'm not sure whether to delete it or not. Does anyone have insight on this?
The main change between when this used to work and this new error is that I abstracted some of my code into a component library that is built/packaged using the Neutrino.js framework. Could it be the webpack configuration of my component library causing issues?
Steps to reproduce
Not sure how to make a minimal reproducible case here, but can be reproduced by cloning the
gatsby
branch of this repo, doingyarn install
and thenyarn build
.Expected result
Should build fine.
Actual result
Gets the above error.
Environment