Sequelize CLI Not Finding Env Variables
Asked Answered
P

1

5

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"
Palaeogene answered 25/11, 2016 at 15:57 Comment(2)
Are you using dotenv(github.com/motdotla/dotenv) node module? Are you able to connect db through username root and password test?Bonacci
how did you solve the problem?Fredfreda
F
17

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"
},
}
Fredfreda answered 26/6, 2017 at 4:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.