12

Node v14.3我正在尝试使用and设置项目sequelize

我不想使用babel-register. 取而代之的是,我设置"type": "module"package.json使用所有 ES6 - ES11 开箱即用的功能。

我还想sequelize-cli用于设置和应用迁移。除以下命令外,一切正常:

& sequelize db:migrate

Sequelize CLI [Node: 14.3.0, CLI: 5.5.1, ORM: 5.21.11]

Loaded configuration file "config/config.json".
Using environment "development".
== 20200530214311-create-user: migrating =======

ERROR: Must use import to load ES Module: /home/kasheftin/work/tests/chai-http-publication/migrations/20200530214311-create-user.js
require() of ES modules is not supported.
require() of /home/kasheftin/work/tests/chai-http-publication/migrations/20200530214311-create-user.js from /home/kasheftin/.nvm/versions/node/v14.3.0/lib/node_modules/sequelize-cli/node_modules/umzug/lib/migration.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename 20200530214311-create-user.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/kasheftin/work/tests/chai-http-publication/package.json.

我们看到引擎盖下sequelize-cli使用require(). ES 模块不允许这样做。它提出了 3 种方法来解决这个问题:

  • 将 20200530214311-create-user.js 重命名为以 .cjs 结尾 - 无法完成,sequelize-cli 找不到以 .cjs 结尾的迁移。

  • 将需要的代码更改为使用 import() - 我不想接触 sequelize-cli 代码。

  • 删除“类型”:“模块” - 我不能因为一切都停止工作。

有没有其他方法可以sequelize-cli工作?我正在大量使用测试,我希望在运行测试之前自动准备测试数据库。

4

3 回答 3

2

一种解决方案是package.json在您的迁移文件夹中添加一个以覆盖type: "module"您的主 package.json

这个 package.json 看起来像这样:

{
  "type": "commonjs"
}

并且您的迁移文件必须如下所示:

module.exports = {
  up: async (queryInterface, Sequelize) => {

  },

  down: async (queryInterface, Sequelize) => {

  }
};

它适用于 nodeJS 14.16.1& Sequelize 6.6.2& sequelize-cli6.2.0

于 2021-05-05T09:39:07.743 回答
0

我通过创建一个.sequelizerc文件让 sequelize 知道我正在覆盖默认路径来实现这一点:

// .sequelizerc
const path = require('path');

const db_path = './';

module.exports = {
  'config': path.resolve(db_path, 'config/config.cjs'),
  'models-path': path.resolve(db_path, 'models'),
  'seeders-path': path.resolve(db_path, 'seeders'),
  'migrations-path': path.resolve(db_path, 'migrations')
};

请注意,我正在使用config.cjs文件。然后只需创建一个迁移,但也将扩展名更改为 .cjs :

// 20211206164144-create_subjects_table.cjs
const tableName = 'subjects';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable(tableName, {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        allowNull: false,
        primaryKey: true,
      },
      title: {
        type: Sequelize.STRING,
        required: true,
      },
      created_at: {
        type: Sequelize.DATE,
      },
      updated_at: {
        type: Sequelize.DATE,
      },
      deleted_at: {
        type: Sequelize.DATE,
      },
    });
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('subjects');
  },
};

这适用于 Node.js 版本 v16.13.1 和以下版本package.json

{
  "type": "module",
  "dependencies": {
    "pg": "^8.7.1",
    "pg-hstore": "^2.3.4",
    "sequelize": "^6.12.0-beta.1"
  },
  "devDependencies": {
    "sequelize-cli": "^6.3.0"
  }
}
于 2021-12-06T17:14:02.220 回答
0

几个月来我一直有同样的问题。在使用 ES 模块导入和 Sequelize cli 时,Babel 寄存器将不起作用。

这就是我最终让一切正常工作的方式。我不得不将src复制到dist目录。然后覆盖json包中的类型。希望这会有所帮助

.sequelizerc

require('@babel/register')({
  presets: [
    ['@babel/preset-env', { targets: { node: 'current' } }]
  ]
});
const path = require('path');

const DB_PATH = 'dist/db'; // use dist instead of src directory

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

包.json

{
  "type": "module",
  "engines": {
    "node": ">=14.18",
    "yarn": ">=1.22"
  },
  "scripts": {
    "start": "node --experimental-specifier-resolution=node src",
    "db": "sequelize db:migrate && sequelize db:seed:all",
    "predb": "yarn dist",
    "dist": "cp -r src dist",
    "predist": "rm -rf dist",
    "postdist": "node setup-dist.js"
  },
  "dependencies": {
    "@babel/core": "^7.16.0",
    "@babel/preset-env": "^7.16.0",
    "@babel/register": "^7.16.0",
    "sequelize": "^6.8.0",
    "sequelize-cli": "^6.2.0"
  }
}

设置-dist.js

import { writeFileSync } from 'fs';

const file = './dist/package.json';
const data = '{ "type": "" }'; // to override ESM in main package.json

writeFileSync(file, data);

参考:https ://gist.github.com/elawad/c0af86ea37629ad0538c2b974b4ea0c1

于 2021-11-01T19:17:25.010 回答