Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack trying to include too many modules when using knex.js on AWS Amplify

I'm encountering build errors with a next.js app on AWS Amplify while it builds and runs fine on my local machine. Specifically, it seems that webpack is trying to include all of the various SQL packages that knex might use. The error message is

Can't resolve 'tedious'

Since I am only connecting to a mysql server and have explicitly listed mysql2 in package.json, I don't need the tedious module as it appears to be for MS SQL Server. Adding tedious to package.json causes the build to complain about mssql, which is yet another module I don't actually need.

Based on this github knex issue, a workaround appears to be adding the various modules to the webpack externals config. The next.js custom webpack config documentation indicates that such settings should be in the next.config.js file. However, I can't seem to get the correct combination. Here is my next.config.js file:

module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.externals.concat([
        // Possible drivers (and related modules) for knex - we'll ignore them
        'tedious',
        'mssql',
        // etc. with more modules
    ]);
    return config;
  },
}

Unfortunately the error persists.

like image 514
John Cummings Avatar asked Sep 11 '25 04:09

John Cummings


1 Answers

It appears that I just missed the obvious error that caused the config to not actually get updated. The value of concat needs to be assigned back to config.externals like so:

config.externals = config.externals.concat(['tedious', /* etc. */]);

Someone upvoted the question so I presume it is useful to someone else.

like image 68
John Cummings Avatar answered Sep 16 '25 09:09

John Cummings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!