Search code examples
javascriptbrowserify

What is wrong with my Browserify JavaScript code?


In my main.js, I have:

var example = require('./test');
example.speak('Hello world');

While in my test.js, I have:

module.exports = function() {
    this.speak = function(str) {
        str += ' < you typed.';
        return str;
    };
    this.listen = function(str) {
        return str;
    };
};

What exactly am I doing wrong so that I can use browserify correctly with this?


Solution

  • You should either export an object:

    module.exports = {
        speak: function () {},
        // etc
    }
    

    or use

    var example = new require("./test")();