jondro
jondro

Reputation: 619

How to import Lodash with TypeScript and Webpack without overwriting existing _ var

I'm creating a module for an application that uses Lodash 3. In that module I'd like to use Lodash 4. The new module is written in TypeScript and packed with Webpack.

I thought that if I do import * as _ from 'lodash' in the new module it would not overwrite the window._ but it does. I noticed this issue on GitHub https://github.com/lodash/lodash/issues/1798 and it looks like my issue and it seemed to be resolved in 4.14.0. I'm using Lodash 4.15.0

Is there something special that I have to do while importing Lodash in my code not to overwrite window._?

Upvotes: 4

Views: 1778

Answers (1)

Scotty Waggoner
Scotty Waggoner

Reputation: 3380

See Lodash unexpectedly injects itself into global when required in subdependency.

Adding

module: {
    noParse: /node_modules\/lodash\/lodash\.js/,
}

to my config stopped lodash from exporting itself to the window.

If you still have issues, you could look at https://lodash.com/docs/4.17.4#noConflict although I'm not sure how well that would solve your problem.

Upvotes: 1

Related Questions