在开发NextJS 13应用程序时,我遇到了这个错误:ReferenceError: global is not defined
我搜索了错误并找到了一些解决方案,但它们不起作用。我试图更改配置文件,并试图跟踪错误,但这些都对我无效。
这是我的NextJS配置文件
const nextConfig = {
reactStrictMode: true,
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'mdx'],
experimental: {
appDir: true,
mdxRs: true,
},
sassOptions: {
prependData: `
@import "styles/theme.scss";
@import "styles/mixins.scss";
`,
},
webpack: (config, {isServer}, options) => {
config.module.rules.push(
{
test: /\.mdx?$/,
use: [
{
loader: '@mdx-js/loader',
/** @type {import('@mdx-js/loader').Options} */
options: {...options},
},
],
},
{
test: /\.ya?ml$/,
oneOf: [
{
resourceQuery: /stream/,
options: {asStream: true},
loader: 'yaml-loader',
},
{loader: 'yaml-loader'},
],
}
);
if (!isServer) {
config.resolve.fallback = {
fs: false,
};
}
config.infrastructureLogging = {
level: 'error',
};
return config;
},
async redirects() {
return [
{
source: '/',
destination: '/home',
permanent: true,
},
];
},
};
字符串
错误代码:
编辑
我发现这个错误是由于从utils
目录中导入函数而引起的,我把我的工具函数放在了utils
目录中,并在客户端组件中使用它们。现在的问题是,我不能把'use client'
放在我的工具函数文件中,这也会导致服务器端组件中的错误。我试图为工具函数创建两个文件,一个用于客户端,另一个用于服务器端,但这也不起作用。
对于这个问题,有没有更好、更直观的解决方案?
1条答案
按热度按时间vd2z7a6w1#
我猜你也使用turborepo。Prisma有这个指南,使用
globalThis
。基本上这个想法类似于使用global
,但是因为global
只在node
环境中可用,我们必须使用globalThis
代替。字符串
来源:https://www.prisma.io/docs/guides/other/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices