【问题标题】:NextJS - `dev` not working but `build` && `start` doNextJS - `dev` 不工作,但 `build` && `start` 可以
【发布时间】:2022-01-18 18:26:17
【问题描述】:

使用 Typescript 和 Storybook 创建了 Next(版本:12.0.7)。我可以buildstart 服务器,但是,一旦我发出请求并吐出此消息,dev 就会失败:

浏览器:

Uncaught ReferenceError: __webpack_require__ is not defined

后端: 没有错误信息

我正在使用 Chakra UI、next-i18next 和 next-auth。

重现错误

  1. 使用打字稿创建下一个应用程序 npx create-next-app@latest --ts
  2. 您现在将面临错误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
  1. 运行yarn run dev
  2. 访问http://localhost:3000/
  3. 查看控制台

【问题讨论】:

标签: javascript typescript next.js


【解决方案1】:

找到麻烦制造者!

当我在 Chrome 中打开该网站时(我个人不使用它,因此它没有安装任何扩展程序),该网站运行完美。经过一番挖掘,我发现我的反广告拦截器是麻烦制造者。禁用它终于解决了这个错误!

【讨论】:

    猜你喜欢
    • 2022-12-17
    • 2021-02-01
    • 2021-09-07
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    相关资源
    最近更新 更多