Nextjs - 在 pages/_document 错误之外导入 next/document

Nextjs - importing next/document outside of pages/_document error

由于 Next.js 自定义文档,我在构建期间遇到此错误。

所以,在构建期间,我收到一条错误消息,我在 pages/_document.js 中也有 _document.js

next/document should not be imported outside of pages/_document.js. See https://nextjs.org/docs/messages/no-document-import-in-page.

请帮帮我!

编辑

接下来更新eslint-config-next到版本11.1.3-canary.7

这是 known issue 来自最新的 Nextjs 更新

引自 Nextjs 团队:

This is fixed in #28745 and works in 11.1.3-canary.7. Please upgrade to that version if you're experiencing this issue and if you're still seeing the same error, make sure to clear your cache (next lint --no-cache).


总结:

您需要将 Nextjseslint-config-next 更新为 11.1.3-canary.7 并且不要忘记清除缓存 next lint --no-cache

已在 #28596

中修复的已知问题

如果您不想更新到 Canary 版本并继续正常工作,只需将其从您的 .eslintrc.json 规则中排除:

"rules": {
    "@next/next/no-document-import-in-page": "off"
}

有关如何在此处排除规则的更多信息:disabling-rules