question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sequelize-cli db:migrate always using development environment

See original GitHub issue

I am using dotenv package. I reuire the package on top. I also tried changing “development” to “test” or “production”, but still It’s always using “development”.

'use strict';
require('dotenv').config({path:'../../.env'})
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development'; // I tried changing 'development' to 'production' and 'test'
const config = require(__dirname + '/../config/config.json')[env];
const db = {};
let sequelize;
console.log(env)
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  console.log(config)
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = sequelize['import'](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

Here is my .env file

//set environment for different configurations 
NODE_ENV=test

//define port
PORT=4000

NODE_CONFIG_DIR =./src/config

When I run the server, then It uses test environment.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10

github_iconTop GitHub Comments

29reactions
savokisscommented, Oct 21, 2019

Oops, I just use this npx sequelize db:migrate --env testing

5reactions
JefferyHuscommented, Dec 12, 2019

You can just do this all time --env production, but I don’t like it this way. It would make sense to just let things works as it should and use my .env file instead

Read more comments on GitHub >

github_iconTop Results From Across the Web

sequalize migration with dotenv - Stack Overflow
You can run migrations command by specifying the environment. sequelize db:migrate --env production. Share.
Read more >
Migrations | Sequelize
With migrations you can transfer your existing database into another state and vice versa: Those ... npm install --save-dev sequelize-cli.
Read more >
How I Setup Database Migration using Sequelize in ... - tekloon
A Node Project with sequelize-cli dependencies; models folder which contain all the model to the table. We shall always have the latest ...
Read more >
5. Database Migrations with Sequelize - Why and How
Learn what migrations are and why you should always be using them in your web ... way to create and utilize migrations with...
Read more >
Configuration - db-migrate - Read the Docs
For example, you might have a dev, test, and prod environment where you need ... If you use the dotenv package to manage...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found