RequireJS

Used AMD (Asynchronous Module Definition) API to support the node.js syntax of using require() to work with browser.

Considering the following file:

// src/logger.js
class Logger {  
    log(content) {  
        console.log(content)  
    }  
}

module.exports = new Logger()
// index.js
var Logger = require('./src/logger.js')  
  
var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];  
  
Logger.log(unique(data));

To use it with RequireJS, we need to change the code slightly

Export

// src/logger.js
class Logger {  
    log(content) {  
        console.log(content)  
    }  
}

define(function() {  
    return new Logger()  
})

Import

require(['src/logger'], (Logger) => {  
    var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];  
    Logger.log(data);  
})

or better syntax (closer to Node.js syntax above)

// src/index.js
define(function (require) {  
    var Logger = require('./src/logger.js')  
    var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];  
    Logger.log(data);  
})

Browser:

<html>  
    <body>Example requirejs</body>  
    <script data-main="index" src="require.js"></script>  
</html>

Where: