Cannot find module 'sequelize/types'
Asked Answered
C

4

6

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

Cranwell answered 5/3, 2021 at 5:32 Comment(7)
Are you sure you have that module installed? Show me the dependencies part in the package.json file.Yeorgi
{ "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" } @Yeorgi seeCranwell
Are you using typescript?Yeorgi
not using typescript @YeorgiCranwell
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.Yeorgi
i tried that too but no help @Yeorgi but thank youCranwell
This question could have an answer for you: #65205170Yeorgi
P
11
"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");
  },
};
Peisch answered 5/3, 2021 at 17:22 Comment(1)
github.com/nkhs/node-sequelize : there is migration sample filesPeisch
C
4

If you change the second line to;

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

It should work fine.

Cloudless answered 28/7, 2021 at 18:35 Comment(1)
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 thinkUnhealthy
C
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) {
    ...
}
Cloudless answered 22/2, 2022 at 2:10 Comment(0)
S
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');
Stu answered 24/2, 2022 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.