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
"Module not found" errors when using knex with webpack #1128
Comments
I had this issue, but adding a Regex to my externals in my webpack config fixed it:
|
+1 |
@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. |
Also ran into this. Not quite sure why ignoring node_modules doesn't work. |
also ran into this. what works for me; externals: {
knex: 'commonjs knex'
} |
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 |
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. |
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 |
@ifullgaz that doesnt seem to be an error, but just a warning that require() was not used with string literal? webpack/webpack#196 |
@ifullgaz, I've tried your solution, but I got errors as below. Did you run into a similar issue?
|
@brianhuangyl have you correct ownership etc. to the node_modules directory and all ist content (you have not ran npm install as root at any moment)?
|
@ifullgaz I found the suggestion from https://www.laurivan.com/make-electron-work-with-knex-js/ fixed these errors:
Specifically, this part worked for me:
|
this was the ticket for me |
I looked a slightly different approach which allows webpack to process most config.module.rules = [
{
include: [
/knex\/lib\/migrate\/index/,
/knex\/lib\/seed\/index/
],
loader: 'string-replace-loader',
options: {
search: 'require(\\([^\'"])',
replace: '__non_webpack_require__$1',
flags: 'g'
}
}
]; |
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 plugins: [
//...
fs.existsSync(path.join(params.targetPackageFilePath, 'migrations')) && new webpack.ContextReplacementPlugin(
/knex\/lib\/migrate/,
'.',
fs.readdirSync(path.join(params.targetPackageFilePath, 'migrations'))
.reduce((result, file) => {
return Object.assign(result, {
[path.join(params.targetPackageFilePath, 'migrations', file)]: path.relative(
path.dirname(require.resolve('knex/lib/migrate')),
path.join(params.targetPackageFilePath, 'migrations', file)
)
});
}, {})),
fs.existsSync(path.join(params.targetPackageFilePath, 'seeds')) && new webpack.ContextReplacementPlugin(
/knex\/lib\/seed/,
'.',
fs.readdirSync(path.join(params.targetPackageFilePath, 'seeds'))
.reduce((result, file) => {
return Object.assign(result, {
[path.join(params.targetPackageFilePath, 'seeds', file)]: path.relative(
path.dirname(require.resolve('knex/lib/seed')),
path.join(params.targetPackageFilePath, 'seeds', file)
)
});
}, {}))
//...
] In my config fs.existsSync(path.join(params.targetPackageFilePath, 'migrations')) && new webpack.ContextReplacementPlugin(
/knex\/lib\/migrate/,
'.',
{
'/Users/yourusername/yourproject/201706261234_migration.ts': '../../../migrations/201706261234_migration.ts'
}),
fs.existsSync(path.join(params.targetPackageFilePath, 'seeds')) && new webpack.ContextReplacementPlugin(
/knex\/lib\/seed/,
'.',
{
'/Users/yourusername/yourproject/seeds/201706261234_first-seed.ts': '../../../seeds/201706261234_first-seed.ts'
}), To break this down a little, the issue is that knex's migration and seed code calls 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. |
Any suggestion for this issue in Angular?
where i can do the configuration to remove this error? |
In webpack most probably. |
Externals entries need to be added to webpack for all of the possible database external requirements that *aren't* included in the api package.json. This is due to oddness in the way knex is referencing said libraries, see commentary at : knex/knex#1128
This fixed it for me (credit to @ifullgaz ) I had to add mysql2. externals: {
// Possible drivers for knex - we'll ignore them
sqlite3: 'sqlite3',
mysql2: 'mysql2',
mariasql: 'mariasql',
mysql: 'mysql',
oracle: 'oracle',
'strong-oracle': 'strong-oracle',
oracledb: 'oracledb',
pg: 'pg',
'pg-query-stream': 'pg-query-stream'
} Then just remove the driver that you're using from this list and webpack will include it in the build. |
This worked great for me. I did this:
|
2019/10 I managed to get it working, see my answer in another issue #1446 (comment) |
I put pg dependency as "external" and it solved the problem thanks a lot, does it mean that webpack parse the back-end dependencies ? |
Save my days, thanks so much. |
For those looking for a solution, this one worked for me using knex@^0.21.1, I wanted In webpack config merge in a modified version of #1128 (comment)
Also add in a webpack plugin, that will exclude the offending packages I modified the original to correctly exclude in my use case,including the correct set of
|
What do you think about using aliases? In my case I was having similar issues using https://classic.yarnpkg.com/en/docs/cli/add/#toc-yarn-add-alias https://docs.npmjs.com/cli/v6/commands/npm-install On your package.json (you may have to do the same for other drivers):
|
What I ended up doing was adding: externals: ["knex", "pg"] to npm --prefix ./dist install knex pg Where prefix is the webpack output directory, right after |
I fixed it in webpack 5 using the IgnorePlugin. const webpack = require('webpack');
//...snip
plugins: [
new webpack.IgnorePlugin({
resourceRegExp: /(sqlite3|pg|pg-query-stream|oracledb|mysql2|tedious)/
}),
] |
For the module.exports = {
//...snip
ignoreWarnings: [
{
module: /knex\/lib\/migrations/,
message: /the request of a dependency is an expression/,
}
],
} |
thanks, worked on my end with mysql |
I work with sqlite3.New version for ignore migrate and seed:
Externals for "module not found",plugins for "Critical dependency". |
In case it helps, I'm porting a NextJS v12 project to v13 using I was getting:
The solution was to add this to my webpack: (
config,
{ buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
) => {
config.externals.push({
knex: 'commonjs knex'
})
// Important: return the modified config
return config
}, Now, in import knex from "knex";
...
async function getData() {
const repos = await getReposWithStats();
return repos
}
... |
@peterbe That worked perfectly! Thank you!!!! |
Thanks for all the solutions so far! I tried adding the following to
downside is you get the experimental feature warning. |
this fixed the issue for me, slightly modified as I was using |
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?
The text was updated successfully, but these errors were encountered: