Next.js Discord

Discord Forum

Help Vercel Deploy Issue

Answered
Rufous-backed Robin posted this in #help-forum
Open in Discord
Rufous-backed RobinOP
it works in my pc but when deploy to the vercel, occurs error.
(node:299) [DEP0040] DeprecationWarning: The punycode module is deprecated. Please use a userland alternative instead.
(Use node --trace-deprecation ... to show where the warning was created)
✓ Compiled successfully
Linting and checking validity of types ...
Collecting page data ...
Generating static pages (0/9) ...
Generating static pages (2/9)
Generating static pages (4/9)
Generating static pages (6/9)
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
ReferenceError: document is not defined
at createTag (/vercel/path0/.next/server/app/page.js:1:577350)
at /vercel/path0/.next/server/app/page.js:1:590238
at /vercel/path0/.next/server/app/page.js:1:590361
at /vercel/path0/.next/server/app/page.js:1:593186
at /vercel/path0/.next/server/app/page.js:1:577015
at 78187 (/vercel/path0/.next/server/app/page.js:1:577019)
at t (/vercel/path0/.next/server/webpack-runtime.js:1:128)
at 14426 (/vercel/path0/.next/server/app/page.js:1:11178)
at Object.t [as require] (/vercel/path0/.next/server/webpack-runtime.js:1:128)
at require (/vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:131:7946)
Export encountered an error on /page: /, exiting the build.
⨯ Static worker exited with code: 1 and signal: null
Error: Command "npm run build" exited with 1
Answered by Rufous-backed Robin
View full answer

46 Replies

@Rufous-backed Robin it works in my pc but when deploy to the vercel, occurs error. (node:299) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) ✓ Compiled successfully Linting and checking validity of types ... Collecting page data ... Generating static pages (0/9) ... Generating static pages (2/9) Generating static pages (4/9) Generating static pages (6/9) Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error ReferenceError: document is not defined at createTag (/vercel/path0/.next/server/app/page.js:1:577350) at /vercel/path0/.next/server/app/page.js:1:590238 at /vercel/path0/.next/server/app/page.js:1:590361 at /vercel/path0/.next/server/app/page.js:1:593186 at /vercel/path0/.next/server/app/page.js:1:577015 at 78187 (/vercel/path0/.next/server/app/page.js:1:577019) at t (/vercel/path0/.next/server/webpack-runtime.js:1:128) at 14426 (/vercel/path0/.next/server/app/page.js:1:11178) at Object.t [as require] (/vercel/path0/.next/server/webpack-runtime.js:1:128) at require (/vercel/path0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:131:7946) Export encountered an error on /page: /, exiting the build. ⨯ Static worker exited with code: 1 and signal: null Error: Command "npm run build" exited with 1
it looks like that you access in your layout or your page.tsx of / the document. Make sure to access it only when it's defined. You can ensure this, but using it only in useEffect or wait until the component is mounted or by checking if it's not undefined typeof document !== undefined. After that, the error should be gone
Rufous-backed RobinOP
sorry but i'm not sure what you mean. please explain more detail. My project used jsx.
@Rufous-backed Robin sorry but i'm not sure what you mean. please explain more detail. My project used jsx.
When your Next.js application runs in the browser (client-side), the document object is available. However, during the build process on Vercel, Next.js pre-renders your pages on a server. The server environment doesn't have a browser or a document object. Your code is trying to use document before the page gets to the browser, leading to the error.

So fix it by using one of these methods:
1. using it only in useEffect:
import { useEffect } from 'react';

function MyComponent() {
  useEffect(() => {
      const element = document.getElementById('myElement');
      if (element) {
          element.textContent = 'Hello from the browser!';
      }

  }, []);

  return (
    <div id="myElement">Initial Text</div>
  );
}

export default MyComponent;

2. wait until the component is mounted
import { useEffect } from 'react';

function MyComponent() {
  const [isMounted, setIsMounted] = useState(false);

  useEffect(() => {
      setIsMounted(true);
  }, []);

  if(!isMounted)
    return null;

  const elmt = document.getElementById("someid"); // should work

  return (
    <div id="myElement">Initial Text</div>
  );
}

export default MyComponent;

3. by checking if it's not undefined
import { useEffect } from 'react';

function MyComponent() {
  if(typeof document !== undefined) {
    const elmt = document.getElementById("someid"); // should work
  }

  return (
    <div id="myElement">Initial Text</div>
  );
}

export default MyComponent;

4. By importing your component without SSR
const MyComponent = dynamic(() => import('./some/Component'), {
  ssr: false // This line is crucial
});
Rufous-backed RobinOP
Thanks for your support. So what exactly can I should fix? which part? all components?
@Rufous-backed Robin Thanks for your support. So what exactly can I should fix? which part? all components?
only the part on your page / where you using document. All 4 solutions are possible ways to fix it
Rufous-backed RobinOP
you mean, in terms of document, like as " document.body.addEventListener('pointermove', UPDATE);"
and then what is example of /?
@Rufous-backed Robin you mean, in terms of document, like as " document.body.addEventListener('pointermove', UPDATE);" and then what is example of /?
yes. With / I mean your page at https://example.com / <--- here at the end the page. And then the code inside your page.jsx or layout.jsx
Rufous-backed RobinOP
my project is not tsx, it is jsx
@Rufous-backed Robin my project is not tsx, it is jsx
it's the same for both 🙂
I changed it to .jsx
Rufous-backed RobinOP
so you mean, all urls in code like as https://example.com have / of their end, right?
@Rufous-backed Robin so you mean, all urls in code like as https://example.com have / of their end, right?
I mean just your start page. When you visit localhost:3000/ while your dev server is active. That page. I mean that page
Rufous-backed RobinOP
I'm not sure what is start page.
page.js?
@Rufous-backed Robin page.js?
yes! Normally your page is called page.jsx. So you might want to change that. Same applies to your layout.
Rufous-backed RobinOP
so I should change page.js and layout.js to page.jsx and layout.jsx? it is all?
there is no urls in page and layout.
@Rufous-backed Robin so I should change page.js and layout.js to page.jsx and layout.jsx? it is all? there is no urls in page and layout.
so I should change page.js and layout.js to page.jsx and layout.jsx?
thats my advice in general, not specific for this issue

You need to find document inside your layout or page
Rufous-backed RobinOP
there is no document
oops
@Rufous-backed Robin there is no document
there must be a document somewhere, else the error wouldn't exists
Rufous-backed RobinOP
In one component, there is document. But it used useeffect
Rufous-backed RobinOP
"use client"

import { useEffect } from 'react';

const GlowCard = ({ children , identifier}) => {
useEffect(() => {
const CONTAINER = document.querySelector(.glow-container-${identifier});
const CARDS = document.querySelectorAll(.glow-card-${identifier});

const CONFIG = {
proximity: 40,
spread: 80,
blur: 12,
gap: 32,
vertical: false,
opacity: 0,
};
const UPDATE = (event) => {
for (const CARD of CARDS) {
const CARD_BOUNDS = CARD.getBoundingClientRect();

if (
event?.x > CARD_BOUNDS.left - CONFIG.proximity &&
event?.x < CARD_BOUNDS.left + CARD_BOUNDS.width + CONFIG.proximity &&
event?.y > CARD_BOUNDS.top - CONFIG.proximity &&
event?.y < CARD_BOUNDS.top + CARD_BOUNDS.height + CONFIG.proximity
) {
CARD.style.setProperty('--active', 1);
} else {
CARD.style.setProperty('--active', CONFIG.opacity);
}

const CARD_CENTER = [
CARD_BOUNDS.left + CARD_BOUNDS.width * 0.5,
CARD_BOUNDS.top + CARD_BOUNDS.height * 0.5,
];
let ANGLE =
(Math.atan2(event?.y - CARD_CENTER[1], event?.x - CARD_CENTER[0]) *
180) /
Math.PI;

ANGLE = ANGLE < 0 ? ANGLE + 360 : ANGLE;

CARD.style.setProperty('--start', ANGLE + 90);
}
};

document.body.addEventListener('pointermove', UPDATE);

const RESTYLE = () => {
CONTAINER.style.setProperty('--gap', CONFIG.gap);
CONTAINER.style.setProperty('--blur', CONFIG.blur);
CONTAINER.style.setProperty('--spread', CONFIG.spread);
CONTAINER.style.setProperty(
'--direction',
CONFIG.vertical ? 'column' : 'row'
);
};

RESTYLE();
UPDATE();

// Cleanup event listener
return () => {
document.body.removeEventListener('pointermove', UPDATE);
};
}, [identifier]);

return (
<div className={glow-container-${identifier} glow-container}>
<article className={glow-card glow-card-${identifier} h-fit cursor-pointer border border-[#2a2e5a] transition-all duration-300 relative bg-[#101123] text-gray-200 rounded-xl hover:border-transparent w-full}>
<div className="glows"></div>
{children}
</article>
</div>
);
};

export default GlowCard;
Rufous-backed RobinOP
Thanks
Let me try
 useEffect(() => {
    if(typeof document === undefined)
      return;

    const CONTAINER = document.querySelector(.glow-container-${identifier});
    const CARDS = document.querySelectorAll(.glow-card-${identifier});
    
    // ... more code
Rufous-backed RobinOP
oops
the same error face
it really stress
@Rufous-backed Robin the same error face
then you must use the document in some other place as well
Rufous-backed RobinOP
anyway, thanks so much for your effort
👍
Rufous-backed RobinOP
not yet
@Rufous-backed Robin not yet
did you check the other calls of document?
@Rufous-backed Robin?
Rufous-backed RobinOP
well
I found solution
@Rufous-backed Robin I found solution
please share your solution, so others can solve the same error too
Rufous-backed RobinOP
yeah of course
I added the this code to package.json.
Rufous-backed RobinOP
Answer
Rufous-backed RobinOP
and have set the config the node version in the vercel general setting manually.
that's it