ESM & sequelize db:migrate

See original GitHub issue

It would be great if sequelize-cli supports migrations in ESM way.

What you are doing?

// db/migrations/20220924162319-create-test.js
export async function up(queryInterface, Sequelize) {
  await queryInterface.createTable('test', {
    id: {
      type: Sequelize.DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
    },
    createdAt: {
      allowNull: false,
      type: Sequelize.DataTypes.DATE,
    },
    updatedAt: {
      allowNull: false,
      type: Sequelize.DataTypes.DATE,
    }
  });
}
export function down(queryInterface, Sequelize) {
  return queryInterface.dropTable('test');
}
npx sequelize db:migrate

What do you expect to happen?

Migration works.

What is actually happening?

npx sequelize db:migrate

Sequelize CLI [Node: 16.16.0, CLI: 6.4.1, ORM: 6.23.0]

Loaded configuration file "db/config.js".
== 20220924162319-create-test: migrating =======

ERROR: require() of ES Module /test/db/migrations/20220924162319-create-test.js from /test/node_modules/umzug/lib/migration.js not supported.
Instead change the require of 20220924162319-create-test.js in /test/node_modules/umzug/lib/migration.js to a dynamic import() which is available in all CommonJS modules.

Output, either JSON or SQL

Dialect: postgres Database version: 12 Sequelize CLI version: 6.4.1 Sequelize version: 6.23.0

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
WikiRikcommented, Sep 21, 2022

sequelize-cli uses pretty old 2.3.0 umzug module:

https://github.com/sequelize/umzug/blob/v2.3.0/src/migration.js#L68

Indeed. We will update to the latest umzug release in v7 of the CLI.

1reaction
1valdiscommented, Nov 25, 2022

As a temporary workaround to not have to rename generated migration files to .cjs, in your migrations folder, create a package.json file with contents

{
  "type": "commonjs"
}

or, since commonjs is the default, simply

{}
image This tricks Node into thinking that your migration folder is a commonjs package, and allows you to use `npx sequelize migration:create` and `npx sequelize db:migrate` the same way as you'd use it in a commonjs project.
Read more comments on GitHub >

github_iconTop Results From Across the Web

sequelize-cli-esm/README.md at master - GitHub
The Sequelize Command Line Interface (CLI) with support for EcmaScript Modules (ESM) ... as config file and db/migrate as the directory for migrations....
Read more >
Using Sequelize CLI with the ESM package - Stack Overflow
Is there a way to execute Sequelize migrations with the ESM package? It requires the -r esm command line argument to be passed, ......
Read more >
Migrations | Sequelize
With migrations you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration ...
Read more >
Can someone kindly tell me why sequelize gives this error ...
... me why sequelize gives this error while running seeders and migrations ... Ideally, you should just be using ESM import/export syntax, ...
Read more >
Using Sequelize CLI with the ESM package-sequelize.js
{ "scripts": { "migrate": "node --require esm ./node_modules/.bin/sequelize-cli db:migrate" } }. then if you run npm run migrate it should work with es6 ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found