Aakash
Aakash

Reputation: 167

Cannot find module 'sequelize/types'

anyone knows why i am getting this error this is my code

"use strict";
const { DataTypes } = require("sequelize/types");

module.exports = {
  up: async (queryInterface, DataTypes) => {
    await queryInterface.createTable("dummytables", {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: DataTypes.INTEGER,
      },
      id: {
        type: DataTypes.NUMBER,
      },
      first_name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      last_name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
    });
  },
  down: async (queryInterface, DataTypes) => {
    await queryInterface.dropTable("dummytables");
  },
};

when am trying to run this command sequelize db:migrate and its showing me ERROR: Cannot find module 'sequelize/types'

my dependencies file

  "dependencies": {
"@types/sequelize": "^4.28.9",
    "express": "^4.17.1",
    "mysql2": "^2.2.5",
    "sequelize": "^6.5.0",
    "sequelize-cli": "^6.2.0"  }

any solution need help

Upvotes: 6

Views: 16858

Answers (4)

phankieuphu
phankieuphu

Reputation: 196

This error, may be can happen when you use auto import and it import sequelize/types, you can find in code has 'sequelize/types' and delete it in code of you should change

const {DataTypes} = require('sequelize');

Upvotes: 0

Bin Emmanuel
Bin Emmanuel

Reputation: 83

A better fix would be to just install Sequelize like;

const Sequelize = require("sequelize");

then use it like;

first_name: {
    type: Sequelize.STRING,
    allowNull: false,
},

You might not even need to do the importation because up() would give it to you for free, You just need to replace DataTypes with Sequelize.

async up(queryInterface, Sequelize) {
    ...
}

Upvotes: 0

Bin Emmanuel
Bin Emmanuel

Reputation: 83

If you change the second line to;

const { DataTypes } = require("sequelize");

It should work fine.

Upvotes: 4

Wang Liang
Wang Liang

Reputation: 4434

"use strict";
//const { DataTypes } = require("sequelize/types"); // Remove this line

module.exports = {
  up: async (queryInterface, DataTypes) => {
    await queryInterface.createTable("dummytables", {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: DataTypes.INTEGER,
      },
      id: {
        type: DataTypes.NUMBER,
      },
      first_name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      last_name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
    });
  },
  down: async (queryInterface, DataTypes) => {
    await queryInterface.dropTable("dummytables");
  },
};

Upvotes: 11

Related Questions