CRA SSR : ReferenceError: window is not defined
See original GitHub issueRunning in to this issue on SSR CRA and have not manage to get around it.
$ cross-env NODE_ENV=production node ./server/index.js
/Users/mathias/Dropbox/Projects/slight/rettfinans/node_modules/reactn/index.js:1
(function (exports, require, module, __filename, __dirname) { !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define("reactn",["react"],e):"object"==typeof exports?exports.reactn=e(require("react")):t.reactn=e(t.React)}(window,function(t){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enum
ReferenceError: window is not defined
at Object.<anonymous> (/Users/mathias/Dropbox/Projects/slight/rettfinans/node_modules/reactn/index.js:1:249)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Module._compile (/Users/mathias/Dropbox/Projects/slight/rettfinans/node_modules/pirates/lib/index.js:83:24)
at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Object.newLoader [as .js] (/Users/mathias/Dropbox/Projects/slight/rettfinans/node_modules/pirates/lib/index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
at require (internal/modules/cjs/helpers.js:20:18)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
React JS Server side issue - window not found - Stack Overflow
BROWSER to my advantage because it will be defined as undefined if it is server side, and it will be true if the...
Read more >How to solve "window is not defined" errors in React and Next.js
First solution: typeof Because typeof won't try to evaluate "window", it will only try to get its type, in our case in Node....
Read more >referenceerror: window is not defined, how to solve
Here's how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >Using window in React SSR: The Complete Guide
window is not defined on the server, so you can't use it during the render of a component being SSR'd. ... During a...
Read more >How to migrate from create-react-app to Next.js
The reason this error pops up is this: when html is rendered on the server (remember, SSR stands for server-side rendering), there's no...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you. I will have to address this Sunday or Monday. I think it is the Webpack configuration targeting browsers specifically, as window shouldn’t be used in ReactN itself. But I’ll look into it to be sure and correct it accordingly.
ReactN does not have any dependencies on the window object. Webpack is building the package for browsers instead of with NodeJS support; the Webpack build process is what is injecting
window
into the code, which otherwise has no reliance on thewindow
object. It seems to be a bug when usingumd
as the library target.I’m testing this fix, which seems promising so far. I’ve tried it with global browser window scope and also the global Node scope.
I’ll push
0.1.7
right now to address this.I will mention that
reactn
does not officially support SSR yet, as there are known bugs and limitations. Primarily, neither re-hydration on the client #9 nor concurrent rendering #17 are supported. As long as you are setting state in the client and usingresetGlobal
between renders, it should be fine.Full server-side rendering supported is planned. I just don’t have enough experience with SSR yet to implement it thoroughly.