1

I'm running npx sequelize-cli db:migrate to create the table in the database and returns the following message:

Sequelize CLI [Node: 18.9.1, CLI: 6.5.1, ORM: 6.20.1]


ERROR: Cannot find "/src/db/migrations/config/config.json". Have you run "sequelize init"?

ERROR: Cannot read properties of undefined (reading 'detail')
sequelize-cli db:migrate

Run pending migrations

But I have already run the command sequelize-init...

this is my config.js file

import dotenv from "dotenv";
dotenv.config();

export default {
  development: {
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    dialect: process.env.DB_DIALECT,
    logging: true,
  },
  test: {
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOST,
    dialect: process.env.DB_DIALECT,
  },
  production: {
    username: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOST,
    dialect: process.env.DB_DIALECT,
  },
};

My migration file:

"use strict";

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable("Users", {
      id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      name: {
        type: Sequelize.STRING(50),
        allowNull: false,
      },
      birth_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
      },
      email: {
        type: Sequelize.STRING(62),
        allowNull: false,
      },
      phone: {
        type: Sequelize.STRING(20),
        allowNull: false,
      },
      address: {
        type: Sequelize.STRING(100),
        allowNull: false,
      },
      password: {
        type: Sequelize.STRING(100),
        allowNull: false,
      },
      position_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: "Position",
          key: "id",
        },
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
      },
      is_admin: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable("Users");
  },
};

my .sequelizerc file:

import path from "path";

export default {
  config: path.resolve("config", "config.js"),
};

and my package.json file:

{
  "name": "startbootstrap-sb-admin-2-gh-pages",
  "version": "1.0.0",
  "description": "",
  "main": "gulpfile.js",
  "scripts": {
    "test": "test",
    "dev": "nodemon src/server.js"
  },
  "type": "module",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "connect-flash": "^0.1.1",
    "cookie-parser": "^1.4.6",
    "dotenv": "^16.0.3",
    "ejs": "^3.1.8",
    "express": "^4.18.1",
    "express-ejs-layouts": "^2.5.1",
    "express-session": "^1.17.3",
    "http": "^0.0.1-security",
    "http-error": "^0.0.6",
    "morgan": "^1.10.0",
    "mysql2": "^2.3.3",
    "nodemon": "^2.0.16",
    "path": "^0.12.7",
    "sequelize": "^6.20.1"
  },
  "devDependencies": {
    "sequelize-cli": "^6.5.1"
  }
}

I would like to perform the migration but I couldn't succeed, even if I changed the config file to json, and the error still persisted...

dev user
  • 41
  • 1
  • 8
  • if you are using typescript and get this exact error, all your ts files need to use require module.exports instead of import export, learned this the hard way of burning 24 hrs – PirateApp Oct 31 '22 at 03:54
  • 1
    im using nodejs vanilla not Typescript – dev user Nov 04 '22 at 23:00

4 Answers4

1

In your models/index.js file, you need to update config.json to config.js

Elco
  • 97
  • 1
  • 7
  • This question has an accepted answer. Please add some details about the reason you are adding a new answer – MD Zand Nov 29 '22 at 20:22
  • 1
    His error: "ERROR: Cannot find "/src/db/migrations/config/config.json". Have you run "sequelize init"?" Then he proceeds to say: "this is my config.js file" Which means he changed config,json to config.js We can also conclude this from the fact that he used env vars in config (which do not work in json files, only js files) But he forgot one thing. When you do that, in your models/index.js file, you need to change config.json to config.js – Elco Nov 30 '22 at 08:38
0

In the config/config.js,

  • did you try to use cont dotenv = require('dotenv'); instead of import dotenv from "dotenv"; ?

  • replacing export default by module.exports = ?

  • Yes, i only can use the sequelize with require/module.exports, do you know if Sequelize don't support ES6 imports? Because of this errors i was thinking about it... – dev user Oct 11 '22 at 14:42
  • As for me, the ES6 imports were the issue as it was not supported by sequelize. Maybe verifying the other files using sequelize would unblock you. – Sébastien NOBOUR Oct 12 '22 at 15:53
0

run the following command would do the trick

npx sequelize-cli db:migrate --debug

for me, i missed a , in my migration file

m_____ilk
  • 143
  • 1
  • 15
0

Make sure you are running npx sequelize-cli db:migrate inside the correct folder where ".sequelizerc" is located

It worked for me, I was trying to run the command in a previous folder, that's why it was giving an error