Error starting nodejs+Sequelize+mysql project

Asked

Viewed 544 times

1

When I try to start my project this giving the following error:

D:\www\nodejs\master>node bin/www
module.js:472
    throw err;
    ^

Error: Cannot find module 'sequelize'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\www\nodejs\master\utils\modelLoader.js:6:17)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

The archive he refers to is this:

'use strict'

const env = process.env.NODE_ENV || "development";            
const config = require('../config/config.json')[env];
const path = require("path");
let Sequelize = require("sequelize");

//Criando a conexão com o banco de dados de acordo com a configuração do config.json
let sequelize = new Sequelize(config.database, config.username, config.password, config);
let db = {};

//Criando um array do caminho dos modelos
const models = [
    '../modulos/cidades/model/cidades',
];

let l = models.length;

//Irá importar os modelos para o sequelize
for (let i = 0; i < l; i++) {
    let model = sequelize.import(path.join(models[i]));
    db[model.name] = model;
}

//Irá percorrer e separar apenas o objeto que contém a propriedade associate, sem o "associate" o sequelize não monta uma relação!
Object.keys(db).forEach((modelName) => {
    if ("associate" in db[modelName])
        db[modelName].associate(db);
});

db.sequelize = sequelize;


module.exports = db;

Files cited in it:

const config = require('../config/config.json')[env];

{
    "development": {
        "dialect": "mysql",
        "port": 3306,
        "host": "localhost",
        "schema": "public",
        "database": "master",
        "username": "root",
        "password": "",
        "logging": false
    }
}
    '../modulos/cidades/model/cidades',
'use strict'

module.exports = (sequelize, DataTypes) => {

    const Cidades = sequelize.define('cidades', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            field: 'cidade_codigo'
        },
        descricao: {
            type: DataTypes.STRING,
            field: 'cidade_descricao'
        }
    }, {
        freezeTableName: true,
        schema: 'public',
        tableName: 'cidade',
        timestamps: false
    });

    return Cidades;
};

Could someone help me see what’s wrong ?

  • Fez npm install sequelize?

  • @Sergio Sim -> $ npm install --save mysql

  • Your mistake is Cannot find module 'sequelize', then you must do npm install by https://www.npmjs.com/package/sequelize, and not by mysql right?

2 answers

3


Cannot find module 'sequelize' means he can’t find the module sequelize.

Tests install with:

npm install --save sequelize

0

After installing the sequel as Sergio said,.

npm install --save mysql2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.