Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

models/index.js doesn't work with ES6 modules

See original GitHub issue

What you are doing?

Trying to load models with models/index.js doesn’t work with ES6 modules activated with "type": "module"

const basename = path.basename(__filename);
// {...}
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);

Variables like __filename and __dirname don’t work anymore with modules.

What do you expect to happen?

There should be an option to choose between Modules or CommonJS.

What is actually happening?

There’s only CommonJS support.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

NixonAzeredocommented, Jan 5, 2022

This is my solution for the moment

import { readdirSync } from "fs";
import { basename, dirname } from "path";
import { Sequelize, DataTypes } from "sequelize";
import { fileURLToPath } from 'url';
import database from "../config/database.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const db = {};
const sequelize = new Sequelize(database.development);

export default (async () => {
  const files = readdirSync(__dirname)
      (file) => file.indexOf('.') !== 0
      && file !== basename(__filename)
      && file.slice(-3) === '.js',

  for await (const file of files) {
    const model = await import(`./${file}`);
    const namedModel = model.default(sequelize, DataTypes);
    db[] = namedModel;

  Object.keys(db).forEach((modelName) => {
    if (db[modelName].associate) {

  db.sequelize = sequelize;
  db.Sequelize = Sequelize;
  return db;
mirzagirlcommented, Dec 17, 2022

model.default …error saying TypeError: model.default is not a function please help

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problems using "index.js" to import ES6 JS Modules
I'm working with React Native and using index.js to manage modules. I have many projects consuming from the same components folder, ...
Read more >
Sequelize model loading with ES6 - Ismayil Khayredinov
A restrospect into defining and loading sequelize models using config objects and ES6 classes.
Read more >
How to use an ES6 import in Node.js?
Node js doesn't support ES6 import directly. If we try to use import for importing modules directly in node js it will throw...
Read more >
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax. A background on modules. JavaScript programs started off pretty ......
Read more >
A Practical guide to ES6 modules
And then just load app.js script in our index.html . But first, in order to make it work, we need to use type="module"...
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 Post

No results found

github_iconTop Related Hashnode Post

No results found