knex: "Module not found" errors when using knex with webpack
I am trying to use knex in a project I’m working on. The problem I’m having is that when I try to run webpack it is tracing the requires and attempting to pull in drivers/dialects that I’m not using and don’t have installed.
Any thoughts on how this can be solved?
ERROR in ./~/mariasql/lib/Client.js
Module not found: Error: Cannot resolve 'file' or 'directory' ../build/Debug/sqlclient in ~/project/node_modules/mariasql/lib
@ ./~/mariasql/lib/Client.js 17:10-45
ERROR in ./~/knex/lib/dialects/sqlite3/index.js
Module not found: Error: Cannot resolve module 'sqlite3' in ~/project/node_modules/knex/lib/dialects/sqlite3
@ ./~/knex/lib/dialects/sqlite3/index.js 33:11-29
ERROR in ./~/knex/lib/dialects/mysql2/index.js
Module not found: Error: Cannot resolve module 'mysql2' in ~/project/node_modules/knex/lib/dialects/mysql2
@ ./~/knex/lib/dialects/mysql2/index.js 33:11-28
ERROR in ./~/knex/lib/dialects/mysql/index.js
Module not found: Error: Cannot resolve module 'mysql' in ~/project/node_modules/knex/lib/dialects/mysql
@ ./~/knex/lib/dialects/mysql/index.js 35:11-27
ERROR in ./~/knex/lib/dialects/oracle/index.js
Module not found: Error: Cannot resolve module 'oracle' in ~/project/node_modules/knex/lib/dialects/oracle
@ ./~/knex/lib/dialects/oracle/index.js 40:11-28
ERROR in ./~/knex/lib/dialects/postgres/index.js
Module not found: Error: Cannot resolve module 'pg' in ~/project/node_modules/knex/lib/dialects/postgres
@ ./~/knex/lib/dialects/postgres/index.js 46:11-24
ERROR in ./~/knex/lib/dialects/postgres/index.js
Module not found: Error: Cannot resolve module 'pg-query-stream' in ~/project/node_modules/knex/lib/dialects/postgres
@ ./~/knex/lib/dialects/postgres/index.js 132:50-76
ERROR in ./~/knex/lib/dialects/strong-oracle/index.js
Module not found: Error: Cannot resolve module 'strong-oracle' in ~/project/node_modules/knex/lib/dialects/strong-oracle
@ ./~/knex/lib/dialects/strong-oracle/index.js 15:9-33```
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 42
- Comments: 33 (4 by maintainers)
also ran into this. what works for me;
This issue seems to be just a problem how to setup webpack configuration when including knex… lets reopen if there are changes required in knex to support webpack better.
In case it helps, I’m porting a NextJS v12 project to v13 using
appdirectories.I was getting:
The solution was to add this to my
next.config.jsNow, in
app/page.tsxI can continue to use:I can’t comment specifically on the mariasql problem but what solved my problem is to define all the drivers I don’t use as external. For instance, I use the mysql2 driver for my project so I have this in externals:
The result is that knex gets bundled without errors or warnings
@joeketchi Ignoring the node_modules directory doesn’t really solve the issue, we’re just working around the issue. It would be awesome if the code would do these requires in a more intelligent way that doesn’t throw errors.
Thanks for all the solutions so far! I tried adding the following to
next.config.jsand that seems to get rid of the issue as well if you don’t want to be creating your own webpack or pushing to config:downside is you get the experimental feature warning.
I had this issue, but adding a Regex to my externals in my webpack config fixed it:
I fixed it in webpack 5 using the IgnorePlugin.
Hi folks, I was working to solve the problem of creating a complete webpack bundle for a service using knex and ran into this issue when trying to run migrations programmatically from that bundle. @mdlavin 's suggestion did get it to run the require but, it resulted in webpack not transforming the source file. It also meant I’d need to include that directory with the bundle out of band from the webpack bundle.
I figured out that you can use webpack’s
ContextReplacementPluginto resolve the issue with migrations and seeds. Below is a sample from my config (written in typescript):In my config
params.targetPackageFiletPathis the resolved path to the package being compiled e.g./Users/yourusername/yourproject. So if a project at that path hasmigrationsandseedsdirectories the resulting plugin configurations would look something like this:To break this down a little, the issue is that knex’s migration and seed code calls
requireto load your migrations and seeds, as it does here https://github.com/tgriesser/knex/blob/master/src/migrate/index.js#L54. It passes the result of a function/expression to require with no hard-coded string prefix or suffix so, when webpack attempts to figure out what files that might actually load it doesn’t have enough information to determine that. These plugins provide that needed information to webpack.I’m not sure if Knex could be changed to make these unnecessary when bundling with webpack. Maybe by doing the requires in user-land?
Anyway, thought I’d share a solution that worked for me.
Actually, I don’t think the issue should be closed yet, there are still unresolved problems with webpack, with migration and seed:
I couldn’t fix these
What I ended up doing was adding:
to
webpack.config.jsso that it doesn’t bundle them (adjust for the connector you’re using), and then doing:Where prefix is the webpack output directory, right after
npm run build(orwebpackcommand)2019/10 I managed to get it working, see my answer in another issue https://github.com/tgriesser/knex/issues/1446#issuecomment-537715431
This worked great for me. I did this:
noop.jsto work because it was trying to import it deep inside knex. I just installed this simplenode-nooplib and that worked fine.Any suggestion for this issue in Angular? I try using express project, it is normal (not showing any error). But when I use it in Angular project, it shows the error:
ERROR in ./node_modules/knex/lib/dialects/oracle/utils.js Module not found: Error: Can't resolve 'crypto' in '/Users/marudits/Documents/CODE/PROJECT/MOJOMARET/app-member/node_modules/knex/lib/dialects/oracle' ERROR in ./node_modules/knex/lib/dialects/mssql/index.js Module not found: Error: Can't resolve 'mssql/package.json' in '/Users/marudits/Documents/CODE/PROJECT/MOJOMARET/app-member/node_modules/knex/lib/dialects/mssql' ERROR in ./node_modules/knex/lib/runner.js Module not found: Error: Can't resolve 'stream' in '/Users/marudits/Documents/CODE/PROJECT/MOJOMARET/app-member/node_modules/knex/lib' ERROR in ./node_modules/knex/lib/dialects/oracledb/index.js Module not found: Error: Can't resolve 'stream' in '/Users/marudits/Documents/CODE/PROJECT/MOJOMARET/app-member/node_modules/knex/lib/dialects/oracledb'where i can do the configuration to remove this error? could it be set up on
angular.jsonortsconfig.jsonfile?I looked a slightly different approach which allows webpack to process most
requirestatements in the knex files, but will leave the non-literalrequires alone so that the migration and seed files provided by the user of the library be found. Here is the approach I took: