Repository navigation
Description
Hello everyone. Many people have reported problems with Sequelize + Webpack in the last few years. I can guess that the root of the problem is some kind of dynamic require involving the optional dialects supported by Sequelize.
However, I have had a hard time exploring the problem for a few reasons:
- I never used Webpack
- Most resources I found on the internet were thinking of Webpack for the frontend (which is probably the most popular usage)
- The resources I found about Webpack for backend seemed very outdated
- My quick attempts of reproducing the problem failed
- I also haven't used things like Serverless, Azure and AWS Lambda (which seem to be mentioned in the same issues) - I don't have enough knowledge to decide whether or not those are just red herrings and all issues stem from the first cause or not.
- Most issues were opened several years ago and I couldn't be sure if the original problem (mentioned in the first post) still happens or if people are just using the related issue to comment about related (but not-exactly-the-same) problems.
- My lack of experience/knowledge with bundlers like Webpack does not allow me to evaluate if the problems are Sequelize's fault, Webpack's fault or the users' fault (like a misconfiguration of some kind).
I noticed two main types of issues:
- ["Type A"] Errors when executing Webpack, usually in the form of
Can't resolve {some-package}, such as this one from 2017: Can't resolve 'pg-hstore' #7509 - ["Type B"] Errors when running code processed by Webpack, usually in the form of
Please install {some-package} manually, such as this one from 2018: Please install mysql2 package manually #9489
My first guess is that the problem is in this code. Especially since the other place which used to perform dynamic requires in our source code was refactored in 2019 via #11464.
However, I need to really understand the problem in order to fix it, because I want to write a failing test first, and only then fix this. The failing test will probably be a separate workflow for GitHub actions which is going to really install Webpack and run it and see that it works.
Can anyone create a new MCVE for both cases (Type A and Type B) in the form of a public GitHub repository that I can clone and run npm install && npm start to see the problem?
I will then use that as a starting point for the new GitHub workflow that is going to test this.
Activity
papb commentedon Apr 4, 2021
By the way, here is my quick (failed) attempt to see the first type of problem happening myself (after following a quick Webpack setup from a tutorial):
papb commentedon Apr 4, 2021
After looking at #9608, I am wondering if the
dialectModuleoption solves at least part of these problems?AwolDes commentedon Apr 4, 2021
@papb from what I know & from what I have experienced, you are right when you say:
Webpack is used to bundle javascript, both on the frontend & on the server (especially when using Lambda and similar serverless solutions).
I fixed both "Type A" and "Type B" errors by using
requiresyntax, and notimportsyntax for packages, particularly Sequelize (although I think this might come down to my configuration, I'm not 100% sure). My PoC repo https://github.com/AwolDes/express-sequlize-lambda-webpack-poc mentioned in #12874 might be a good place to start to try and re-produce. I wonder if adding the changes in #9609 would helpTrinovantes commentedon Apr 5, 2021
Here's an example for type A using latest Webpack https://github.com/Trinovantes/issue-sequelize-webpack
As I mentioned this in #7509 (comment), Webpack bundles every dependency into a single file. If it can't know for sure at compile time that something may/may not be needed, it includes it to be safe:
sequelize/src/data-types.js
Lines 1023 to 1027 in a8b8b7f
From Webpack's perspective, all of these
requires might be executed at runtime so it'll try to bundle them and failKeimeno commentedon Apr 5, 2021
A possible solution might be to dynamically require all dialects:
or only require if a given dialect should be used
But this could potentially still cause problems with webpack, as the dialects might not be included at all in that case.
Trinovantes commentedon Apr 6, 2021
I think a plugin system would work:
This is similar to how other libraries like dayjs handle optional components and compatibility with webpack https://day.js.org/docs/en/plugin/relative-time
HirenPatel2791 commentedon Apr 12, 2021
This worked for me for serverless-bundle.
serverless.yml (this solves: 'Module not found: Error: Can't resolve 'pg-hstore' in '/Users/.../node_modules/sequelize/lib/dialects/postgres')
Now since it's ignoring the pg dialect module, I need to define my dialect module like when init sequelize.
invaderb commentedon Jun 3, 2021
I was able to get sequelize working with serverless-bundle with @HirenPatel2791 recommendation, but not with just serverless-webpack.
serverless bundle doesn't leave much for configuration and no minification so the files are much larger.
@papb with serverless-webpack I tried the dialectModule and was still getting errors and warnings.
github-actions commentedon Oct 27, 2021
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
github-actions commentedon Nov 5, 2021
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
6 remaining items
ephys commentedon Jan 18, 2023
You need to install
pg-hstoreif you want to bundle sequelize instead of loading it as an externalIn Sequelize 6,
pg-hstoreis an optional dependency that you must install if you useDataTypes.HSTORE. It's loaded dynamicallyRollup can't do that so it tries to bundle it, so you need to either make it an external or install it
In Sequelize 7,
pg-hstoreis always installed because it's a tiny library and we're going to move each dialect to its own package (e.g.@sequelize/postgres) which should resolve this problem. (#15553)The dialect split should also resolve the remaining bundling issues as we will not be loading dependencies dynamically anymore
bhallstein commentedon Jan 19, 2023
Thanks very much for the informative reply @ephys! Simply installing pg-hstore does seem to resolve the missing dependency issue, and as you say, it's not a large dependency.
I looked further into handling dynamic require rollup-side, and in fact the commonjs plugin has config to allow this — this is the config that I'm currently testing (currently partly blocked on this due to another tooling issue, will update this comment when able to test fully):
The dialect split sounds like a good resolution for the future.
Thanks again for your help.
Zenlight commentedon Jan 22, 2023
Hello all! I happen to just receive the same error as above in addition to one more. The first one went away when I installed
pg-hstore.However, now I'm left with this error:
Ive tried the
externalsandpluginsfix, but I'm still receiving the error above.Any help is appreciated, thank you!
bhallstein commentedon Jan 22, 2023
@Zenlight Vue builds for the browser, where node’s library functions aren’t available – see #6424
You might be able to shim the node library using browserify, but you’re likely to be better off using a library built for the browser. Maybe have a look at knex?
Zenlight commentedon Jan 22, 2023
Hi @bhallstein. I will look into that tomorrow and get back you. Thanks for the quick response!
xlinxie commentedon May 13, 2023
dialectModulesolves the Please install {some-package} manually problemsallyzoughaib commentedon Dec 1, 2023
@bhallstein I am minifying and bundling a Node.js TypeScript backend project that uses Sequelize to connect with a database. I used Webpack for this task and successfully generated the 'bundle.js' file. However, when I ran 'node dist/bundle.js,' the server started, but any request requiring a database connection resulted in an error. After debugging and investigating, I discovered that I am missing Sequelize configurations in my 'web.config.js' file. Could you please help me identify what's missing?
this is my webpack.config.js:
bhallstein commentedon Dec 3, 2023
@sallyzoughaib Happy to try to help but I don't want to spam this issue (to be clear I'm not affiliated with Sequelize). If you have a Discord account feel free to repost the question in #help-programming on https://discord.gg/C2JpWRcG
sallyzoughaib commentedon Dec 4, 2023
@bhallstein thank you,
Now I am attempting using Rollup to bundle my Node.js TypeScript backend application code and dependencies (node_modules) into a single JavaScript file named "bundle.js".
I got this error when i ran npm run build
where build : rollup -c
Error: [!] (plugin rpt2) Error: Unexpected character '�' (Note that you need plugins to import files that are not JavaScript)node_modules/@sentry/profiling-node/lib/sentry_cpu_profiler-darwin-x64-93.node (1:0)
This is my rollup.config.js filer:
import alias from '@rollup/plugin-alias';
import nodeBuiltins from 'rollup-plugin-node-builtins';
import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import tslibResolveId from 'rollup-plugin-tslib-resolve-id';
import json from '@rollup/plugin-json';
export default {
input: 'src/server.ts',
output: {
file: 'dist/bundle.js',
format: 'es',
},
plugins: [
nodeBuiltins(),
tslibResolveId(),
typescript(),
json(),
commonjs({
include: 'node_modules/**',
ignoreGlobal: false,
sourceMap: false,
}),
alias({
entries: [
{ find: '@config', replacement: 'config' },
{ find: '@controllers', replacement: 'controllers' },
{ find: '@middlewares', replacement: 'middlewares' },
{ find: '@routes', replacement: 'routes' },
{ find: '@services', replacement: 'services' },
],
}),
resolve(),
],
};
anotherCoward commentedon Apr 3, 2024
I was having a similar issues with nextjs (14.1.4), sequelize (6.37.1) and mysql2 (3.9.3).
It was running fine under
devbut when I triedbuildit failed because mysql2 getsrequire()'d inside a constructor function and this seems to be ignored by webpack, when generating the bundles.Without the prop
dialectModule, I always got the MODULE_NOT_FOUND Error. So it was kinda mandatory to implement it there.By using the property
dialectModulein the database instanciate script it raised an Error without any matching error message during build. The child process is just stopped with code: 3221225725 (0xc00000fd) which refers to endless recursion.I was able to solve the issue by altering the webpack config from within the
next.config, which just adds"mysql2"toconfig.externals.The main cause for the issue are the
dialects\[database]scripts and how the drivers are loaded there.webpack is unable to find the node module when
_loadDialectModule(modulename)is called from the abstractConnectionManagerclass after webpack bundled it.The best solution would be to alter the
connection-manager.jsfiles slightly, as this abstractrequire()-usage is causing the problem. Webpack also logsCritical dependency: the request of a dependency is an expressionwhenrequire()is used that way and always refers to theconnection-manager-files from the driver/dialect used.See also vercel/next.js#50587
ephys commentedon Apr 3, 2024
All dynamic requires are being phased out in Sequelize 7.
_loadDialectModulehas already been fully removed. These changes are not released yet and are tracked in #13722This should make Sequelize itself bundable, but I can't speak for the libraries we use that handle connecting to the database
anotherCoward commentedon Apr 3, 2024
@ephys that's good news, but there are still tons of projects using older versions, so please keep that in mind too. Some might also require bundling Sequelize 6 (or older) in the
package.json.It would be easily possible to avoid the
module not founderror if theconnection-manager.jsfiles were slightly altered.Example:
From the current
sequelize\lib\mysql\connection-manager.js(6.37.2):to:
Also, an upgrade to a new major version might present other challenges, such as rewriting several parts of the project if a method usage was altered.
I've simply added my research and results for others who are facing these issues. I couldn't find any other solution for the issue that works properly with Next.js 14.1 built-in webpack bundler.
Thanks for your time.
ephys commentedon Apr 3, 2024
Unfortunately that is not a change we can implement as it would require all users to install
mysql2, instead of just the users that are using the mysql dialectIf you use
dialectModule, try setting it the result ofimport * as mysql2 from "mysql2";instead ofimport mysql2 from "mysql2";anotherCoward commentedon Apr 3, 2024
@ephys yeah, tough challenge you are facing there. 😅
Just a suggestion then. It would be nice to see this informations in the documentation under
Environment specific things. (also for v6)It really took a while to find the cause of the issue, especially when the Jest worker-error showed up without any kind of debugging details.
The mentioned solution
isn't really recommended as the experimental feature might change or could be removed at any time.
cirosantilli commentedon Jul 27, 2024
The following is a minimal example of "Please install {some-package} manually". If you replace sequelize with webpack compatible package like lodash it works.
Adding:
as a workaround to another issue even though it should not be needed in this sqlite example.
webpack.config.js
package.json
index.js
run