Skip to content

Please install mysql2 package manually #9489

Closed

Description

@wuxiaolan91

What are you doing?

when I run 'node app.js' ,throw error 'new Error('Please install mysql2 package manually');'
but i'm installed mysql2.
my local client haven't this problem, just my ubuntu 14 server have it.

You must set the NODE_ENV environment variable
/home/www/test/tougao-server/node_modules/_sequelize@4.37.10@sequelize/lib/dialects/mysql/connection-manager.js:38
        throw new Error('Please install mysql2 package manually');
        ^

Error: Please install mysql2 package manually
    at new ConnectionManager (/home/www/test/tougao-server/node_modules/_sequelize@4.37.10@sequelize/lib/dialects/mysql/connection-manager.js:38:15)
    at new MysqlDialect (/home/www/test/tougao-server/node_modules/_sequelize@4.37.10@sequelize/lib/dialects/mysql/index.js:14:30)
    at new Sequelize (/home/www/test/tougao-server/node_modules/_sequelize@4.37.10@sequelize/lib/sequelize.js:239:20)
    at Object.<anonymous> (/home/www/test/tougao-server/controllers/adController.js:3:19)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)
    at Module.require (internal/modules/cjs/loader.js:626:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/www/test/tougao-server/router/ad.js:2:20)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)
    at Module.require (internal/modules/cjs/loader.js:626:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at /home/www/test/tougao-server/router/index.js:37:17
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/home/www/test/tougao-server/router/index.js:36:4)

What do you expect to happen?

I wanted Foo!

What is actually happening?

But the output was bar!

Output, either JSON or SQL

