Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

ReferenceError: document is not defined when running a production server. #55

Closed
ShuoShen opened this issue Mar 17, 2015 · 4 comments
Closed

Comments

@ShuoShen
Copy link

This happens when I'm using the domquery module. The issue can be reproduced in the following four steps:
#1. install the components

$ npm install domquery; npm install matches-selector; npm install closest; npm install event;
#2. change the following file:

--- a/app/Home/index.jsx
+++ b/app/Home/index.jsx
@@ -1,5 +1,6 @@
var React = require("react");
var Link = require("react-router").Link;
+var dom = require('domquery')
#3. run build

$ npm run build;
#4. run start, which gives the error:

$ npm run start

error message below

react-starter@0.0.0 start /Users/shen/Documents/workspace/react-starter
node lib/server-production

/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:5860
var doc = document
^
ReferenceError: document is not defined
at /Users/shen/Documents/workspace/react-starter/build/prerender/main.js:5860:14
at doc (/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:5856:73)
at Object. (/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:5859:4)
at webpack_require (/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:21:30)
at Object.module.exports.module.exports (/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:5029:14)
at webpack_require (/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:21:30)
at Object. (/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:4760:17)
at webpack_require (/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:21:30)
at Object. (/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:4987:15)
at webpack_require (/Users/shen/Documents/workspace/react-starter/build/prerender/main.js:21:30)

npm ERR! react-starter@0.0.0 start: node lib/server-production
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the react-starter@0.0.0 start script.
npm ERR! This is most likely a problem with the react-starter package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node lib/server-production
npm ERR! You can get their info via:
npm ERR! npm owner ls react-starter
npm ERR! There is likely additional logging output above.
npm ERR! System Darwin 13.4.0
npm ERR! command "/usr/local/Cellar/node/0.10.24/bin/node" "/usr/local/bin/npm" "run" "start"
npm ERR! cwd /Users/shen/Documents/workspace/react-starter
npm ERR! node -v v0.10.24
npm ERR! npm -v 1.3.21
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/shen/Documents/workspace/react-starter/npm-debug.log
npm ERR! not ok code 0

What is the cause of this error? Please help! Thanks.

@sokra
Copy link
Member

sokra commented Mar 19, 2015

There is no DOM while prerendering.
Write your components without using the DOM, or
require DOM specific stuff in componentDidMount.

@sokra
Copy link
Member

sokra commented Mar 19, 2015

if(typeof document !== "undefined")

@ShuoShen
Copy link
Author

Great! That works. Thanks for your help.

@smashraid
Copy link

How can i debug with vscode

image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants