10

I'm not sure if I'm doing something wrong or what. I feel like I'm running a modern, fairly common stack. But I cannot get the new Sequelize v6 to work nicely with my setup. I am on Node v14.17, Sequelize v6.6.2 and in my package.json I have "type": "module". I finally figured out how to get my models automatically imported with a lot of googling and tinkering. So now I am trying to add a field to a model using the migration tool. I created the migration file in the migrations folder.
That looks like this:

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

And then I am trying to run the migration with: npx sequelize-cli db:migrate and I get the following errors: 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\_npx\12576\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.

I have tried renaming the config to .cjs and then get the error: ERROR: Dialect needs to be explicitly supplied as of v4.0.0 so I don't think it is correctly reading in the ENV variables or something.

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];
dmikester1
  • 1,374
  • 11
  • 55
  • 113

1 Answers1

9

I ran into the same issue recently while trying to run migrations. I solved it using help from the docs.

Note: I'm using Node v15.0.0

If you're using babel, install babel-register package

npm i --save-dev babel-register

Edit your .sequelizerc file to include the package import

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')
}

In both your .sequelizerc and config.js remove traces of es6, instead use common js (require) for imports and exports.

Njeri Ngigi
  • 515
  • 5
  • 15
  • 1
    Finally got around to digging in and working on this and it worked for me! I did have to add a `package.json` file in my config folder with `{ "type": "commonjs" }` to finally get it to work. And I had to change all of my imports to this: `import * as config from '../config/config.js';` – dmikester1 Dec 06 '21 at 04:50
  • 1
    If anyone comes here and has the same issue, just an FYI, I also had to add a `package.json` file to my migrations folder with the contents `{ "type": "commonjs" }` to get the migrations to work correctly. What a mess! – dmikester1 Dec 10 '21 at 17:39
  • adding package.json file in migrations folder with only having { "type": "commonjs" } worked for me too. – Apoorva Ambhoj Apr 04 '22 at 22:51
  • It should be noticed that the question specifies that type is set to module, i.e es6. – Abel LIFAEFI MBULA Dec 10 '22 at 02:42
  • I used a `config.cjs` file instead of `config.js`. – Abel LIFAEFI MBULA Dec 10 '22 at 03:47
  • I am not able to run seeders, migration are working fine. For seeder I am getting `ERROR: require() of ES Module /Users/tinkeshwarsingh/Sites/Open/node-latest-sequelize/database/seeders/20220717130025-initial-admin-user.js not supported. Instead change the require of 20220717130025-initial-admin-user.js in null to a dynamic import() which is available in all CommonJS modules.` – Tinkeshwar Singh Dec 26 '22 at 16:14
  • if you are using @babel/core ^7.0.0 you need to use @babel/register in this solution fyi – ozygz Aug 25 '23 at 09:27