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)

Most upvoted comments

This needs to be reopened, This is an issue with how sequelize conditionally requires its dependencies. If I can’t just yarn add sequelize and have it work then it’s a problem on your end. I’m using mysql, not postgress so ideally the code that attempts require('pg-hstore') should never had ran. I didn’t use to have this issue so its due to a regression.

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:

WARNING in ./node_modules/sequelize/lib/dialects/mssql/connection-manager.js
Module not found: Error: Can't resolve 'tedious' in '/Users/louiskw/Documents/GitHub/WhoTargetsMe-Backend/node_modules/sequelize/lib/dialects/mssql'
 @ ./node_modules/sequelize/lib/dialects/mssql/connection-manager.js 23:19-37
 @ ./node_modules/sequelize/lib/dialects/mssql/index.js
 @ ./node_modules/sequelize/lib/sequelize.js
 @ ./node_modules/sequelize/index.js
 @ ./src/db/models.js
 @ ./src/handler.js

WARNING in ./node_modules/sequelize/lib/dialects/mysql/connection-manager.js
Module not found: Error: Can't resolve 'mysql2' in '/Users/louiskw/Documents/GitHub/WhoTargetsMe-Backend/node_modules/sequelize/lib/dialects/mysql'
 @ ./node_modules/sequelize/lib/dialects/mysql/connection-manager.js 33:19-36
 @ ./node_modules/sequelize/lib/dialects/mysql/index.js
 @ ./node_modules/sequelize/lib/sequelize.js
 @ ./node_modules/sequelize/index.js
 @ ./src/db/models.js
 @ ./src/handler.js

WARNING in ./node_modules/sequelize/lib/dialects/sqlite/connection-manager.js
Module not found: Error: Can't resolve 'sqlite3' in '/Users/louiskw/Documents/GitHub/WhoTargetsMe-Backend/node_modules/sequelize/lib/dialects/sqlite'
 @ ./node_modules/sequelize/lib/dialects/sqlite/connection-manager.js 27:19-37
 @ ./node_modules/sequelize/lib/dialects/sqlite/index.js
 @ ./node_modules/sequelize/lib/sequelize.js
 @ ./node_modules/sequelize/index.js
 @ ./src/db/models.js
 @ ./src/handler.js

WARNING in ./node_modules/sequelize/lib/sequelize.js
374:61-74 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/sequelize/lib/dialects/mssql/connection-manager.js
21:19-62 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/sequelize/lib/dialects/postgres/connection-manager.js
21:16-59 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/sequelize/lib/dialects/mysql/connection-manager.js
31:19-62 Critical dependency: the request of a dependency is an expression

WARNING in ./node_modules/sequelize/lib/dialects/sqlite/connection-manager.js
25:19-62 Critical dependency: the request of a dependency is an expression

ERROR in ./node_modules/sequelize/lib/dialects/mssql/query.js
Module not found: Error: Can't resolve 'tedious' in '/Users/louiskw/Documents/GitHub/WhoTargetsMe-Backend/node_modules/sequelize/lib/dialects/mssql'
 @ ./node_modules/sequelize/lib/dialects/mssql/query.js 10:14-32
 @ ./node_modules/sequelize/lib/dialects/mssql/index.js
 @ ./node_modules/sequelize/lib/sequelize.js
 @ ./node_modules/sequelize/index.js
 @ ./src/db/models.js
 @ ./src/handler.js

ERROR in ./node_modules/pg/lib/native/client.js
Module not found: Error: Can't resolve 'pg-native' in '/Users/louiskw/Documents/GitHub/WhoTargetsMe-Backend/node_modules/pg/lib/native'
 @ ./node_modules/pg/lib/native/client.js 10:13-33
 @ ./node_modules/pg/lib/native/index.js
 @ ./node_modules/pg/lib/index.js
 @ ./node_modules/sequelize/lib/dialects/postgres/connection-manager.js
 @ ./node_modules/sequelize/lib/dialects/postgres/index.js
 @ ./node_modules/sequelize/lib/sequelize.js
 @ ./node_modules/sequelize/index.js
 @ ./src/db/models.js
 @ ./src/handler.js

The following fix worked for me in my webpack.config.js:

var config = {
    ...
    externals: ['pg', 'sqlite3', 'tedious', 'pg-hstore'],
};

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:

new webpack.ContextReplacementPlugin(
            /Sequelize(\\|\/)/,
            path.resolve(__dirname, '../src')
          )

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:

externals: fs.readdirSync('node_modules').filter((x) => { return x !== '.bin'; }),

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 me


After 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 to

- dialectMap.postgres = require('./dialects/postgres/data-types')(DataTypes);
+if (!process.env.SEQUELIZE_DISABLE_POSTGRES) {
+  dialectMap.postgres = require('./dialects/postgres/data-types')(DataTypes);
+}

and just have Webpack users define process.env.SEQUELIZE_DISABLE_POSTGRES to be false at compile time with the DefinePlugin 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.:

import Sequelize from 'sequelize' // By default does not support any database
import mysqlDialect 'sequelize/dialects/mysql'
Sequelize.use(mysqlDialect)  // Does whatever is needed to register MySQL as a dialect

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.

Why are you trying to webpack sequelize at all - its a backend library? https://github.com/sequelize/sequelize/issues/6142#issuecomment-227659153

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

import { defineConfig } from "astro/config";
export default defineConfig({
  vite: {
    optimizeDeps: {
      exclude: ['sequelize', 'sqlite3'],
    },
  }
});

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 !!!