6

I'm creating a Node.js/Express.js app with Sequelize as my ORM. This is the tutorial I'm following. I'm having trouble migrating my tables onto my Azure SQL database.

My config/config.json file is as such:

{
    "development" : {
       "use_env_variable" : "DATABASE_URL",
       "dialect" : "mssql"
    }
}

My .env file is as such:

DATABASE_URL=<connection string here>

These are being called by this code in models/index.js:

'use strict';

var fs        = require('fs');
var path      = require('path');
var Sequelize = require('sequelize');
var basename  = path.basename(module.filename);
var env       = process.env.NODE_ENV || 'development';
var config    = require(__dirname + '/../config/config.json')[env];
var db        = {};

if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

I have require('dotenv').config() in my main file app.js. My understanding was that the dotenv package exports all of my .env variables throughout my project.

When I try running node_modules/.bin/sequelize db:migrate in the terminal, I get

ERROR: Error parsing url: undefined

Why is it coming back as undefined?

Here is my file structure in case it helps.

enter image description here

3
  • Try saying require(path.join(__dirname, '../config/config.json'))? Commented Apr 23, 2018 at 19:12
  • In what file would I add this? Commented Apr 23, 2018 at 19:14
  • where you say “var config =“ Commented Apr 24, 2018 at 19:50

5 Answers 5

8

Where are you requiring your .env file? When you are running migration, this file would not be required and hence the error.

You should add require('dotenv').config(); in your config/config.js as your first line.

Sign up to request clarification or add additional context in comments.

Comments

5

I hope this helps someone, I had this resolved by exporting the DATABASE_URL on the terminal using: export DATABASE_URL=postgresql://[user[:password]@][netlocation][:port][/dbname]

As stated here: https://tutel.me/c/programming/questions/45929489/use_env_variable+returned+undefined+in+sequelize

Comments

5

It seem's that when you are running sequelize through cli it doesn't load your .env config as expected, I have the same issue and it can be resolved by creating a .sequelizerc in your project root folder with this content:

// .sequelizerc

require('dotenv').config(); // <- require this here

// ...Any aditional configuration for sequelize-cli like custom folders for models and migrations
const path = require('path');
module.exports = {
  'config': path.resolve('src', 'config', 'config.js'),
  'models-path': path.resolve('src', 'models'),
  'seeders-path': path.resolve('src', 'seeders'),
  'migrations-path': path.resolve('src', 'migrations')
};

The .sequelizerc file would work as a configuration not just to your sequelize but also to your sequelize-cli.

2 Comments

this solution helped me a lot
i have same issue now, and my sequelizerc already look like that , then how to solve ? after deploying into heroku i got these problem
2

I had this problem too and resolved it. In my case, it is about the .env file. You need to do 2 things. First:

require('dotenv').config();

to use variables in the .env file. Second, in the .env file, you must use the = to separate between key and value, like this:

DATABASE_URL=postgres://postgres:123456@localhost:5432/databasename

It took me half of a day to realize that I used the : in the .env file (not do this):

DATABASE_URL:postgres://postgres:123456@localhost:5432/databasename

EDIT: Just for more detail, you need to put require('dotenv').config(); in the top of .sequelizerc file.

Comments

2

I managed to resolve this error by installing the sequelize dependency:

yarn add sequelize

or

npm i sequelize

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.