Search code examples
sequelize.jssequelize-clidotenv

Sequelize migration doesnot read dotenv variable if I don't run it from root directory. why?


This is what I did

  1. require('dotenv').config() in the config file
  2. set .sequelizerc in the root directory like below
  3. set .sequelie file to point config, migrations, models, seeds directory from root directory
  4. ran npx seuqlie-cli db:migrate form root directory. It work!
  5. ran npx seuqlie-cli db:migrate form sequelize directory. It doesn't read dotenv variable and come with connect ECONNREFUSED 127.0.0.1:3306

and I want to know what is differences between 4 and 5..?

this is my directory looks like enter image description here

my .sequlizerc file

const path = require('path')

module.exports={
    config: path.resolve('src/sequelize/config','config.js'),
    'migrations-path': path.resolve('src/sequelize/migrations'),
    'seeders-path': path.resolve('src/sequelize/seeders'),
    'models-path': path.resolve('src/sequelize/models')
}

and my config file

require("dotenv").config();

module.exports = {
  development: {
    username: process.env.DEV_DATABASE_USER_NAME,
    password: process.env.DEV_DATABASE_PASSWORD,
    database: process.env.DEV_DATABASE_NAME,
    host: process.env.DEV_DATABASE_HOST,
    dialect: "mysql",
    charset: "utf8",
    collate: "utf8_general_ci",
    operatorsAliases: false,
    define: {
      underscored: true
    }
  },
...
}

Solution

  • You could always use the path property for the dotenv so it is not restricted to the folder that has been called.

    dotenv.config({ path: `${process.cwd()}/.env`})