Skip to content

Commit b86a173

Browse files
jescalanijjk
andauthored
Error Message Clarity (vercel#16052)
* make the error message more clear if webpack config comes back undefined * Update check and add test * bump * Update build-output test Co-authored-by: JJ Kasper <[email protected]>
1 parent d016ead commit b86a173

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

errors/undefined-webpack-config.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Missing webpack config
2+
3+
#### Why This Error Occurred
4+
5+
The value returned from the custom `webpack` function in your `next.config.js` was undefined. This can occur from the initial config value not being returned.
6+
7+
#### Possible Ways to Fix It
8+
9+
Make sure to return the `webpack` config from your custom `webpack` function in your `next.config.js`
10+
11+
```js
12+
// next.config.js
13+
14+
module.exports = {
15+
webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
16+
// Note: we provide webpack above so you should not `require` it
17+
// Perform customizations to webpack config
18+
config.plugins.push(new webpack.IgnorePlugin(/\/__tests__\//))
19+
20+
// Important: return the modified config
21+
return config
22+
},
23+
}
24+
```
25+
26+
### Useful Links
27+
28+
- [Custom webpack config Documentation](https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config)

packages/next/build/webpack-config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,13 @@ export default async function getBaseWebpackConfig(
12241224
webpack,
12251225
})
12261226

1227+
if (!webpackConfig) {
1228+
throw new Error(
1229+
'Webpack config is undefined. You may have forgot to return properly from within the "webpack" method of your next.config.js.\n' +
1230+
'See more info here https://err.sh/next.js/undefined-webpack-config'
1231+
)
1232+
}
1233+
12271234
if (dev && originalDevtool !== webpackConfig.devtool) {
12281235
webpackConfig.devtool = originalDevtool
12291236
devtoolRevertWarning(originalDevtool)

test/integration/build-output/test/index.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ describe('Build Output', () => {
101101
expect(parseFloat(err404Size) - 3.6).toBeLessThanOrEqual(0)
102102
expect(err404Size.endsWith('kB')).toBe(true)
103103

104-
expect(parseFloat(err404FirstLoad) - 65.1).toBeLessThanOrEqual(0)
104+
expect(parseFloat(err404FirstLoad) - 65.2).toBeLessThanOrEqual(0)
105105
expect(err404FirstLoad.endsWith('kB')).toBe(true)
106106

107-
expect(parseFloat(sharedByAll) - 61.7).toBeLessThanOrEqual(0)
107+
expect(parseFloat(sharedByAll) - 61.8).toBeLessThanOrEqual(0)
108108
expect(sharedByAll.endsWith('kB')).toBe(true)
109109

110110
if (_appSize.endsWith('kB')) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
webpack() {},
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return 'Index page'
3+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-env jest */
2+
3+
import { join } from 'path'
4+
import { launchApp, nextBuild } from 'next-test-utils'
5+
6+
jest.setTimeout(1000 * 60 * 2)
7+
8+
const appDir = join(__dirname, '../')
9+
const expectedErr = /Webpack config is undefined. You may have forgot to return properly from within the "webpack" method of your next.config.js/
10+
11+
describe('undefined webpack config error', () => {
12+
it('should show in production mode', async () => {
13+
const result = await nextBuild(appDir, [], {
14+
stdout: true,
15+
stderr: true,
16+
})
17+
expect(result.stderr || '' + result.stdout || '').toMatch(expectedErr)
18+
})
19+
20+
it('should show in dev mode', async () => {
21+
let output = ''
22+
23+
await launchApp(appDir, [], {
24+
onStderr(msg) {
25+
output += msg || ''
26+
},
27+
ontStdout(msg) {
28+
output += msg || ''
29+
},
30+
})
31+
32+
expect(output).toMatch(expectedErr)
33+
})
34+
})

0 commit comments

Comments
 (0)