找不到模块 'sequelize/types'

Cannot find module 'sequelize/types'

谁知道我为什么会收到这个错误 这是我的代码

"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");
  },
};

当我尝试 运行 这个命令时 sequelize db:migrate 它向我显示 ERROR: Cannot find module 'sequelize/types'

我的依赖文件

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

任何解决方案都需要帮助

"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");
  },
};

如果将第二行改为;

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

它应该可以正常工作。

更好的解决方法是像这样安装 Sequelize;

const Sequelize = require("sequelize");

然后像这样使用它;

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

您甚至可能不需要输入,因为 up() 会免费提供给您,您只需将 DataTypes 替换为 Sequelize

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

这个错误,可能会在你使用auto import并且导入sequelize/types时发生,你可以在代码有 'sequelize/types' 和 删除 它在你的代码中应该更改

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