title: Node Sequelize Mysql app "Encoding not recognized: 'undefined'" error
body: <p>I have a simple web app which uses Sequelize with Mysql. After a long time and a series of updates I did not keep track of (it worked fine a year ago), the app now crashes upon starting the server.
  I spent a couple of hours searching, but I did not find a satisfying answer. Does somebody know what's wrong?</p>
  <p>Here is a minimal working example:</p>
  <h3>Setup</h3>
  <p>I have a basic directory structure with the following files:</p>
  <pre><code>+ test-nodejs
  |- package.json
  |- package-lock.json
  |-+ node_modules
  |-+ server
    |- server.js
    |-+ models
      |- index.js
      |- user.model.js
  </code></pre>
  <p>I ran <code>npm init; npm install express; npm install sequelize</code> in the root dir.</p>
  <h3>Code</h3>
  <p>I believe the only relevant part of the code is in <code>server/server.js</code> and <code>server/models/index.js</code>. Here it is:</p>
  <h4><code>server/server.js</code></h4>
  <pre class="lang-js prettyprint-override"><code>const express = require(&quot;express&quot;);
  const app = express();
  
  const db = require(&quot;./models&quot;);
  db.sequelize.sync()
  
  app.get(&quot;/&quot;, (req, res) =&gt; {
    res.json({message: &quot;Welcome&quot;});
  });
  
  const PORT = 8080
  app.listen(PORT, () =&gt; {
      console.log(`Server is running on port ${PORT}`)
  });
  </code></pre>
  <h4><code>server/models/index.js</code></h4>
  <pre class="lang-js prettyprint-override"><code>const Sequelize = require(&quot;sequelize&quot;);
  const sequelize = new Sequelize(&quot;db&quot;, &quot;user&quot;, &quot;password&quot;, {host: 'localhost', dialect: 'mysql'});
  
  const db = {};
  db.Sequelize = Sequelize;
  db.sequelize = sequelize;
  
  db.users = require(&quot;./user.model.js&quot;)(sequelize, Sequelize); // This is actually not needed for the MWE
  
  module.exports = db;
  </code></pre>
  <h3>Output</h3>
  <p>When I run <code>node server.js</code> of this minimal working example, I get the following output:</p>
  <pre><code>Server is running on port 8080
  node:internal/process/promises:246
            triggerUncaughtException(err, true /* fromPromise */);
            ^
  
  Error
      at Query.run (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/mysql/query.js:52:25)
      at /home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:313:28
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async MySQLQueryInterface.databaseVersion (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/abstract/query-interface.js:69:12)
      at async Sequelize.databaseVersion (/home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:418:12)
      at async /home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:181:31
      at async ConnectionManager.getConnection (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/abstract/connection-manager.js:197:7)
      at async /home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:303:26
      at async Sequelize.authenticate (/home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:414:5)
      at async Sequelize.sync (/home/box/test-nodejs/node_modules/sequelize/dist/lib/sequelize.js:374:7) {
    name: 'SequelizeDatabaseError',
    parent: Error: Encoding not recognized: 'undefined' (searched as: 'undefined')
        at Object.getCodec (/home/box/test-nodejs/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104:23)
        at Object.getEncoder (/home/box/test-nodejs/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:115:23)
        at Object.exports.encode (/home/box/test-nodejs/node_modules/mysql2/lib/parsers/string.js:23:25)
        at Query.toPacket (/home/box/test-nodejs/node_modules/mysql2/lib/packets/query.js:16:30)
        at Query.start (/home/box/test-nodejs/node_modules/mysql2/lib/commands/query.js:60:38)
        at Query.execute (/home/box/test-nodejs/node_modules/mysql2/lib/commands/command.js:45:22)
        at Connection.handlePacket (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:456:32)
        at Connection.addCommand (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:478:12)
        at Connection.query (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:546:17)
        at results (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/mysql/query.js:60:22) {
      sql: 'SELECT VERSION() as `version`',
      parameters: undefined
    },
    original: Error: Encoding not recognized: 'undefined' (searched as: 'undefined')
        at Object.getCodec (/home/box/test-nodejs/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104:23)
        at Object.getEncoder (/home/box/test-nodejs/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:115:23)
        at Object.exports.encode (/home/box/test-nodejs/node_modules/mysql2/lib/parsers/string.js:23:25)
        at Query.toPacket (/home/box/test-nodejs/node_modules/mysql2/lib/packets/query.js:16:30)
        at Query.start (/home/box/test-nodejs/node_modules/mysql2/lib/commands/query.js:60:38)
        at Query.execute (/home/box/test-nodejs/node_modules/mysql2/lib/commands/command.js:45:22)
        at Connection.handlePacket (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:456:32)
        at Connection.addCommand (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:478:12)
        at Connection.query (/home/box/test-nodejs/node_modules/mysql2/lib/connection.js:546:17)
        at results (/home/box/test-nodejs/node_modules/sequelize/dist/lib/dialects/mysql/query.js:60:22) {
      sql: 'SELECT VERSION() as `version`',
      parameters: undefined
    },
    sql: 'SELECT VERSION() as `version`',
    parameters: {}
  }
  </code></pre>
  <h3>Environment</h3>
  <pre><code>$ mysql --version
  Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
  
  $ node --version
  v16.13.1
  
  $ npm --version
  8.3.0
  
  $ npm list
  test-nodejs@1.0.0 /home/box/test-nodejs
  ├── express@4.17.2
  ├── mysql2@2.3.3
  └── sequelize@6.12.0
  </code></pre>