https://github.com/sequelize/cli/blob/master/lib/helpers/generic-helper.js#L28
Also, this argument isn't documented (as far as I could tell after reading the readme).
Documentation might be enough, but I did find it odd that it internally switches the environment, but doesn't set NODE_ENV which dependent scripts like config.js might be expecting to happen.
My example config.js file that this doesn't work with:
"use strict";
const config = require('./index').dbConfig
let configEnv = {}
configEnv[process.env.NODE_ENV || "development"] = config;
module.exports = configEnv
"use strict";
process.env.NODE_ENV = process.env.NODE_ENV || "development"
console.log(`Company Config : Using Environment "${process.env.NODE_ENV}"`)
require('dotenv').config() //load .env first if it exists
require('dotenv').config({path: './config/.env.' + process.env.NODE_ENV}) //then load env file if env values are missing or a .env file doesn't exist
module.exports = {
authenticationServer: process.env.AUTHENTICATION_SERVER,
zipkinServiceName : process.env.ZIPKIN_SERVICE_NAME,
zipkinLoggerEndpoint : process.env.ZIPKIN_LOGGER_ENDPOINT,
dbConfig:{
username: process.env.DB_USER_NAME,
password:process.env.DB_PASSWORD || null,
database:process.env.DB,
host: process.env.DB_HOST,
dialect: process.env.DB_DIALECT,
logging: process.env.DB_QUERY_LOGGING ? undefined : false
}
}
using sequelize version --env=test
outputs
Company Config : Using Environment "development"
Sequelize [Node: 6.1.0, CLI: 2.5.1, ORM: 3.24.3]
Company Config : Using Environment "development"
Loaded configuration file "config/config.js".
possibly also be able to set NODE_ENV from the config so that it can use the .env file? took me a bit to realize why it wasnt working.. (mainly because i had messed with that variable on my system so it wasn't defaulting to dev as i expected, so usually not an issue, but still)
or support dotenv natively?
For anyone using dotenv who arrived here searching for a solution to your config.js/database.js file not loading your env variables, you probably just need to require dotenv:
require('dotenv').config(); // magic
module.exports = {
"development" : {
"database" : process.env.POSTGRES_DATABASE,
"username" : process.env.POSTGRES_USERNAME,
"password" : process.env.POSTGRES_PASSWORD,
"host" : process.env.POSTGRES_HOST,
"dialect" : "postgres"
},
"test" : {
"database" : process.env.POSTGRES_DATABASE,
"username" : process.env.POSTGRES_USERNAME,
"password" : process.env.POSTGRES_PASSWORD,
"host" : process.env.POSTGRES_HOST,
"dialect" : "postgres"
},
"production" : {
"database" : process.env.POSTGRES_DATABASE,
"username" : process.env.POSTGRES_USERNAME,
"password" : process.env.POSTGRES_PASSWORD,
"host" : process.env.POSTGRES_HOST,
"dialect" : "postgres"
}
}
It took me quite some time to find out how to set a different environment other than the default development.
The documentation does not explain how to set that. I had to look into the code to find out that you can set NODE_ENV to the one you want.
I also saw that there is the possibility of using an option named env for the commands, but when I tried it didn't work. Has anyone had success on using it?
I have a local environment set in the config file, and tried sequelize db:create --env local, but it didn't work
@notrev I spend another week trying find how this works...I am glad that I found your post!
Would be nice to put this --env arg/param to docs ;)
You can find the --env mentioned in the help output too. I think _someone_ thinks it is obvious to check there vs put it in documentation.
C:\Users\cmeza\dev\specialproject (release/2.15) ([email protected])
位 sequelize --help db:migrate
Sequelize CLI [Node: 8.1.4, CLI: 3.0.0, ORM: 3.30.0]
Options:
--version Show version number [boolean]
--help Show help [boolean]
--env The environment to run the command in [string] [default: "development"]
--config The path to the config file [string]
--options-path The path to a JSON file with additional options [string]
--migrations-path The path to the migrations folder [string] [default: "migrations"]
--seeders-path The path to the seeders folder [string] [default: "seeders"]
--models-path The path to the models folder [string] [default: "models"]
--url The database connection string to use. Alternative to using --config files [string]
--debug When available show various debug information [boolean] [default: false]
The trouble I had with this is that the --env argument picks the correct object from inside the config object, so it can connect to the db, but if the NODE_ENV isn't also set, the index.js will set its own env to 'development', wrecking the new Sequelize() object. I did some digging into the process when you run "sequelize" using the CLI, & the --env argument appears at process.argv[4]. So in index.js I replaced
var env = process.env.NODE_ENV || 'development';
with
const env = process.env.NODE_ENV ? process.env.NODE_ENV : process.argv[4];
This also works if you don't pass the --env argument. The default process.argv[4] on the command line is "development".
Also, it allows you define your own environment names, eg, sandbox, staging, etc.
Using Sequelize "4.42.0" with Sequelize CLI "5.4.0"
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
For anyone using
dotenvwho arrived here searching for a solution to your config.js/database.js file not loading your env variables, you probably just need to require dotenv: