Node.js object is not a function - module.exports

前端 未结 1 1973
面向向阳花
面向向阳花 2021-02-05 09:20

I have a module I created for a node.js app. The app also uses socket.io and I want to pass the socket.io object into the auction object when I create it.

This works wh

1条回答
  •  生来不讨喜
    2021-02-05 10:00

    You are exporting an object with 1 property Auction

    When you required the module, you imported an object which looks like

    {
      Auction: function(){...}// Auction function
    }
    

    So either export just the function:

    module.exports = Auction;
    

    or reference the property when you require the module:

    var  Auction = require('./lib/auction').Auction;
    

    By default, module.exports is an empty object : {}

    You can replace exports with a function. This will export just that function.

    Or you can export many functions, variables, objects, by assigning them to exports. This is what you have done in your question: assigned the function Auction to the property Auction of exports.

    0 讨论(0)
提交回复
热议问题