0

I have created a Model named 'Product' and the below code in there

const Sequelize = require('sequelize');

const sequelize = require('../util/database');

const Product = sequelize.define('product',{
    id: {
        type: Sequelize.INTEGER,
        autoincrement: true,
        allowNull: false,
        primaryKey: true
    },
    title: Sequelize.STRING,
    price: {
        type: Sequelize.DOUBLE,
        allowNull: false
    },
    imageUrl:{
        type: Sequelize.STRING,
        allowNull: true
    },
    description: {
        type: Sequelize.STRING,
        allowNull: true
    }
});

exports.module = Product;

I am getting the below error

Product.create is not a function

If I replace exports.module = Product; with module.exports = Product;, It is working good.

Can anyone clarify what is the difference between the two

Madhu
  • 37
  • 5

1 Answers1

4

module.exports is the property used to export values from a CommonJS module.

exports is a shorthand for module.exports.

exports.module is the same as module.exports.module which creates an value named module on the exported object.


So if you have:

module.exports = Product;

Then you would match it with:

const Product = require('./myModule');

But if you have:

exports.module = Product;

Then you would:

const myModule = require('./myModule');
const Product = myModule.module;

exports.module is, basically, a mistake and makes code confusing. Don't do that.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335