Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Class constructor model cannot be invoked without 'new' after uninstalling babel #9280

Closed
ekkatharisi opened this issue Jan 5, 2019 · 7 comments
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@ekkatharisi
Copy link

ekkatharisi commented Jan 5, 2019

Bug Report

Current Behavior
i have a trouble using babel , and when i unistalling it to use just the ES5 , it shoots this error :
TypeError: Class constructor model cannot be invoked without 'new'
this is my code :
Input Code

        const fs = require('fs');
        const path = require('path'); 
        const Sequelize = require('sequelize');
        const basename = path.basename(__filename);
        const env = 'development';
        const config = require('../config/config')[env];

           const sequelize  = new Sequelize(config.database.database_name, config.database.username, 
           config.database.password, config.database.database_config);

      var db = {
         sequelize : sequelize,
         Sequelize : Sequelize
      };

        db.sequelize.authenticate().then(() => {
        console.log('database worked !!!');


    fs.readdirSync(path.join(__dirname,'models/')).filter(file => {
        return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
    }).forEach(file => {
        var model = db.sequelize.import(path.join(__dirname,"models/", file));
        //var model = require(path.join(__dirname,"models/", file))(sequelize, Sequelize);
        db[model.name] = model;
    });

       Object.keys(db).forEach(modelName => {
           if (db[modelName].associate) {
               db[modelName].associate(db);
          }
         } );
       db.sequelize.sync({force : true}).then(res => {
           console.log('database created !!!!!!!!!');
      }).catch(err => {
           console.log(err.stack);
     })
        }).catch(error => {
            console.log(error.stack);
       });
       module.exports = db;

please what i should do now ??

Environment

  • Babel version(s): [e.g. v6.0.0, v7.0.0-beta.34]
  • Node/npm version: [e.g. Node 10.14.2/npm 6.4.1]
  • OS: [Windows 10]
  • How you are using Babel: [cli]
@babel-bot
Copy link
Collaborator

Hey @ekkatharisi! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo
Copy link
Member

Which lines doe throw that error?
Also, I'm not sure that I understood the question: your code works when if you use Babel but doesn't work without Babel?

@ekkatharisi
Copy link
Author

ekkatharisi commented Jan 7, 2019

thank you @nicolo-ribaudo for responding me, so the problem that i install BABEL in my laptop (and other pc), after that the console shoots this error in this line
var model = db.sequelize.import(path.join(__dirname,"models/", file));
after , i remove BABEL by this comande :
npm uninstall babel-cli babel-preset-es2015 babel-preset-env
npm uninstall -g babel-cli babel-preset-es2015 babel-preset-env
but the error still the same, what I want is to execute the code fine without this error.
NOTE 1: the error occurs when I use this command
node connect.js
NOTE 2: I use node version 10.13.0.
the whole project is in this repo
thank you in advance.

@nicolo-ribaudo
Copy link
Member

Maybe you can find a solution at sequelize/sequelize#7840.

Another possible solution is to transpile the sequelize module to es5.
Btw, I suggest you to use Babel 7 instead of Babel 6.

@ekkatharisi
Copy link
Author

but the sequelize modules are transpiled to es5, (have you see any es6 expression )
and no as i say i remove conmpletely BABEL , but the error still the same .
and thank you @nicolo-ribaudo for reference but i don't find the solution yet.

@TidyIQ
Copy link

TidyIQ commented Apr 27, 2019

I'm experiencing the same issue although as far as I'm aware it only occurs when I AM using babel (if I remove babel then none of my imports work so I can't run my app to check).

It's also occurring for me at sequelize.import()

edit: nope, it was also not working without babel. I believe it's an issue with sequelize. I switched to the sequelize-typescript package so I don't need to use sequelize.import() anymore and now my app works fine.

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Apr 9, 2020

If this was a Babel bug, it has been fixed by #8656

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 10, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: question outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants