5

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

Aakash
  • 139
  • 1
  • 3
  • 22
  • Are you sure you have that module installed? Show me the dependencies part in the package.json file. – m_hm0ud Mar 05 '21 at 05:41
  • `{ "name": "sqlSequelize", "version": "1.0.0", "description": "", "main": "app.js", "dependencies": { "express": "^4.17.1", "mysql2": "^2.2.5", "sequelize": "^6.5.0" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" } ` @m_hm0ud see – Aakash Mar 05 '21 at 05:45
  • Are you using typescript? – m_hm0ud Mar 05 '21 at 05:49
  • not using typescript @m_hm0ud – Aakash Mar 05 '21 at 05:54
  • Well I searched on npm website and there's no such package called sequelize/types. There's @types/sequelize which is for making sequelize compatible with typescript. – m_hm0ud Mar 05 '21 at 05:57
  • i tried that too but no help @m_hm0ud but thank you – Aakash Mar 05 '21 at 05:58
  • 1
    This question could have an answer for you: https://stackoverflow.com/questions/65204670/error-cannot-find-module-sequelize-types?rq=1 – m_hm0ud Mar 05 '21 at 06:01

4 Answers4

10
"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");
  },
};
Wang Liang
  • 4,244
  • 6
  • 22
  • 45
4

If you change the second line to;

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

It should work fine.

  • The better way is to replace the `Sequelize` parameter in the async callback to `DataTypes`, Importing `DataTypes` and presence of `Sequelize` can lead to certain conflicts, atleast that's what I think – Prhyme Sep 10 '21 at 22:26
0

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) {
    ...
}
0

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');
Otis
  • 156
  • 2
  • 11