-
-
Notifications
You must be signed in to change notification settings - Fork 851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gatsby build error - window is not defined #657
Comments
Hi @davistran86 I had this error, I try using a state and useEffect
I also tray
But the error persists only on build for production in development I have no error. |
It happens also without Gatsby when running SSR on nodejs: (node:16012) UnhandledPromiseRejectionWarning: ReferenceError: window is not defined
at node_modules/pdfjs-dist/lib/web/ui_utils.js:564:3
at new Promise (<anonymous>)
at Object.<anonymous> (node_modules/pdfjs-dist/lib/web/ui_utils.js:563:26)
at Module._compile (internal/modules/cjs/loader.js:1118:30)
at Module._compile (node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1138:10)
at Object.newLoader [as .js] (node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:982:32)
at Function.Module._load (internal/modules/cjs/loader.js:875:14)
at Module.require (internal/modules/cjs/loader.js:1022:19) |
Go to function dispatchDOMEvent(eventName, args = null) {
const details = Object.create(null);
if (args && args.length > 0) {
const obj = args[0];
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
continue;
}
const value = obj[key];
if (key === 'source') {
if (value === window || value === document) {
return;
}
continue;
}
details[key] = value;
}
}
const event = document.createEvent('CustomEvent');
event.initCustomEvent(eventName, true, true, details);
document.dispatchEvent(event);
}
export default class EventBus {
constructor({
dispatchToDOM = false,
} = {}) {
this._listeners = Object.create(null);
this._dispatchToDOM = dispatchToDOM === true;
if (dispatchToDOM) {
console.error('The `eventBusDispatchToDOM` option/preference is deprecated, add event listeners to the EventBus instance rather than the DOM.');
}
}
on(eventName, listener) {
this._on(eventName, listener, {
external: true,
});
}
off(eventName, listener) {
this._off(eventName, listener);
}
dispatch(eventName, ...args) {
const eventListeners = this._listeners[eventName];
if (!eventListeners || eventListeners.length === 0) {
if (this._dispatchToDOM) {
dispatchDOMEvent(eventName, args);
}
return;
}
let externalListeners;
eventListeners.slice(0).forEach(({
listener,
external,
}) => {
if (external) {
if (!externalListeners) {
externalListeners = [];
}
externalListeners.push(listener);
return;
}
listener(...args);
});
if (externalListeners) {
externalListeners.forEach(listener => listener(...args));
externalListeners = null;
}
if (this._dispatchToDOM) {
dispatchDOMEvent(eventName, args);
}
}
_on(eventName, listener, options = {}) {
let eventListeners = this._listeners[eventName];
if (!eventListeners) {
eventListeners = [];
this._listeners[eventName] = eventListeners;
}
eventListeners.push({
listener,
external: options.external === true,
});
}
_off(eventName, listener) {
const eventListeners = this._listeners[eventName];
if (!eventListeners) {
return;
}
for (let i = 0, ii = eventListeners.length; i < ii; i += 1) {
if (eventListeners[i].listener === listener) {
eventListeners.splice(i, 1);
return;
}
}
}
} If that won't explode, we can consider replacing Mozilla's eventBus with this custom one, making us independent from ui_utils, which should fix the problem. |
I'm currently experiencing the same issue with v5. The error occurs at line 563 in const animationStarted = new Promise(function (resolve) {
window.requestAnimationFrame(resolve);
}); to: const animationStarted = new Promise(function (resolve) {
if (typeof window === 'undefined') {
return resolve();
}
window.requestAnimationFrame(resolve);
}); |
My suggestion would be to:
|
All, Thanks! |
Before you start - checklist
Description
I just updated react-pdf to version 5.0.0 and got error when using
gatsby build
to build my project. I have no error when using version 4.2.0Environment
Error Log
The text was updated successfully, but these errors were encountered: