[英]What is a module and difference between module.exports vs exports?
[英]What is the difference between "module.exports" and "exports.module" in java script
我创建了一个名为“Product”的 Model 和下面的代码
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;
我收到以下错误
Product.create is not a function
如果我替换exports.module = Product;
与module.exports = Product;
, 它工作得很好。
任何人都可以澄清两者之间的区别
module.exports
是用于从 CommonJS 模块中导出值的属性。
exports
是module.exports
的简写。
exports.module
与 module.exports.module 相同,它在导出的module.exports.module
上创建一个名为module
的值。
因此,如果您有:
module.exports = Product;
然后你会匹配它:
const Product = require('./myModule');
但如果你有:
exports.module = Product;
然后你会:
const myModule = require('./myModule');
const Product = myModule.module;
exports.module
基本上是一个错误,使代码混乱。 不要那样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.