Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize on NodeJS/ExpressJS return an error when running CLI command db:migrate

I'm following this tutorial Using PostgreSQL and Sequelize to persist our data on medium, and right now I'm stuck at the db:migrate. it's returning this error

Sequelize CLI [Node: 12.1.0, CLI: 5.4.0, ORM: 5.8.2]

Loaded configuration file "config.json".
Using environment "development".

ERROR: Error parsing url: undefined

as you can see I'm using NodeJS version 12.1.0 and Sequelize CLI version 5.4.0 and Sequelize version 5.8.2 and all of them were the latest version.

and before running sequelize db:migrate, I'm running this command first SET DATABASE_URL=postgresql://[user[:password]@][netlocation][:port][/dbname] and it does not returns any error.

but it's returning error after I ran db:migrate

I already tried to find the problem, but I can't found the answer yet. Here is my ./Models/Index.js file.

'use strict';

require('dotenv').config();

import { readdirSync } from 'fs';
import { basename as _basename, join } from 'path';
import Sequelize from 'sequelize';
const basename = _basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../../config.json')[env];
const db = {};

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

readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = sequelize['import'](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;

export default db;

if you realize I just changed it to ES6 format which change some codes, but before I change it to ES6, it doesn't work either. and for all the rest of the files I following the tutorial.

Here are the files that I think have a connection:

.env

DATABASE_URL=postgres://postgres:admin@localhost:5432/test_app

.sequelizerc

const path = require('path');

module.exports = {
  "config": path.resolve('./config.json'),
  "models-path": path.resolve('./app/Models'),
  "migrations-path": path.resolve('./migrations')
};

config.json

{
  "development": {
    "use_env_variable": "DATABASE_URL"
  },
  "test": {
    "use_env_variable": "DATABASE_URL"
  },
  "production": {
    "use_env_variable": "DATABASE_URL"
  }
}

If there are some files that I haven't included yet please tell me, and please help me to fix find the solution for this problem. Thank you

OS: Windows 10

like image 637
Khrisna Gunanasurya Avatar asked Jun 28 '26 15:06

Khrisna Gunanasurya


2 Answers

Basically you are unable to set environment variable DATABASE_URL successfully.

I am not a Windows guy, but this should do your job.

If you are using GitBash, then it is as simple as:

export DATABASE_URL=postgres://postgres@localhost:5432/database_name

and after that:

node_modules/.bin/sequelize db:migrate

EDIT:

I am not sure how to set this variable in gitbash and cmd. Here is an alternate.

in config/config.json

"development": {
    "username": "postgres"
    "password": "postgres",
    "database": "your_db_here",
    "host": "127.0.0.1",
    "dialect": "postgres"
},

update these variables according to your postgres db.

and run:

node_modules/.bin/sequelize db:migrate
like image 64
aitchkhan Avatar answered Jul 01 '26 10:07

aitchkhan


  1. You cannot fetch values at runtime inside config.json. It has to be static.
  2. You should either use config.json or env variables or roll your own like mentioned in another answer.

To use env variables, you will eschew config.json. Instead, in models/index.js, set

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

to

sequelize = new Sequelize(process.env.DATABASE_URL)

like image 28
suv Avatar answered Jul 01 '26 11:07

suv