next.js: False positive for `no-document-import-in-page`
What version of Next.js are you using?
11.1.0
What version of Node.js are you using?
14.17.1
What browser are you using?
Chrome
What operating system are you using?
Windows
How are you deploying your application?
next start
Describe the Bug
The @next/next/no-document-import-in-page ESLint rule displays an error in a custom _document if it is located at pages/_document/index.js, pages/_document/index.jsx, pages/_document/index.ts, or pages/_document/index.tsx.
Expected Behavior
I expect there to be no ESLint error from the @next/next/no-document-import-in-page rule in all valid custom _document file locations.
To Reproduce
Create pages/_document/index.jsx as follows with the ESLint rule enabled.
import Document, { Html, Head, Main, NextScript } from 'next/document';
class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;
This results in an error on line 1.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 31
- Comments: 36 (8 by maintainers)
Commits related to this issue
- Fix `next lint` no-document-import-in-page on Windows (#28745) Fixes #28596 — committed to vercel/next.js by styfle 3 years ago
- https://github.com/vercel/next.js/issues/28596 — committed to yudejp/yude.jp_v1 by yude 3 years ago
I’m getting the same eslint error after updating next.js from 11.1.0 to 11.1.1. I don’t recall getting this error when I was on next.js 11.1.0. In my case, the custom
_document.tsxfile is located undersrc/pages.other npm packages I updated that might cause this error:
Also, I should mention that I have upgraded to the latest typescript 4.4.2. And it seems upgrading
next&eslint-config-nextfrom 11.1.0 to 11.1.1 causes this eslint issue for me.Can confirm as well that it’s reproducible on Windows. Letting the Next.js team know!
It is introduced by v11.1.1-canary.17 with:
no-import-document-in-pagerule: #28261No problems with v11.1.1-canary.16.
This issue occurred in
11.1.2.I get the same false positive when using npm
next build.All I did was update from Next v11.1.0 to Next 11.1.2 and update eslint-config-next from 11.1.0 to 11.1.2.
I’m on Windows 10, using node v16.7.0. Everything was fine with next 11.1.0
To everyone who still has the issue after upgrading to both Next.js and Eslint >= v11.1.3-canary.18 - run
npx next lint --no-cacheAlso, I had run
npm install --forcewhen upgrading to@housseindjirdeh @samsisle - I can confirm that this issue only affects Windows users. I just tried on my Ubuntu VM and am able to build without error.
TLDR; it works for me (on both v11.1.3-canary.18 and v11.1.2)
I no longer get this error in either v11.1.3-canary.18 or v11.1.2. I thought I would still get this error on v11.1.2, but I don’t anymore. The important thing I did was run
next lint --no-cache. I believe running this is what allowed me to switch from v11.1.3-canary.18 to v11.1.2 without running into this error.Steps I took:
next lint --no-cachenext build(I no longer get the error)next build(I no longer get the error)Update: I just checked again, and it works on v11.1.3-canary.22 (but not on v11.1.2). Not sure why it worked on v11.1.2 yesterday, but it only works on v11.13-canary for me. The important thing is that the bug seems to be fixed on my end 😄
Huh, why was this reopened?
Thanks so much for the reproduction steps @kimbaudi and @rtritto. I also haven’t been able to reproduce using either of your examples so this seems like it might be an issue only occurring on Windows machines.
(Thanks @samsisle for trying them both out as well 🙏 )
Thanks for logging this issue @GrantGryczan, and thanks to everyone else sharing their details which always helps. For some reason, I can’t reproduce this. I’ve tried next & eslint-config-next versions 11.1.0, 11.1.1, and 11.1.2 and it seems to work fine . I’ve even tried spinning up a new application and still couldn’t reproduce it:
npx create-next-app test-next-appcd test-next-apppackage.json(bothnext&eslint-config-nextare versions 11.1.2)pages/_document/index.jsxwith a custom document:yarn lintI’ve also tried variations of the document file (
pages/_document/index.jsx,src/pages/_document/index.js,pages/_document.tsx) and still not seeing the issue.That’s the only major change that’s been introduced to the file since 11.1.0, but doesn’t seem to be causing the error on my machine. I can revert the changes if necessary though.
Can anyone share their repository of where they’re seeing this issue if it’s public? Not sure if I’m missing something obvious here 😅 . Also, I would suggest running
yarn lint --no-cachein case caching is causing this wonky behavior, so if anyone finds that happens to resolve this issue please let me know.I got this issue with Next being version
11.0.1. I see that my@next/eslint-plugin-nextpackage updated automatically by Yarn from11.1.0to11.1.2. So i assume this is the cause. Added ignore line comment temporaly.