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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With