Cli: Syntax Error for sequelize db:migrate and sequelize db:create

Created on 29 Mar 2018  路  3Comments  路  Source: sequelize/cli

What you are doing?

# happening for db:create and db:migrate
$ node_modules/.bin/sequelize db:migrate         

Sequelize CLI [Node: 8.9.1, CLI: 4.0.0, ORM: 4.37.4]

ERROR: Error reading "config/database.js". Error: SyntaxError: Unexpected token :

What do you expect to happen?

  • It should create a database if I run sequelize db:create respecting database.js configuration
  • It should migrate a database if I run sequelize db:migrate respecting database.js configuration

What is actually happening?

db:create and db:migrate commands are unable to read config/database.js configurations. It is giving syntax error.

Sequelize CLI [Node: 8.9.1, CLI: 4.0.0, ORM: 4.37.4]


ERROR: Error reading "config/database.js". Error: SyntaxError: Unexpected token :

__Dialect:__ mysql
__Database version:__ mysql Ver 14.14 Distrib 5.7.18, for osx10.12 (x86_64) using EditLine wrapper
__Sequelize CLI version:__ 4.0.0
__Sequelize version:__ 4.37.3

Most helpful comment

__The Problem is in config/database.js file__, as the cli is reporting syntax error, if we are using database.js to load configuration dynamically, we need to export the configurations using module.exports:

Solution:

module.exports = {
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}

All 3 comments

Here is my .sequelizerc file

const path = require('path');

module.exports = {
  'config': path.resolve('config', 'database.js'),
  'models-path': path.resolve('src', 'models'),
  'seeders-path': path.resolve('db', 'seeders'),
  'migrations-path': path.resolve('db', 'migrations')
}

config/database.js

{
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}

__The Problem is in config/database.js file__, as the cli is reporting syntax error, if we are using database.js to load configuration dynamically, we need to export the configurations using module.exports:

Solution:

module.exports = {
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}

Do you guys think you could update your documentation once in a while?

https://sequelize.org/master/manual/migrations.html

Was this page helpful?
0 / 5 - 0 ratings