gatsby: Cannot read property 'Provider' of undefined. BaseContext from `@reach/router` is undefined.
Description
BaseContext
is undefined here.
Also, it looks like @reach/router
doesn’t export BaseContext 🤔. Ideally gatsby
shouldnt’ be relying on this import, if its not actually exported from @reach/router
.
NOTE: This issue goes away, if I fix the BaseContext
by copying over the snippet to re-create it from here and then patching the production-app.js
file inside cache-dir
.
const RouteHandler = props => (
<BaseContext.Provider
value={{
baseuri: `/`,
basepath: `/`,
}}
>
<PageRenderer {...props} />
</BaseContext.Provider>
)
Steps to reproduce
Issue arises especially when I use docker to create a docker image of the source code, and then build the gatsby image, copying the public
directory and then use tools like serve
to host the static files. This is NOT reproducible on gatsby build
or gatsby develop
, which makes it even more difficult to debug, on why this issue is happening. I’m unfortunately not able to provide more information due to lack thereof.
Expected Behaviour
Gatsby should handle the BaseContext
being undefined error, more gracefully.
Environment
System: OS: macOS 10.15.5 CPU: (12) x64 Intel® Core™ i7-9750H CPU @ 2.60GHz Shell: 5.7.1 - /bin/zsh Binaries: Node: 12.18.3 - ~/n/bin/node Yarn: 1.22.4 - ~/.yarn/bin/yarn npm: 6.14.7 - /usr/local/bin/npm Languages: Python: 2.7.16 - /usr/bin/python Browsers: Edge: 85.0.564.44 Safari: 13.1.1 npmPackages: gatsby: ^2.24.53 => 2.24.54 gatsby-link: ^2.4.13 => 2.4.13 gatsby-plugin-catch-links: ^2.3.5 => 2.3.11 gatsby-plugin-emotion: ^4.3.2 => 4.3.10 gatsby-plugin-gtag: ^1.0.12 => 1.0.13 gatsby-plugin-layout: ^1.3.10 => 1.3.10 gatsby-plugin-manifest: ^2.2.33 => 2.4.28 gatsby-plugin-mdx: ^1.0.61 => 1.2.38 gatsby-plugin-nprogress: ^2.3.2 => 2.3.10 gatsby-plugin-offline: ^3.2.11 => 3.2.27 gatsby-plugin-react-helmet: ^3.1.18 => 3.3.10 gatsby-plugin-remove-serviceworker: ^1.0.0 => 1.0.0 gatsby-plugin-remove-trailing-slashes: ^2.3.5 => 2.3.11 gatsby-plugin-sitemap: ^2.2.24 => 2.4.12 gatsby-remark-autolink-headers: ^2.3.5 => 2.3.12 gatsby-remark-copy-linked-files: ^2.1.33 => 2.3.14 gatsby-source-filesystem: ^2.3.27 => 2.3.28 gatsby-transformer-remark: ^2.8.32 => 2.8.33
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 21 (8 by maintainers)
Hi! It’s over two years now and I also still have this bug. Is there any way we can help?
pnpm install @gatsbyjs/reach-router
does workHello , I am facing same exact issue! any fix or help , please ?
I am getting this error now when running a normal
yarn dev
on my pc, not docker. Have spent a few hours trying anything to solve.Gatsby dependency bugs are hitting us VERY BAD. As normal with these bugs the issue is closed because of course there is no simple was to create a minimal reproduction.
I encountered this issue in the process of upgrading a project from Gatsby v2 to v4, and put a bunch of console logs in some places to try to understand what was happening. I was able to arrive at a solution that doesn’t require editing node_modules, and I hope it may shed some light on why this is happening in the first place.
TL;DR Cause — The
reach-router-add-basecontext-export-loader
is adding the needed export to@gatsby/reach-router/es/index.js
but not to@gatsby/reach-router/index.js
, and the latter is what Gatsby is importing (in my case, at least).TL;DR Fix/Workaround — Create your own basic loader to add the export, just like the Gatsby team did for the other file. Code below.
I wouldn’t call myself a Gatsby pro, so I don’t know why Gatsby in my project is using
/index.js
instead of/es/index.js
. I’m on Node 16 LTS, if that matters. The fact thatreach-router-add-basecontext-export-loader
addresses this problem, but only for/es/index.js
, suggests that either my Gatsby project is importing the router wrongly somehow (and perhaps some other people’s projects are not, hence difficulty reproducing), or that certain projects legitimately import the router this way, and not having a built-inBaseContext
patch for it like the other path does is simply an oversight.Hopefully this information can be useful in figuring out the underlying problem. But in the meantime, the issue can be worked around by doing the same thing the Gatsby team did: create a loader that adds the export at build time. In my case, I created my own
reach-router-add-basecontext-export-loader.js
at the root of my project with the appropriate export format (it’s different from what the ES version needs):And then added it to the Webpack config via
actions.setWebpackConfig
ingatsby-node.js
, making sure to test for thereach-router/index.js
rather thanreach-router/es/index.js
:This yields the same effect in the build as manually adding the export to the file in node_modules, but without the complications of patching node_modules. I hope it’s helpful for anyone else who runs into this until the bug is truly resolved.
@ascorbic, @vladar, and @pieh , I had the same issue and ran
gatsby build --no-uglify
. This was my warning:I went into @gatsby/reach-router/index.js and added
exports.BaseContext = BaseContext;
to the final line of the file and it seems to build well now. Not sure why this only is happening for @shrirambalaji and myself though. I also have some custom webpack configs