Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize CLI Not Finding Env Variables

I am trying to run a db migration with the Sequelize CLI tool, but I'm running into an issue where my ENV variables are not being processed by the tool. In the github repo, it says in version 2.0.0 (I'm on 2.4.0) you can directly access ENV variables in config/config.js like so, process.env.DB_HOSTNAME, but I get an error that indicates that no values are being passed in from the variables

Error:

Unable to connect to database: SequelizeAccessDeniedError: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)

config.js:

module.exports = {
    "development": {
        "username": process.env.LOCAL_USERNAME,
        "password": process.env.LOCAL_PASSWORD,
        "database": process.env.LOCAL_DATABASE,
        "host": "127.0.0.1",
        "dialect": "mysql",
        "migrationStorageTableName": "sequelize_meta"
    },
}

.env:

LOCAL_DATABASE="db_name"
LOCAL_USERNAME="root"
LOCAL_PASSWORD="test"
like image 854
cphill Avatar asked Nov 25 '16 15:11

cphill


1 Answers

you forgot to require dotenv module:

require('dotenv').config();  // this line is important!
module.exports = {
"development": {
    "username": process.env.LOCAL_USERNAME,
    "password": process.env.LOCAL_PASSWORD,
    "database": process.env.LOCAL_DATABASE,
    "host": "127.0.0.1",
    "dialect": "mysql",
    "migrationStorageTableName": "sequelize_meta"
},
}
like image 53
Ali Sherafat Avatar answered Sep 28 '22 06:09

Ali Sherafat