Next.js: ReferenceError: document is not defined

Created on 11 Jul 2017  路  2Comments  路  Source: vercel/next.js

I encountered this error today,

document is not defined
ReferenceError: document is not defined
    at Function._callee$ (/data/frank/image-website/.next/dist/pages/entity.js:95:72)
    at tryCatch (/data/frank/image-website/node_modules/regenerator-runtime/runtime.js:65:40)
    at Generator.invoke [as _invoke] (/data/frank/image-website/node_modules/regenerator-runtime/runtime.js:303:22)
    at Generator.prototype.(anonymous function) [as next] (/data/frank/image-website/node_modules/regenerator-runtime/runtime.js:117:21)
    at step (/data/frank/image-website/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at /data/frank/image-website/node_modules/babel-runtime/helpers/asyncToGenerator.js:35:14
    at F (/data/frank/image-website/node_modules/core-js/library/modules/_export.js:35:28)
    at Function.<anonymous> (/data/frank/image-website/node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12)
    at Function.getInitialProps (/data/frank/image-website/.next/dist/pages/entity.js:118:22)
    at _callee$ (/data/frank/image-website/node_modules/next/dist/lib/utils.js:36:30)

No clue at all, any ideas? Thanks!

My Environment

| Tech | Version |
|---------|---------|
| next | 3.0.1-beta.9 |
| node | v7.10.0 |
| OS | centos-release-7-3.1611.el7.centos.x86_64 |
| browser | Version 59.0.3071.115 (Official Build) (64-bit) |

Most helpful comment

Hi, you're using/writing some library the reply on DOM/ browser API. Since SSR is executed in node environment, the script won't have access to those API.
Consider wrapping your code in if (process.browser){} or put it in componentDidMount hook

All 2 comments

Hi, you're using/writing some library the reply on DOM/ browser API. Since SSR is executed in node environment, the script won't have access to those API.
Consider wrapping your code in if (process.browser){} or put it in componentDidMount hook

It turns out I used document.cookie in getInitialProps(), after removing it everything is fine. Thanks!

Was this page helpful?
0 / 5 - 0 ratings