0

I'm trying to use a web API in my Preact app and can't seem to find a workaround for this error

Build  [=================== ] 93% (10.2s) after chunk asset optimization
ReferenceError: window is not defined
method: UqHU

Stack:

{
    "fileName": "D:\\preact-app\\build\\ssr-build\\ssr-bundle.js",
    "lineNumber": 1,
    "functionName": "Object.UqHU",
    "methodName": "UqHU",
    "columnNumber": 93549,
    "native": false
}
This is most likely caused by using DOM or Web APIs.
Pre-render runs in node and has no access to globals available in browsers.

Consider wrapping code producing error in: "if (typeof window !== "undefined") { ... }"

Alternatively use "preact build --no-prerender" to disable prerendering.

See https://github.com/preactjs/preact-cli#pre-rendering for further information.

I have tried "if (typeof window !== "undefined")" but still getting the same error. Commenting out the code or using --no-prerender solves it

2
  • If --no-prerender solves your issue, what exactly is the question? Commented Feb 5, 2023 at 15:48
  • @SzigetiPéter They want to avoid the need to disable prerendering, clearly. Commented Feb 5, 2023 at 23:37

2 Answers 2

1

The problem came from the import of axios, which depends on the "form-data" library and is not compatible with Preact prerendering as far as I understand. I chose to use fetch instead of axios to fix this but rschristian gives more solutions in this post: https://github.com/preactjs/preact-cli/discussions/1784

Sign up to request clarification or add additional context in comments.

Comments

0

preact-cli prerenders your app in Node, where browser globals (such as window) do not exist.

If you've added a window check but are still getting the same error, that means you have other code also trying to access window. Each offending use will need to be wrapped in a window check if you want to prerender your app.

If you want to make a discussion on the preact-cli repo and provide your app, I can try to take a look.


Edit: The issue is in an upstream dependency of axios, form-data. This has an unsound browser vs Node check, meaning any use of axios at all will throw an error. The simplest solution would be to patch form-data/lib/browser.js with the following snippet:

module.exports = typeof self == 'object' ? self.FormData : typeof window !='undefined' ? window.FormData : undefined;

https://github.com/preactjs/preact-cli/discussions/1784

2 Comments

the window check doesn't work at all, commenting out the code or writing something like if(1+1 == 3) instead of if(typeof window !== 'undefined') works. Will make a discussion on the preact-cli repo in a bit, thanks!
That would only happen if something's defining window, which is rather horrifying and very bad for any build tool, not just preact-cli. Basically you're just looking for a condition that's only true in a browser, so Node skips it. window is the standard, but there are other things you could test against too to varying degrees of success (some of those conditions are commonly polyfilled, so effectiveness depends a bit on what you're using).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.