使用 Typescript 和 Storybook 创建了 Next(版本:12.0.7
)。我可以构建
并启动
服务器,但是,一旦我发出请求并吐出以下消息,dev
就会失败:
浏览器:
Uncaught ReferenceError: __webpack_require__ is not defined
后端: 没有错误消息
我正在使用 Chakra UI、next-i18next 和 next-auth。
重现错误
- 使用 typescript 创建下一个应用程序
npx create-next-app@latest --ts
- 您现在将面临错误
exports is not Defined
。使用this hack您可以使用以下内容创建自定义_document.tsx
:
import Document, {
Html,
Head,
Main,
NextScript,
DocumentContext,
} from "next/document"
class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const initialProps = await Document.getInitialProps(ctx)
return {...initialProps}
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<script>var exports = {"{}"};</script>
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
- 运行
yarn run dev
- 访问http://localhost:3000/
- 查看控制台
最佳答案
找到了麻烦制造者!
当我在 Chrome 中打开该网站(我个人不使用 Chrome,因此没有安装任何扩展程序)时,该网站运行完美。经过一番挖掘,我发现我的反广告拦截器是麻烦制造者。禁用它终于解决了这个错误!
关于javascript - NextJS - `dev` 不工作,但 `build` && `start` 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70732066/