How to correctly include lodash/underscore on website with conflicting libs?

The latest CDN version is 4.17.15 :)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"> </script>
<script>
_u = _.noConflict();
</script>

As long as there are no relevant async scripts before the manual method, it should always work:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
<script>
_u = _.noConflict(); // lets call ourselves _u
</script>

It makes no difference if other scripts set/use _ before or after this. (lodash remembers the last setting of _ and restores it on the _.noConflict() call.)

But if scripts before this are async there is always a possibility that they are allowed to execute between these two scripts. You would have to either use AMD or combine the manual setting into the same script as lodash to avoid races with async scripts.


You can use the "_" directly in another js file if you have loaded the cdn script tag of lodash.