![](/img/trans.png)
[英]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.