18

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
AndrewBenjamin
  • 651
  • 1
  • 7
  • 16

8 Answers8

13

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

Dan
  • 1,130
  • 2
  • 20
  • 38
Nizam Khan
  • 207
  • 2
  • 3
  • This can not be an answer. Please insert it as a comment. – dpapadopoulos Feb 18 '19 at 09:46
  • 1
    @dpap Looks fine to me as an answer, and works in TS. – Dan Feb 19 '19 at 14:39
  • Uh huh... This answer results in 'lodash.js is not a module' as outlined in the second sentence of the question. So this answer doesn't help at all. If "import * as _ from 'lodash'" was the answer I wouldn't have had any trouble and this question wouldn't exist. "import * as _ from 'lodash'" will never work if your @typings file is incompatible with your framework. The accepted answer, which came earlier, addressed the issue. However, since this post I have refused to work outside major frameworks because there isn't much support... – AndrewBenjamin Mar 15 '19 at 18:09
  • Worked for me as well. If this is somehow "bad", I'd like to hear why, and the proper way of dealing with global imports in combination with TS. – svenema Feb 17 '22 at 11:45
10

None of the other answers worked for me. Here is how I solved it.

With this method you will be able to load lodash from the global include

You will also have critically important type hinting without having to build lodash into your webpack bundle

Lodash will now be accessible globally in all your ts files without needing to import it

Here is my setup.

HTML

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js" integrity="sha256-VeNaFBVDhoX3H+gJ37DpT/nTuZTdjYro9yBruHjVmoQ=" crossorigin="anonymous"></script>
</head>

NPM

npm i @types/lodash lodash --save-dev

Webpack

externals: {
    _: 'lodash'
}

global.d.ts

import _ from 'lodash';
declare global {
    const _: typeof _;
}

tsconfig.json

"types": [
    "lodash"
],

entry.ts

console.log(_.VERSION)

This method will speed up the time it takes to compile bundles. Reduce your build size and in turn reducing the download size to the client after each build and allow the client to use the cached library from the CDN which they may already have in memory from previous builds, other pages or other sites.

Dieter Gribnitz
  • 5,062
  • 2
  • 41
  • 38
9

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
Feng
  • 397
  • 3
  • 6
  • 2
    This isn't really a solution, more of a hacky workaround and defeats the purpose of TS – Emobe Feb 25 '19 at 17:32
  • when I try to compile this `_.union([1], [2, 3]);` I got: `Cannot use namespace '_' as a value.` – Max Mar 23 '19 at 14:27
8

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

declare const _;

It works for me. See also

Max
  • 853
  • 1
  • 10
  • 18
1

There's a compiler option for allowing this if you know it's safe:

--allowUmdGlobalAccess Allow accessing UMD globals from modules.

LukeGT
  • 2,324
  • 1
  • 21
  • 20
0

'_' 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.

AndrewBenjamin
  • 651
  • 1
  • 7
  • 16
0

I was facing the same error while using loadash in my react application. What I was doing wrong is _.uniqWith() .

then I just removed underscore and dot and used it as uniqWith().

This resolved my error

import { uniqWith } from loadash;
0

In declarations.d.ts:

import type { LoDashStatic } from 'lodash';

declare global {
  const _: LoDashStatic;
}
Anton Liannoi
  • 543
  • 5
  • 13