Sequelize CLI 找不到环境变量 [英] Sequelize CLI Not Finding Env Variables

查看:20
本文介绍了Sequelize CLI 找不到环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Sequelize CLI 工具运行数据库迁移,但我遇到了一个问题,即我的 ENV 变量没有被该工具处理.在 github 存储库中,它说在 2.0.0 版本(我在 2.4.0)中,您可以像这样直接访问 config/config.js 中的 ENV 变量,例如 process.env.DB_HOSTNAME,但是我收到一个错误,表明没有从变量中传递任何值

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

错误:

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"

推荐答案

你忘了 require dotenv 模块:

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"
},
}

这篇关于Sequelize CLI 找不到环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