sequelize: Can't resolve 'pg-hstore'
May be necessary need move ‘pg-hstore’ with devDependencies in dependencies?
ERROR in ./~/sequelize/lib/dialects/postgres/hstore.js
Module not found: Error: Can't resolve 'pg-hstore' in 'node_modules/sequelize/lib/dialects/postgres'
@ ./~/sequelize/lib/dialects/postgres/hstore.js 3:15-35
@ ./~/sequelize/lib/dialects/postgres/data-types.js
@ ./~/sequelize/lib/data-types.js
@ ./~/sequelize/lib/sequelize.js
@ ./~/sequelize/index.js
Dialect: any Sequelize version: 4.0.0-2
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 26
- Comments: 47 (9 by maintainers)
This needs to be reopened, This is an issue with how
sequelize
conditionallyrequire
s its dependencies. If I can’t justyarn add sequelize
and have it work then it’s a problem on your end. I’m usingmysql
, notpostgress
so ideally the code that attemptsrequire('pg-hstore')
should never had ran. I didn’t use to have this issue so its due to a regression.https://github.com/sequelize/sequelize/search?q=webpack&type=Issues&utf8=✓
Even though i’m using Postgres as my dialect, it doesn’t seem like Tedious is a conditional import. Workaround by just installing Tedious but it is a little tedious 😉 having to bundle unnecessary modules
Webpack build warnings and errors:
The following fix worked for me in my
webpack.config.js
:Similar problem here when build with webpack. Using webpack-node-externals to make node_modules externals but does not help.
First possible workaround I found to build without errors is install all dialect libraries.
Second possible workaround is add array [“pg”, “sqlite3”, “tedious”, “pg-hstore”] to webpack externals, for me:
externals: [nodeExternals(), ["pg", "sqlite3", "tedious", "pg-hstore"]]
But warnings still remains - for example:
WARNING in ./node_modules/sequelize/lib/sequelize.js 374:61-74 Critical dependency: the request of a dependency is an expression
Inspirated from comment here https://github.com/angular/angular/issues/11580#issuecomment-282705332 I rewrite this piece of code for sequelize, and warnings dissapeared:
If anyone find better workaround, please write a comment. Also I agree that this issue should be reopened , as build bug using webpack.
EDIT: my previous answer was wrong. To fix this problem, the easiest solution is to configure Webpack to make all your node_modules
externals
:see https://webpack.js.org/configuration/externals/ and http://jlongster.com/Backend-Apps-with-Webpack--Part-I
When I build my project with webpack.
I had the same issue with NextJS 13, I’ve solved it by adding ‘sequelize’ to the ‘next.config.mjs’ under the ‘experimental’ label:
const nextConfig = { output: 'standalone', experimental: { appDir: true, serverComponentsExternalPackages: ['sequelize'] } } module.exports = nextConfig;
externals: ['pg', 'sqlite3', 'tedious', 'pg-hstore'],
fixed the issue for meAfter taking a deeper look, it seems the problem is on https://github.com/sequelize/sequelize/blob/master/src/data-types.js#L1023 where it imports every dialect. Webpack can’t tell at compile time that unused dialects will never be reached at runtime and thus will try to package every require into the final bundle.
A possible backwards-compatible solution would be to change
data-types.js
toand just have Webpack users define
process.env.SEQUELIZE_DISABLE_POSTGRES
to befalse
at compile time with theDefinePlugin
so that Webpack’s dead code elimination pass will avoid including the other dialect drivers.Another possible solution would be to refactor the codebase so that dialects are only enabled when they are imported e.g.:
I think the latter approach is what most libraries use nowadays
The
externals: ...
workaround you guys listed worked. However, this seems to have almost doubled my webpacked chunk sizes. What to do about it?@janmeier Can you reopen this issue? It is impossible to bundle up code using webpack without installing all dependencies that are required.
Were deploying single bundled up file to the production servers and here all data types are being required which breaks our build since not all of the required dependencies are installed https://github.com/sequelize/sequelize/blob/v4.1.0/lib/data-types.js#L842-L845
I agree this should not be closed…
Had this problem in Electron Forge w/ Webpack, trying to get a simple SQLite database and Sequelize wired up.
None of the webpack.config options resolved it so on a whim I simply installed pg and pg-hstore as devDependencies, and the error went away. I’m not even trying to use PostGreSQL, I’m using SQLite as my dialect. Strange.
Sorry to bring this back up, but I’m getting this same error when trying to run
sam build
for an AWS lambda. Using Node/TS. I’m just trying to use sequelize with mysql2.In AstroJS this is the intended way of solving this:
In the root folder create a file with the name
astro.config.mjs
Explanation: AstroJS uses Vite under the hood. This way it will be excluded from the build(?) process. I assume it tries to rebuild/treeshaking the dependencies. It works for me.
Source:
I’ve added a repo replicating this issue, for details see under the other issue at https://github.com/sequelize/sequelize/issues/13169#issuecomment-1387344221
There is nothing to fix on our end. Marking these packages as external is exactly what you need to do. They are not designed to be bundled.
Got this while using AstroJS on a SSG (fully backend)
i fix this problem in next js . my problem was used require() in src attribute img tag
Same problem here when trying to build single file application using webpack.
I suggest adding pg-hstore into dependencies instead of devDependencies.
Please I have same issue without having webpack !!!