Reputation: 149
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.
Upvotes: 6
Views: 9051
Reputation: 21
I managed to resolve this error by installing the sequelize dependency:
yarn add sequelize
or
npm i sequelize
Upvotes: 2
Reputation: 339
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.
Upvotes: 2
Reputation: 193
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.
Upvotes: 5
Reputation: 69
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
Upvotes: 5
Reputation: 7282
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.
Upvotes: 8