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



我正在构建一个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;

我该怎么办?什么都听不懂

当您不显示导致错误的代码时,很难帮助您…

当我猜我认为你是使用窗口。*在你的代码中,这在构建时是不可用的,所以一种方法是检查它是否存在

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

或者使用useEffect钩子

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

如果你提供你的代码的某些部分,更具体的帮助是可能的:)

最新更新