Lodash - '_' refers to a UMD global and lodash.js is not a module errors

怎甘沉沦 提交于 2019-12-10 03:34:41

问题


If I do nothing and add the type definition it works for jquery but lodash gets a

'_' referes to a UMD global, but the current file is a module. Consider adding an import instead.

If I try to import lodash with any combination of import call, I get

lodash.js is not a module

I tried to get help by asking a question here and it was closed because there was another answer for Angular (Angular 2). I really need a real answer so I'm re-posting with my own solutions, hoping someone will eventually help me understand this problem.

In this case I am:

  1. Not using any frameworks like Angular
  2. Using TypeScript
  3. Using requirejs to import modules
  4. Using Visual Studio
  5. Using MVC5

回答1:


I added following code in my global typing definition file (~\src\typings.d.ts) to fix the problem. I'm using Angular CLI 1.7

// lodash global typing - begin
declare namespace _ {
}
// lodash global typing - end



回答2:


I added in my ~\src\typings.d.ts :

declare const _;

It works for me. See also




回答3:


I use import * as _ from 'lodash'; in my working file after installing lodash.




回答4:


'_' referes to a UMD global, but the current file is a module. Consider adding an import instead.

This is caused by the type definition. Go into your @types/lodash file and delete these lines:

export = _;
export as namespace _;

This will solve the error.

***EDIT****

Removing the export statements above will cause another error if you have any global declarations in your type definition file, for example, like this:

declare global { }

To solve this, either move the properties from the global declaration to the interfaces that reference them - or delete them entirely and change the types of the references to something generic. I'm not sure which solution will make a better final compilation but the former has not caused any problems in my application as far back as IE11.



来源:https://stackoverflow.com/questions/47542928/lodash-refers-to-a-umd-global-and-lodash-js-is-not-a-module-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!