Dialect: mysql
__Database version:_5.5.60
__Sequelize version: 4.37.10
__Tested with latest release: no

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. sushantdhiman commented on May 29, 2018

    @sushantdhiman
    Contributor

    Try removing your node_modules folder then make sure both dependencies are installed locally. I am closing this issue as it seems to be a dependency issue, feel free to comment if it still doesn't works.

  2. cleiton-tavares commented on Nov 26, 2018

    @cleiton-tavares

    I have the same problem, I have already tried to remove the node_modules folder including globally install the mysql2 package but every time I run any command Sequelize CLI -" ERROR: Please install mysql2 package manually "

    I tried running CLI locally and globally as well.

    My specs -> Sequelize CLI [Node: 10.12.0, CLI: 5.3.0, ORM: 4.38.1] and mysql2 1.6.4

  3. pasha1307 commented on Nov 26, 2018

    @pasha1307

    ..I am having the same issue with the error when try to run db:migrate
    Sequelize CLI [Node: 11.2.0, CLI: 5.3.0, ORM: 4.41.2] Loaded configuration file "config/config.json".
    Using environment "development".
    ERROR: Please install mysql2 package manually.
    Tried all the above and more - does not help. Is there any explanation why and how to fix it?

  4. vmarushkoossystem commented on Dec 13, 2018

    @vmarushkoossystem

    Hi!

    Try to understand real trouble. You need to find the following code and add console.log() for displaying error's description:

    File: node_modules/sequelize/lib/dialects/mysql/connection-manager.js

    Place:

    class ConnectionManager extends AbstractConnectionManager {
      constructor(dialect, sequelize) {
        super(dialect, sequelize);
    
        this.sequelize = sequelize;
        this.sequelize.config.port = this.sequelize.config.port || 3306;
        try {
          if (sequelize.config.dialectModulePath) {
            this.lib = require(sequelize.config.dialectModulePath);
          } else {
            this.lib = require('mysql2');
          }
        } catch (err) {
    // HERE!!! ---^
          if (err.code === 'MODULE_NOT_FOUND') {
            throw new Error('Please install mysql2 package manually');  // <--- It is not real error that you get!
          }
          throw err;
        }
    
        this.refreshTypeParser(DataTypes);
      }

    After that you will be able to understand what is actually happened.

  5. pasha1307 commented on Dec 13, 2018

    @pasha1307

    yes, it does make sense - yet, the error should not be specified then, if it may or may not be related to the sql package. Adds more confusion to the equasion. thank you.

  6. catamphetamine commented on Apr 24, 2019

    @catamphetamine
    Contributor

    If you're bundling with Webpack, my workaround for v5 is:

    // Fix "Please install mysql2 package manually".
    import mysql2 from 'mysql2';
    if (options.dialect === 'mysql') {
      options.dialectModule = mysql2;
    }
    new Sequelize(options)

    The bug is in the code:

      _loadDialectModule(moduleName) {
        try {
          if (this.sequelize.config.dialectModulePath) {
            return require(this.sequelize.config.dialectModulePath);
          }
          if (this.sequelize.config.dialectModule) {
            return this.sequelize.config.dialectModule;
          }
          return require(moduleName);
    
        } catch (err) {
          if (err.code === 'MODULE_NOT_FOUND') {
            if (this.sequelize.config.dialectModulePath) {
              throw new Error(`Unable to find dialect at ${this.sequelize.config.dialectModulePath}`);
            }
            throw new Error(`Please install ${moduleName} package manually`);
          }
    
          throw err;
        }
      }

    require(moduleName) won't work with Webpack.

  7. arielrahmane commented on May 5, 2019

    @arielrahmane

    @catamphetamine I'm sorry if it is a rookie question, but in which file should I place that correction?

    // Fix "Please install mysql2 package manually".
    import mysql2 from 'mysql2';
    if (options.dialect === 'mysql') {
      options.dialectModule = mysql2;
    }
    new Sequelize(options)
  8. MaximusMcCann commented on May 17, 2019

    @MaximusMcCann

    @arielrahmane wherever you import sequelize you can set the dialectModule in the options. Example below.

    import Sequelize from 'sequelize';
    import mysql2 from 'mysql2'; // Needed to fix sequelize issues with WebPack
    
    const sequelize = new Sequelize(
      process.env.DB_NAME,
      process.env.DB_USER,
      process.env.DB_PASSWORD,
      {
        dialect: 'mysql',
        dialectModule: mysql2, // Needed to fix sequelize issues with WebPack
        host: process.env.DB_HOST,
        port: process.env.DB_PORT
      }
    )
    
    export async function connectToDatabase() {
      console.log('Trying to connect via sequelize')
      await sequelize.sync()
      await sequelize.authenticate()
      console.log('=> Created a new connection.')
    
      // Do something 
    }
    
    
  9. nahumzs commented on Jul 1, 2019

    @nahumzs

    The previous solution works correctly from MySql, but for Postgres is not working. I was able to go passed the Please install pg package manually error, and got stuck with an error complying about undefined of indexOf.

    Because I'm trying to use sequelize and serverless for a prototype, I changed my db selection to mysql, and simply move forward.

  10. CovertIII commented on Sep 19, 2019

    @CovertIII

    I got this working with Postgres. As noted above use the dialectModule option when initializing Sequelize v5 like so:

    const Sequelize = require('sequelize');
    const pg = require('pg');
    
    const sequelize = new Sequelize(process.env.DATABASE_URL, {
      dialectModule: pg
    });
    
    module.exports = sequelize;
    

    To get past the undefined of indexOf error add this to the webpack config:

      plugins: [new webpack.IgnorePlugin(/\.\/native/, /\/pg\//)]
    

    See these related issues:
    brianc/node-postgres#1187
    serverless-heaven/serverless-webpack#78

  11. benbowler commented on Oct 31, 2019

    @benbowler

    I had this issue deploying to now.sh and was able to fix to add this dialectModule option directly in my Sequalize config:

    const config = {
      username: process.env.DATABASE_USERNAME,
      password: process.env.DATABASE_PASSWORD,
      database: process.env.DATABASE_NAME,
      host: process.env.DATABASE_HOST,
      dialect: "mysql",
      dialectModule: "mysql2"
    };
    
  12. valentinho27 commented on Jul 22, 2020

    @valentinho27

    in my case i use it !

    add

    const mysql2 = require('mysql2');

    "development": {
    "username": DB_USERNAME,
    "password": DB_PASS,
    "database": DB_NAME,
    "host" : DB_HOST,
    "dialect" : "mysql",
    "dialectModule" : mysql2 // Needed to fix sequelize issues

    }

  13. knowself commented on Sep 11, 2020

    @knowself

    I was working through the Apollo GraphQL Tutorial ... https://www.apollographql.com/docs/tutorial/introduction/

    ... and ran into a brick wall until I did this ...

    ==============================
    const {Sequelize} = require('sequelize');
    const {sqlite} = require('sqlite3'); // Needed to fix sequelize issues with WebPack

    module.exports.paginateResults = ({
    .
    .
    .
    module.exports.createStore = () => {
    const db = new Sequelize({
    dialect: 'sqlite',
    dialectModule: sqlite, // Needed to fix sequelize issues with WebPack
    storage: './store.sqlite'
    });
    .
    .
    .

    Dead in the water until this post ... Thanks!!!

  14. added a commit that references this issue on Dec 9, 2020
  15. papb commented on Apr 4, 2021

    @papb
    Member

    Hello, I will concentrate all efforts involving Webpack problems in this new issue: #13169 - Please help me there if possible.

  16. Sky-walker-only commented on Apr 8, 2021

    @Sky-walker-only

    npm install mysql2 --global, npm install mysql2 --save; it's done;

  17. Mnabawy commented on Jun 29, 2021

    @Mnabawy

    npm install --save mysql2

  18. SumitJadiya commented on Jul 7, 2021

    @SumitJadiya

    npm i sequelize
    npm i mysql2

    repeating above queries fixed my problem

  19. added a commit that references this issue on Jun 29, 2023
  20. Kaleb11 commented on Jul 12, 2023

    @Kaleb11

    I had this issue deploying to now.sh and was able to fix to add this dialectModule option directly in my Sequalize config:

    const config = {
      username: process.env.DATABASE_USERNAME,
      password: process.env.DATABASE_PASSWORD,
      database: process.env.DATABASE_NAME,
      host: process.env.DATABASE_HOST,
      dialect: "mysql",
      dialectModule: "mysql2"
    };
    
  21. wildy13 commented on Sep 25, 2023

    @wildy13

    mysql2

    require('dotenv').config();
    
    const Sequelize = require('sequelize');
    const mysql2 = require('mysql2')
    const sequelize = new Sequelize(
        process.env.DB_NAME,
        process.env.DB_USERNAME,
        process.env.DB_PASSWORD,
        {
            host : 'localhost',
            dialect : process.env.DB_DIALECT,
            dialectModule: mysql2,
            dialectOptions: {
                dateStrings: true,
                typeCast: true,
            },
            pool: {
                max: 10,
                min: 0,
                idle: 10000,
            },
              
        }
    )
    
    const config = {
        host : process.env.BACKEND_HOST,
        port : process.env.BACKEND_PORT,
        secret: { session: process.env.SESSION_KEY },
    }
    
    module.exports = { sequelize, config }
    

    but I'm still error,

    Error: Please install mysql2 package manually

  22. moxinnai commented on May 18, 2025

    @moxinnai

    If you're bundling with Webpack, my workaround for v5 is:

    // Fix "Please install mysql2 package manually".
    import mysql2 from 'mysql2';
    if (options.dialect === 'mysql') {
    options.dialectModule = mysql2;
    }
    new Sequelize(options)
    The bug is in the code:

    _loadDialectModule(moduleName) {
    try {
    if (this.sequelize.config.dialectModulePath) {
    return require(this.sequelize.config.dialectModulePath);
    }
    if (this.sequelize.config.dialectModule) {
    return this.sequelize.config.dialectModule;
    }
    return require(moduleName);

    } catch (err) {
      if (err.code === 'MODULE_NOT_FOUND') {
        if (this.sequelize.config.dialectModulePath) {
          throw new Error(`Unable to find dialect at ${this.sequelize.config.dialectModulePath}`);
        }
        throw new Error(`Please install ${moduleName} package manually`);
      }
    
      throw err;
    }
    

    }
    require(moduleName) won't work with Webpack.

    use import * as mysql2 from 'mysql2' instead of import mysql2 from 'mysql2'
    is worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions