Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper error when failing to load next.config.js #28099

Merged
merged 3 commits into from Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions errors/manifest.json
Expand Up @@ -442,6 +442,10 @@
{
"title": "module-not-found",
"path": "/errors/module-not-found.md"
},
{
"title": "next-config-error",
"path": "/errors/next-config-error.md"
}
]
}
Expand Down
14 changes: 14 additions & 0 deletions errors/next-config-error.md
@@ -0,0 +1,14 @@
# next.config.js Loading Error

#### Why This Error Occurred

When attempting to load your `next.config.js` file an error occurred. This could be due to a syntax error or attempting to `require` a module that wasn't available.

#### Possible Ways to Fix It

See the error message in your terminal where you started `next` to see more context. The `next.config.js` file is not transpiled by Next.js currently so ensure only features supported by your current node.js version are being used.

### Useful Links

- [next.config.js documentation](https://nextjs.org/docs/api-reference/next.config.js/introduction)
- [node.js version feature chart](https://node.green/)
12 changes: 11 additions & 1 deletion packages/next/server/config.ts
Expand Up @@ -467,7 +467,17 @@ export default async function loadConfig(

// If config file was found
if (path?.length) {
const userConfigModule = require(path)
let userConfigModule: any

try {
userConfigModule = require(path)
} catch (err) {
console.error(
chalk.red('Error:') +
' failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error'
)
throw err
}
const userConfig = normalizeConfig(
phase,
userConfigModule.default || userConfigModule
Expand Down
3 changes: 3 additions & 0 deletions test/integration/config-syntax-error/pages/index.js
@@ -0,0 +1,3 @@
export default function Page(props) {
return <p>index page</p>
}
30 changes: 30 additions & 0 deletions test/integration/config-syntax-error/test/index.test.js
@@ -0,0 +1,30 @@
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import { nextBuild } from 'next-test-utils'

jest.setTimeout(1000 * 60 * 1)
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')

describe('Invalid resolve alias', () => {
it('should show relevant error when webpack resolve alias is wrong', async () => {
await fs.writeFile(
nextConfig,
`
module.exports = {
reactStrictMode: true,,
}
`
)
const { stderr } = await nextBuild(appDir, undefined, {
stderr: true,
})
await fs.remove(nextConfig)

expect(stderr).toContain(
'Error: failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error'
)
expect(stderr).toContain('SyntaxError')
})
})