Next.js构建失败。窗口未定义

发布于 2025-02-13 07:49:32 字数 2154 浏览 2 评论 0原文

我正在构建一个next.js应用程序,并且在开发模式下它非常完美。部署到生产时,它失败了以下错误:

[    ] info  - Generating static pages (0/6)/home/ec2-user/groot-dashboard/node_modules/next-auth/react/index.js:220
            _ref5 = options !== null && options !== void 0 ? options : {}, _ref5$callbackUrl = _ref5.callbackUrl, callbackUrl = _ref5$callbackUrl === void 0 ? window.location.href : _ref5$callbackUrl, _ref5$redirect = _ref5.redirect, redirect = _ref5$redirect === void 0 ? true : _ref5$redirect;
                                                                                                                                                               ^

ReferenceError: window is not defined
    at _callee5$ (/home/ec2-user/groot-dashboard/node_modules/next-auth/react/index.js:220:160)
    at tryCatch (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:86:17)
    at Generator._invoke (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:66:24)
    at Generator.next (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:117:21)
    at asyncGeneratorStep (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at /home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
    at new Promise (<anonymous>)
    at /home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12
    at _signIn (/home/ec2-user/groot-dashboard/node_modules/next-auth/react/index.js:329:18)
[=   ] info  - Generating static pages (0/6)/home/ec2-user/groot-dashboard/node_modules/next-auth/react/index.js:220
            _ref5 = options !== null && options !== void 0 ? options : {}, _ref5$callbackUrl = _ref5.callbackUrl, callbackUrl = _ref5$callbackUrl === void 0 ? window.location.href : _ref5$callbackUrl, _ref5$redirect = _ref5.redirect, redirect = _ref5$redirect === void 0 ? true : _ref5$redirect;

我该怎么办?什么都不了解..

I'm building a next.js app and it works perfect in development mode. While deploying to production it fails with the following error:

[    ] info  - Generating static pages (0/6)/home/ec2-user/groot-dashboard/node_modules/next-auth/react/index.js:220
            _ref5 = options !== null && options !== void 0 ? options : {}, _ref5$callbackUrl = _ref5.callbackUrl, callbackUrl = _ref5$callbackUrl === void 0 ? window.location.href : _ref5$callbackUrl, _ref5$redirect = _ref5.redirect, redirect = _ref5$redirect === void 0 ? true : _ref5$redirect;
                                                                                                                                                               ^

ReferenceError: window is not defined
    at _callee5$ (/home/ec2-user/groot-dashboard/node_modules/next-auth/react/index.js:220:160)
    at tryCatch (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:86:17)
    at Generator._invoke (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:66:24)
    at Generator.next (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/regeneratorRuntime.js:117:21)
    at asyncGeneratorStep (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at /home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
    at new Promise (<anonymous>)
    at /home/ec2-user/groot-dashboard/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12
    at _signIn (/home/ec2-user/groot-dashboard/node_modules/next-auth/react/index.js:329:18)
[=   ] info  - Generating static pages (0/6)/home/ec2-user/groot-dashboard/node_modules/next-auth/react/index.js:220
            _ref5 = options !== null && options !== void 0 ? options : {}, _ref5$callbackUrl = _ref5.callbackUrl, callbackUrl = _ref5$callbackUrl === void 0 ? window.location.href : _ref5$callbackUrl, _ref5$redirect = _ref5.redirect, redirect = _ref5$redirect === void 0 ? true : _ref5$redirect;

What should i do? Can't understand anything..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请持续率性 2025-02-20 07:49:32

当您不显示导致错误的代码时,很难为您提供帮助...

当我想我认为您正在使用窗口时。 则可以使用

if (typeof window == 'undefined') {
     // Client-side-only code
}

或使用使用效果挂钩的更具体的帮助:)

useEffect(() => {
    // Client-side-only code
})

如果您提供代码的某些部分,

It is very hard to help you when you are not showing the code that causes the error ...

When I guess I think you are using window.* in your code and this is not available at build time so one way is to check if it is existing with

if (typeof window == 'undefined') {
     // Client-side-only code
}

or use the useEffect hook

useEffect(() => {
    // Client-side-only code
})

If you provide some parts of your code more specific help is possible :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文