2

I am trying to use import export statements in the sequelize migrations, have tried all the things in the other questions and github issues. Following are the files I am using to make it work.

.sequelizerc

require('@babel/register');
require('@babel/polyfill');
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')
}

package.json

{
  "name": "sideproject3",
  "version": "0.0.0",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "nodemon --exec babel-node --experimental-specifier-resolution=node ./bin/www.js",
    "start": "node --experimental-specifier-resolution=node ./bin/www.js",
    "lint": "eslint --ignore-path .gitignore  \"**/*.js\"",
    "lint-fix": "eslint --ignore-path .gitignore --fix \"**/*.js\""
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "dotenv": "^8.2.0",
    "express": "~4.16.1",
    "express-jwt": "^6.0.0",
    "http-errors": "~1.6.3",
    "jade": "~1.11.0",
    "jsonwebtoken": "^8.5.1",
    "morgan": "~1.9.1",
    "mysql2": "^2.2.5",
    "sequelize": "^6.6.2"
  },
  "devDependencies": {
    "@babel/cli": "^7.13.16",
    "@babel/core": "^7.13.16",
    "@babel/node": "^7.13.13",
    "@babel/polyfill": "^7.12.1",
    "@babel/preset-env": "^7.13.15",
    "@babel/register": "^7.13.16",
    "eslint": "^7.25.0",
    "eslint-config-google": "^0.14.0",
    "nodemon": "^2.0.7",
    "sequelize-cli": "^6.2.0"
  }
}

.babelrc

{
  "presets": [
    [
      "@babel/preset-env"
    ]
  ]
}

migration.js

'use strict';
import { TABLE_NAMES, REQUEST_STATUS } from '../constants';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('Requests', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      message: {
        type: Sequelize.TEXT,
      },
      status: {
        type: Sequelize.ENUM([Object.values(REQUEST_STATUS)]),
      },
      requested_time: {
        type: Sequelize.DATE,
      },
      requested_by_id: {
        type: Sequelize.INTEGER,
        references: {
          model: TABLE_NAMES.USERS,
          key: 'id',
        },
      },
      requested_to_id: {
        type: Sequelize.INTEGER,
        references: {
          model: TABLE_NAMES.USERS,
          key: 'id',
        },
      },
      reject_response: {
        type: Sequelize.TEXT,
      },
      response_time: {
        type: Sequelize.DATE,
        allowNull: true,
      },
      type: {
        type: Sequelize.STRING,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
    });
  },
  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('Requests');
  },
};

Solutions by others tell that using @babel/register and using preset-env should have worked, but it is not working and giving the following error

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:

... require() of ES modules is not supported.

mdanishs
  • 1,996
  • 8
  • 24
  • 50
  • not a babel expert, but shouldn't presets be a simply array, rather than one array nested in another? – Christian Fritz Apr 30 '21 at 03:14
  • @ChristianFritz will not make a difference, tried with that as well same error – mdanishs Apr 30 '21 at 03:17
  • require() of ES modules is not supported. Can you paste the entire error message? Does it say something like "nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules" ? – dee Jul 02 '21 at 07:51
  • Similar question answered here https://stackoverflow.com/a/69801750/1704845 – Aymen Nov 01 '21 at 21:25

0 Answers0