Search code examples
javascriptimportnamespacesunderscore.jslodash

How to import lodash directly into a custom namespace in JavaScript


I would like to import lodash core v4.14.1 from https://raw.githubusercontent.com/lodash/lodash/4.14.1/dist/lodash.core.js. However, simply executing the link adds the library to the global namespace, i.e., window. How can I add the library directly into a custom namespace, say, window.myns? Please note that I don't want to add the library to the global namespace and make a reference to it after.


Solution

  • Use lodash's _.noConflict() to free _ and assign it to a new alias:

    window.myns = _.noConflict();