Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup smooch-js with RequireJS

I followed Smooch instructions with no success.

Here is the minimal code that fails for me:

<!doctype html><html>
<head>
    <meta charset="utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script>
    <script>
        require.config({
            "paths": {
                "smooch": "https://cdn.smooch.io/smooch.min"
            }
        });
        // Tried this
        require(["require", "smooch"], function(require){
            var Smooch = require("smooch");
            console.log(Smooch);  // → undefined
        });
        // Tried that
        require(["smooch"], function(Smooch){
            console.log(Smooch);  // → undefined
        });
    </script>
</head>
<body></body>
</html>

require returns me an undefined so no Smooch.init(...) for me. Tested in both Firefox and Chrome.

Am I doing something wrong ?

like image 307
Maxime R. Avatar asked Dec 30 '25 08:12

Maxime R.


1 Answers

You should use "Smooch" instead of "smooch" in your require calls, like this :

<!doctype html><html>
<head>
    <meta charset="utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.min.js"></script>
    <script>
        require.config({
            "paths": {
                "Smooch": "https://cdn.smooch.io/smooch.min"
            }
        });
        // Tried this
        require(["require", "Smooch"], function(require){
            var Smooch = require("Smooch");
            console.log(Smooch);  // → Smooch object
        });
        // Tried that
        require(["Smooch"], function(Smooch){
            console.log(Smooch);  // → Smooch object
        });
    </script>
</head>
<body></body>
</html>

The lib is wrapped in a UMD wrapper and self-defines its name to be "Smooch".

like image 126
Marc-Antoine Lemieux Avatar answered Jan 01 '26 00:01

Marc-Antoine Lemieux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!