Sequelize - 运行 使用 es6 和模块进行迁移

Sequelize - run migration with es6 and modules

我不确定是我做错了什么。我觉得我 运行 正在使用一个现代的、相当普通的堆栈。但是我无法让新的 Sequelize v6 与我的设置很好地配合使用。我在 Node v14.17、Sequelize v6.6.2 上,在我的 package.json 中有 "type": "module"。我终于想出了如何通过大量谷歌搜索和修补来自动导入我的模型。所以现在我正在尝试使用迁移工具向模型添加一个字段。我在迁移文件夹中创建了迁移文件。
看起来像这样:

'use strict';

module.exports = {
    up: async (queryInterface, Sequelize) => {
        /**
         * Add altering commands here.
         *
         * Example:
         * await queryInterface.createTable('users', { id: Sequelize.INTEGER });
         */
        return Promise.all([
            queryInterface.addColumn(
                'Customers', // table name
                'include_core_items', // new field name
                {
                    type: Sequelize.Boolean,
                    allowNull: false,
                    defaultValue: true,
                    after: 'customer_name',
                }
            ),
        ]);
    },

    down: async (queryInterface, Sequelize) => {
        /**
         * Add reverting commands here.
         *
         * Example:
         * await queryInterface.dropTable('users');
         */
        return Promise.all([
            queryInterface.removeColumn('Customers', 'include_core_items'),
        ]);
    },
};

然后我尝试 运行 迁移:npx sequelize-cli db:migrate 并且出现以下错误: ERROR: Error reading "config\config.js". Error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\...\config\config.js require() of ES modules is not supported. require() of C:\...\config\config.js from C:\Users\...\AppData\Roaming\npm-cache\_npx576\node_modules\sequelize-cli\lib\helpers\config-helper.js is an ES module file as it is a .js file whose nearest pare nt package.json contains "type": "module" which defines all .js files in that package scope as ES modules. Instead rename config.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\...\package.json.

我尝试将配置重命名为 .cjs,然后收到错误:ERROR: Dialect needs to be explicitly supplied as of v4.0.0 所以我认为它没有正确读取 ENV 变量或其他内容。

config.[c]js

// seed command: sequelize seed:create --name PermissionData
// import dotenv from 'dotenv';
// dotenv.config();

import 'dotenv/config.js';

const username = process.env.NAME;
const password = process.env.PASSWORD;
const database = process.env.DATABASE;
const host = process.env.HOST;
const port = process.env.DB_PORT;
const dialect = process.env.DIALECT;
const node_env = process.env.NODE_ENV;
const session_secret = process.env.SESSION_SECRET;
const base_url = process.env.BASE_URL;
const client_url = process.env.CLIENT_APP_LOC;
const secure_cookie = process.env.SECURE_COOKIE;

const config = {
    dev: {
        username,
        password,
        database,
        host,
        port,
        dialect,
        logging: true,
        session_secret,
        base_url,
        client_url,
        secure_cookie,
    },
    testing: {
        username,
        password,
        database,
        host,
        port,
        dialect,
        logging: true,
        session_secret,
        base_url,
        client_url,
        secure_cookie,
    },
    production: {
        username,
        password,
        database,
        host,
        port,
        dialect,
        logging: true,
        session_secret,
        base_url,
        client_url,
        secure_cookie,
    },
};

export default config[node_env];

我最近 运行 在尝试 运行 迁移时遇到了同样的问题。我使用 the docs.

的帮助解决了它

Note: I'm using Node v15.0.0

如果您使用的是 babel,请安装 babel-register 包

npm i --save-dev babel-register

编辑您的 .sequelizerc 文件以包含包导入

require("babel-register");

const path = require('path');

module.exports = {
  'config': path.resolve('config', 'config.json'),
  'models-path': path.resolve('models'),
  'seeders-path': path.resolve('seeders'),
  'migrations-path': path.resolve('migrations')
}

在你的 .sequelizercconfig.js 中删除 es6 的痕迹,而是使用通用的 js (require) 进行导入和导出。