Unverified Commit b1a7b888 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add warning when importing "next" directly (#35884)

* Add warning when importing "next" directly

* Apply suggestions from code review
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>

* Apply suggestions from code review
Co-authored-by: 's avatarSteven <steven@ceriously.com>

* add example
Co-authored-by: 's avatarBalázs Orbán <info@balazsorban.com>
Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent 384953b3
# Invalid "next" Import
#### Why This Error Occurred
Somewhere in your application, you imported `next` directly which is only meant to be used with legacy custom servers.
You should not import `next` inside of pages or components.
#### Possible Ways to Fix It
Ensure any usage of `import next from "next"` is specific to custom server usage and isn't included in your pages or components.
Also ensure any type imports are kept inside of TypeScript files e.g. ensure `import { PageConfig } from 'next'` isn't used in JavaScript files.
### Useful Links
- [Custom Server Documentation](https://nextjs.org/docs/advanced-features/custom-server)
...@@ -641,6 +641,10 @@ ...@@ -641,6 +641,10 @@
{ {
"title": "client-flush-effects", "title": "client-flush-effects",
"path": "/errors/client-flush-effects.md" "path": "/errors/client-flush-effects.md"
},
{
"title": "import-next",
"path": "/errors/import-next.md"
} }
] ]
} }
......
...@@ -841,6 +841,12 @@ export default async function getBaseWebpackConfig( ...@@ -841,6 +841,12 @@ export default async function getBaseWebpackConfig(
// absolute paths. // absolute paths.
(process.platform === 'win32' && path.win32.isAbsolute(request)) (process.platform === 'win32' && path.win32.isAbsolute(request))
// make sure import "next" shows a warning when imported
// in pages/components
if (request === 'next') {
return `commonjs next/dist/lib/import-next-warning`
}
// Relative requires don't need custom resolution, because they // Relative requires don't need custom resolution, because they
// are relative to requests we've already resolved here. // are relative to requests we've already resolved here.
// Absolute requires (require('/foo')) are extremely uncommon, but // Absolute requires (require('/foo')) are extremely uncommon, but
......
import * as Log from '../build/output/log'
Log.warn(
`"next" should not be imported directly, imported in ${module.parent?.filename}\nSee more info here: https://nextjs.org/docs/messages/import-next`
)
...@@ -19,6 +19,7 @@ describe('should set-up next', () => { ...@@ -19,6 +19,7 @@ describe('should set-up next', () => {
let server let server
let appPort let appPort
let errors = [] let errors = []
let stderr = ''
let requiredFilesManifest let requiredFilesManifest
beforeAll(async () => { beforeAll(async () => {
...@@ -109,6 +110,7 @@ describe('should set-up next', () => { ...@@ -109,6 +110,7 @@ describe('should set-up next', () => {
if (msg.includes('top-level')) { if (msg.includes('top-level')) {
errors.push(msg) errors.push(msg)
} }
stderr += msg
}, },
} }
) )
...@@ -118,6 +120,14 @@ describe('should set-up next', () => { ...@@ -118,6 +120,14 @@ describe('should set-up next', () => {
if (server) await killApp(server) if (server) await killApp(server)
}) })
it('should warn when "next" is imported directly', async () => {
await renderViaHTTP(appPort, '/gssp')
await check(
() => stderr,
/"next" should not be imported directly, imported in/
)
})
it('`compress` should be `true` by default', async () => { it('`compress` should be `true` by default', async () => {
expect( expect(
await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8') await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8')
......
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
// eslint-disable-next-line
import next from 'next' // force a warning during `next build`
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
export async function getServerSideProps({ res }) { export async function getServerSideProps({ res }) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment