Importing lodash into angular2 + typescript application
Update September 26, 2016:
As @Taytay's answer says, instead of the 'typings' installations that we used a few months ago, we can now use:
npm install --save @types/lodash
Here are some additional references supporting that answer:
- https://www.npmjs.com/package/@types/lodash
- TypeScript typings in NPM @types org packages
If still using the typings installation, see the comments below (by others) regarding '''--ambient''' and '''--global'''.
Also, in the new Quick Start, config is no longer in index.html; it's now in systemjs.config.ts (if using SystemJS).
Original Answer:
This worked on my mac (after installing Angular 2 as per Quick Start):
sudo npm install typings --global
npm install lodash --save
typings install lodash --ambient --save
You will find various files affected, e.g.
- /typings/main.d.ts
- /typings.json
- /package.json
Angular 2 Quickstart uses System.js, so I added 'map' to the config in index.html as follows:
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
},
map: {
lodash: 'node_modules/lodash/lodash.js'
}
});
Then in my .ts code I was able to do:
import _ from 'lodash';
console.log('lodash version:', _.VERSION);
Edits from mid-2016:
As @tibbus mentions, in some contexts, you need:
import * as _ from 'lodash';
If starting from angular2-seed, and if you don't want to import every time, you can skip the map and import steps and just uncomment the lodash line in tools/config/project.config.ts.
To get my tests working with lodash, I also had to add a line to the files array in karma.conf.js:
'node_modules/lodash/lodash.js',
Here is how to do this as of Typescript 2.0: (tsd and typings are being deprecated in favor of the following):
$ npm install --save lodash
# This is the new bit here:
$ npm install --save-dev @types/lodash
Then, in your .ts file:
Either:
import * as _ from "lodash";
Or (as suggested by @Naitik):
import _ from "lodash";
I'm not positive what the difference is. We use and prefer the first syntax. However, some report that the first syntax doesn't work for them, and someone else has commented that the latter syntax is incompatible with lazy loaded webpack modules. YMMV.
Edit on Feb 27th, 2017:
According to @Koert below, import * as _ from "lodash";
is the only working syntax as of Typescript 2.2.1, lodash 4.17.4, and @types/lodash 4.14.53. He says that the other suggested import syntax gives the error "has no default export".
First things first
npm install --save lodash
npm install -D @types/lodash
Load the full lodash library
//some_module_file.ts
// Load the full library...
import * as _ from 'lodash'
// work with whatever lodash functions we want
_.debounce(...) // this is typesafe (as expected)
OR load only functions we are going to work with
import * as debounce from 'lodash/debounce'
//work with the debounce function directly
debounce(...) // this too is typesafe (as expected)
UPDATE - March 2017
I'm currently working with ES6 modules
, and recently i was able to work with lodash
like so:
// the-module.js (IT SHOULD WORK WITH TYPESCRIPT - .ts AS WELL)
// Load the full library...
import _ from 'lodash'
// work with whatever lodash functions we want
_.debounce(...) // this is typesafe (as expected)
...
OR import
specific lodash functionality
:
import debounce from 'lodash/debounce'
//work with the debounce function directly
debounce(...) // this too is typesafe (as expected)
...
NOTE - the difference being * as
is not required in the syntax
References:
Good Luck.