sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

TypeError: dispatcher.getOwner is not a function on RR7+React19

Open AbhiPrasad opened this issue 5 months ago • 9 comments

https://discord.com/channels/621778831602221064/1400566232754688161/1400566232754688161

using sentry/react-router 10.0.0, react router 7.5.3 and react 19.1.0 i get this error in the prod build. any ideas what could be going wrong? some sort of react version mismatch?

Works after you set NODE_ENV=production

Related issue: https://github.com/remix-run/react-router/issues/12138

AbhiPrasad avatar Jul 31 '25 20:07 AbhiPrasad

Hi, I am also affected by this issue and would like to bump it. Maybe adding the corresponding labels would help to easier find this issue.

The fix did in fact solve it, but I feel like this shouldn't be a permanent solution. I am currently using @sentry/react-router 10.22.0.

DarkIntaqt avatar Oct 25 '25 12:10 DarkIntaqt

Hey @DarkIntaqt could you share more details about your app? What version of react router and react are you using? Are you also seeing this for prod builds? Are you able to provide a reproduction?

AbhiPrasad avatar Oct 27 '25 14:10 AbhiPrasad

Hi, thank you for your quick response.

My app was a blank one as I was going to start a new project, Sentry was the first thing that I installed.

I used the following versions which are all most likely all latest.

...
  "dependencies": {
    "@react-router/node": "^7.9.2",
    "@react-router/serve": "^7.9.2",
    "@sentry/react-router": "^10.22.0",
    "isbot": "^5.1.31",
    "react": "^19.1.1",
    "react-dom": "^19.1.1",
    "react-router": "^7.9.2"
  },
...

The error only occurs in production builds and deployments, not during development.

Here is a blank react-router repository with @sentry/react-router, in which I followed the exact setup steps as in https://docs.sentry.io/platforms/javascript/guides/react-router/ (except that I used "Do you need to customize your handleRequest function?" instead of copying the entire entry.client.tsx).
The issue occurs upon running npm run build && npm run start and accessing the site. I also added the script npm run build-fix && npm run start-fix, which resolves the issue:
https://github.com/DarkIntaqt/rr7-sentry (I hope a git repo is okay, otherwise I could also share a stackblitz or smth like that)

This is the entire error message:

TypeError: dispatcher.getOwner is not a function
    at getOwner (/workspaces/codespaces-blank/node_modules/react/cjs/react.development.js:163:54)
    at Module.process.env.NODE_ENV.exports.createElement (/workspaces/codespaces-blank/node_modules/react/cjs/react.development.js:1052:9)
    at getRouteComponents (file:///workspaces/codespaces-blank/node_modules/react-router/dist/development/chunk-OIYGIGL5.mjs:7919:104)
    at file:///workspaces/codespaces-blank/node_modules/react-router/dist/development/chunk-OIYGIGL5.mjs:7939:10
    at Array.map (<anonymous>)
    at createServerRoutes (file:///workspaces/codespaces-blank/node_modules/react-router/dist/development/chunk-OIYGIGL5.mjs:7932:45)
    at ServerRouter (file:///workspaces/codespaces-blank/node_modules/react-router/dist/development/chunk-WY5IRSCW.mjs:76:16)
    at renderWithHooks (/workspaces/codespaces-blank/node_modules/react-dom/cjs/react-dom-server.node.production.js:4647:18)
    at renderElement (/workspaces/codespaces-blank/node_modules/react-dom/cjs/react-dom-server.node.production.js:4785:14)
    at retryNode (/workspaces/codespaces-blank/node_modules/react-dom/cjs/react-dom-server.node.production.js:5503:16)
TypeError: dispatcher.getOwner is not a function
    at getOwner (/workspaces/codespaces-blank/node_modules/react/cjs/react.development.js:163:54)
    at Module.process.env.NODE_ENV.exports.createElement (/workspaces/codespaces-blank/node_modules/react/cjs/react.development.js:1052:9)
    at getRouteComponents (file:///workspaces/codespaces-blank/node_modules/react-router/dist/development/chunk-OIYGIGL5.mjs:7919:104)
    at file:///workspaces/codespaces-blank/node_modules/react-router/dist/development/chunk-OIYGIGL5.mjs:7939:10
    at Array.map (<anonymous>)
    at createServerRoutes (file:///workspaces/codespaces-blank/node_modules/react-router/dist/development/chunk-OIYGIGL5.mjs:7932:45)
    at ServerRouter (file:///workspaces/codespaces-blank/node_modules/react-router/dist/development/chunk-WY5IRSCW.mjs:76:16)
    at renderWithHooks (/workspaces/codespaces-blank/node_modules/react-dom/cjs/react-dom-server.node.production.js:4647:18)
    at renderElement (/workspaces/codespaces-blank/node_modules/react-dom/cjs/react-dom-server.node.production.js:4785:14)
    at retryNode (/workspaces/codespaces-blank/node_modules/react-dom/cjs/react-dom-server.node.production.js:5503:16)
GET / 500 - - 25.575 ms

DarkIntaqt avatar Oct 27 '25 14:10 DarkIntaqt

This still looks like an upstream issue where RR is not setting the NODE_ENV correctly for some reason.

chargome avatar Oct 28 '25 17:10 chargome

also running into this issue when running healthchecks on fly.io. I followed the guide on Sentry: https://docs.sentry.io/platforms/javascript/guides/react-router/

With: Do you need to customize your handleRequest function?

"@sentry/react-router": "^10.28.0"
"@react-router/node": "^7.10.0"
"@react-router/serve": "^7.10.0"

mnlfischer avatar Dec 05 '25 08:12 mnlfischer

I took a look and noticed that RR does set the env but only when the react-router-serve command runs, which is after importing instrument.server.mjs which already imports some stuff from RR, so the env isn't set by the time Sentry imports stuff from RR, and is set after that.

Current workaround is to manually set NODE_ENV to production when running the start command.

NODE_ENV=production NODE_OPTIONS='--import ./instrument.server.mjs' react-router-serve ./build/server/index.js

This is tricky to fix, so I will try to play around with some ideas and report back.

logaretm avatar Dec 08 '25 09:12 logaretm

@logaretm I set it to my dockerfile and it works now. Thanks for pointing me in the right direction.

mnlfischer avatar Dec 08 '25 15:12 mnlfischer

@chargome @AbhiPrasad While this would be a general problem with anything that runs before the react router, do you think we can do something like introducing an async setup wrapper? Mainly it would read what's the running command (e.g: dev vs start) and assign the env variable to process.env if it wasn't set already.

await setupInstrumentation().then((Sentry) => {
  Sentry.init({
    // ...
  });
});

I tried it locally and it works but I don't like it from a DX perspective, and I think it may negatively impact tree-shaking. I think this is something that needs to be done upstream, or just use the current workaround.

I will document the workaround in the meanwhile.

logaretm avatar Dec 09 '25 09:12 logaretm