[art]: ReferenceError: document is not defined
After upgrading to the latest version of RNW that includes the ART module, I started getting this error on application startup:
W20171220-14:45:17.615(-5)? (STDERR) ReferenceError: document is not defined
W20171220-14:45:17.615(-5)? (STDERR) at hasCanvas (/Users/joncursi/Sites/joncursi/redbird-web/node_modules/art/modes/fast-noSideEffects.js:3:16)
W20171220-14:45:17.615(-5)? (STDERR) at Object.<anonymous> (/Users/joncursi/Sites/joncursi/redbird-web/node_modules/art/modes/fast-noSideEffects.js:8:5)
W20171220-14:45:17.616(-5)? (STDERR) at Module._compile (module.js:635:30)
W20171220-14:45:17.616(-5)? (STDERR) at Object.Module._extensions..js (module.js:646:10)
W20171220-14:45:17.616(-5)? (STDERR) at Module.load (module.js:554:32)
W20171220-14:45:17.616(-5)? (STDERR) at tryModuleLoad (module.js:497:12)
W20171220-14:45:17.616(-5)? (STDERR) at Function.Module._load (module.js:489:3)
W20171220-14:45:17.616(-5)? (STDERR) at Module.require (module.js:579:17)
W20171220-14:45:17.616(-5)? (STDERR) at require (internal/module.js:11:18)
W20171220-14:45:17.617(-5)? (STDERR) at /Users/joncursi/Sites/joncursi/redbird-web/node_modules/react-art/cjs/react-art.development.js:27:25
I followed the chain and it seems that ART -> react-art -> art module, which causes the break. Any suggestions on how to resolve this? I am using SSR in my app.
If it helps explain the context, I'm using Meteor which eagerly loads all of this module's code. Meteor doesn't support code splitting / tree shaking, so I can't tell it not to. Is it possible to make a code change in this repo so that the ART module will not crash the server?
This is an issue with ART not working properly for server-side-rendering. You should ask them about what needs to be done.
relevant PR on art https://github.com/sebmarkbage/art/pull/16
bad news is the repo seems inactive.
We could also raise this issue with the React team, as they maintain react-art in the new react monorepo. In the meantime, using the RNW babel plugin (is this possible with Meteor?) should avoid ART being bundled if you're not using it.
how to resolve it
Opened an issue against the react monorepo where react-art is published from:
https://github.com/facebook/react/issues/11916
I ran into this when I tried to use the CheckBox component today. A quick fix that is working for me so far is to add this ugly hack to the beginning of my server-side code:
if (typeof document === 'undefined') {
global.document = {
createElement: () => null,
};
}
Ran into what? A reference error originating from art / react-art?
document is not defined with ssr when I used a CheckBox. react-art was in the stack trace. Apparently the CheckBox depends on it as that is the only time I've seen it.
The checkbox doesn't depend on ART. Can you share the stack trace and what tree is being rendered?
My code is first referenced in this line. It uses CheckBox. If I don't import CheckBox and instead just use a View in this file, the problem goes away, so thats why I suspect something with CheckBox. But, my app is also bundled with next, so its possible there is more to it:
at Object.<anonymous> (/Users/myuser/apps/myapp/.next/dist/src/client/components/UI/Forms/CheckInput.js:5:150)
Here is the full stack trace:
ReferenceError: document is not defined
at hasCanvas (/Users/myuser/apps/myapp/node_modules/art/modes/fast-noSideEffects.js:3:25)
at Object.<anonymous> (/Users/myuser/apps/myapp/node_modules/art/modes/fast-noSideEffects.js:8:5)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at /Users/myuser/apps/myapp/node_modules/react-art/cjs/react-art.development.js:27:25
at Object.<anonymous> (/Users/myuser/apps/myapp/node_modules/react-art/cjs/react-art.development.js:7797:5)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/node_modules/react-art/index.js:6:20)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/node_modules/react-native-web/dist/components/ART/index.js:5:17)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/node_modules/react-native-web/dist/index.js:110:12)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at Module._extensions..js (module.js:579:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/.next/dist/src/client/components/UI/Forms/CheckInput.js:5:150)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at loader (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/src/client/components/UI/Forms/index.js:5:1)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at loader (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/src/client/components/Login.js:14:1)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at loader (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/src/client/components/ModalsNav.js:5:1)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at loader (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/src/client/components/AppLayout.js:15:1)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at loader (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/src/client/components/App.js:4:1)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at loader (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/myuser/apps/myapp/pages/app.js:2:1)
at Module._compile (module.js:570:32)
at Module._compile (/Users/myuser/apps/myapp/node_modules/source-map-support/source-map-support.js:492:25)
at loader (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/myuser/apps/myapp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at _callee$ (/Users/myuser/apps/myapp/node_modules/next/dist/server/require.js:33:46)
at tryCatch (/Users/myuser/apps/myapp/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Users/myuser/apps/myapp/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Users/myuser/apps/myapp/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:114:21)
at step (/Users/myuser/apps/myapp/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
at /Users/myuser/apps/myapp/node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13
You can see from the stack trace that the error lies in art. The reason this only happens with CheckBox is because of a bug in 0.2.2 (that component is missing from the Babel plugin's list). But this will be fixed in 0.3.0 which is due to be published very soon.
I can confirm, I had this issue a few days ago -and with 0.3.0 it has been fixed. Thanks!
I am also getting this error with react-native(android). My version is 0.3.0 but still geting error Can't find variable: document when importing react-native-web
@umairfarooq44 you should share a stack trace in order to track the root of the issue
This is the stack trace :

Why are you running web code on Android though?
I'm getting a similar error when trying to great a simple stub project served with webpack-dev-server.
NB: Yes, I am injecting react native/web components into my custom Component as props.
index.js which is the entry point for webpack:
import React from 'react'
import { AppRegistry, Text, View, StyleSheet } from 'react-native'
import HelloWorld from '../common/HelloWorld'
const WebApp = () => {
const props = { Text, View, StyleSheet }
return <HelloWorld { ...props } />
}
AppRegistry.registerComponent('HelloWorld', () => HelloWorld);
AppRegistry.runApplication('HelloWorld', { rootTag: document.getElementById('app') });
When attempting to run the dev server, Stack trace is:
webpack-dev-server --config ./src/web/webpack.config.babel.js --open
/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agnostic-boilerplate/node_modules/babel-core/lib/transformation/file/o
ptions/option-manager.js:328
throw e;
^
ReferenceError: document is not defined (While processing preset: "/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agn
ostic-boilerplate/node_modules/react-native-web/dist/index.js")
at hasCanvas (/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agnostic-boilerplate/node_modules/art/modes/fast-noS
ideEffects.js:3:16)
at Object.<anonymous> (/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agnostic-boilerplate/node_modules/art/modes
/fast-noSideEffects.js:8:5)
at Module._compile (module.js:635:30)
at Module._extensions..js (module.js:646:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agnost
ic-boilerplate/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
When building locally, the stack trace is:
webpack --config ./src/web/webpack.config.babel.js
/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agnostic-boilerplate/node_modules/babel-core/lib/transformation/file/o
ptions/option-manager.js:328
throw e;
^
ReferenceError: document is not defined (While processing preset: "/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agn
ostic-boilerplate/node_modules/react-native-web/dist/index.js")
at hasCanvas (/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agnostic-boilerplate/node_modules/art/modes/fast-noS
ideEffects.js:3:16)
at Object.<anonymous> (/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agnostic-boilerplate/node_modules/art/modes
/fast-noSideEffects.js:8:5)
at Module._compile (module.js:635:30)
at Module._extensions..js (module.js:646:10)
at Object.require.extensions.(anonymous function) [as .js] (/Users/dmeehan/Documents/Dropbox/Projects/node-tutorials/react-agnost
ic-boilerplate/node_modules/babel-register/lib/node.js:152:7)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
Use the Babel plugin
@necolas Please can you elaborate. Babel plugin to what?
webpack.config.babel.js:
export default {
entry: path.resolve(__dirname, 'index.js'),
output: {
filename: 'build.js',
path: path.resolve(__dirname, '../../dist')
},
devServer: {
contentBase: path.resolve(__dirname, '../../dist')
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, 'index.html'),
filename: 'index.html'
})
],
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['env', 'react', 'react-native-web']
}
}
}
]
}
}
.babelrc:
{
"presets": [ "env" ]
}