Next.js: Webpack config.externals is undefined in Next 8.1.0?

Created on 17 Apr 2019  路  8Comments  路  Source: vercel/next.js

Question about Next.js

I'm not sure if this qualifies as a "bug" per se, as we're engaging with some wild chicanery in our next.config.js, but there was a change in [email protected] that broke our current configuration.

next.config.js (simplified)

module.exports = withTypescript({
    webpack: (config, options) => {
        const updatedConfig = withSass({
            // SASS options here
        }).webpack(config, options);

        updatedConfig.resolve.symlinks = false;
        updatedConfig.externals = config.externals.map(external => {
            // Some custom dependency resolution here
        });

        // Returns the customized Next.js configuration
        return updatedConfig;
    }
});

This causes no problems on [email protected] - however, after updating to 8.1.0 I started seeing the following error:

(node:45880) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'map' of undefined
    at Object.webpack (/Users/aeksco/project/web/next.config.js:43:52)
    at Object.webpack (/Users/aeksco/project/web/node_modules/@zeit/next-typescript/index.js:57:27)
    at Object.getBaseWebpackConfig [as default] (/Users/aeksco/project/web/node_modules/next/dist/build/webpack-config.js:308:32)
    at HotReloader.getWebpackConfig (/Users/aeksco/project/web/node_modules/next/dist/server/hot-reloader.js:162:37)

It appears that the Webpack configuration object passed into the webpack function does not include an externals array, which causes our customized configuration to explode when we try to run config.externals.map(...)

I was curious if the missing config.externals array is a bug, or if we're doing something that's so completely non-standard that the problem is really with our approach.

Any help or insight is appreciated - thanks!

Most helpful comment

Note that you should actually make the value undefined if you're not adding anything to it.

All 8 comments

experiencing similar issues since 8.1.0

my problem was located here: https://github.com/martpie/next-transpile-modules/issues/29 and latest version fixes it

It's not a bug. Not adding the config value makes the plugin not load in webpack, which is better for performance.

@timneutkens Thanks for the reply and the context! I've added the following line to my next.config.js file which solves the problem:

config.externals = config.externals || [];

Glad to know this is actually a _solution_ to the problem and not just a kludgey band-aid. Thanks again!

Note that you should actually make the value undefined if you're not adding anything to it.

same issue with nextjs ^9.1.7

@derit as explained in the issue, this is not a bug, but a problem with your custom config.

@Timer yes, i solved with validate config external

Was this page helpful?
0 / 5 - 0 ratings

Related issues

havefive picture havefive  路  3Comments

rauchg picture rauchg  路  3Comments

knipferrc picture knipferrc  路  3Comments

ghost picture ghost  路  3Comments

kenji4569 picture kenji4569  路  3Comments