Sequelize CLI 找不到环境变量
Sequelize CLI Not Finding Env Variables
我正在尝试 运行 使用 Sequelize CLI 工具进行数据库迁移,但我 运行 遇到了一个问题,我的 ENV 变量没有被该工具处理。在 github 回购中,它说在 2.0.0 版中(我在 2.4.0 上)你可以像这样直接访问 config/config.js
中的 ENV 变量,process.env.DB_HOSTNAME,但我得到一个表示没有从变量传入任何值的错误
错误:
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"
您忘记要求 dotenv
模块:
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 工具进行数据库迁移,但我 运行 遇到了一个问题,我的 ENV 变量没有被该工具处理。在 github 回购中,它说在 2.0.0 版中(我在 2.4.0 上)你可以像这样直接访问 config/config.js
中的 ENV 变量,process.env.DB_HOSTNAME,但我得到一个表示没有从变量传入任何值的错误
错误:
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"
您忘记要求 dotenv
模块:
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"
},
}