Reputation: 2969
I'm trying to just import the add
method of lodash
using angular4+typescript.
I tried a lot of differents things:
import { add } from 'lodash';
import { add } from 'lodash/add';
import * as add from 'lodash/add';
import { add } from 'lodash.add';
import * as add from 'lodash.add';
(and others I've forgotten)
Here's my latest (desesperate?) attempt at achieving this:
import { Component, OnInit } from '@angular/core';
import add = require('lodash.add');
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
private x: number;
constructor() { }
ngOnInit() {
let y = add(5, 5);
}
}
But it doesn't work.
When I "ng serve" I get the message:
ERROR in ...test.component.ts (2,1): Import assignment cannot be used when targeting ECMAScript 2015 modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.)
My package config:
{
"name": "treeshake",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"ng": "ng",
"start": "ng serve",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/common": "^2.4.0",
"@angular/compiler": "^2.4.0",
"@angular/core": "^2.4.0",
"@angular/forms": "^2.4.0",
"@angular/http": "^2.4.0",
"@angular/platform-browser": "^2.4.0",
"@angular/platform-browser-dynamic": "^2.4.0",
"@angular/router": "^3.4.0",
"@types/lodash": "lattest",
"@types/lodash.add": "^3.7.2",
"core-js": "^2.4.1",
"lodash": "^4.17.4",
"lodash.add": "^3.7.0",
"rxjs": "^5.4.2",
"zone.js": "^0.7.6"
},
"devDependencies": {
"@angular/cli": "1.0.0-beta.32.3",
"@angular/compiler-cli": "^2.4.0",
"@types/jasmine": "2.5.38",
"@types/node": "~6.0.60",
"codelyzer": "~2.0.0-beta.4",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.0",
"ts-node": "~2.0.0",
"tslint": "~4.4.2",
"typescript": "^2.4.1"
}
}
and my tsconfig.json
{
"compilerOptions": {
"baseUrl": "",
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2016",
"dom"
],
"mapRoot": "./",
"module": "es2015",
"moduleResolution": "node",
"outDir": "../dist/out-tsc",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/@types"
]
}
}
Upvotes: 2
Views: 1403
Reputation: 6029
I recommend using lodash-es
, which is a the same lodash but distributed with each function as a separate ECMA Script module. Then to import just the code needed for the add
function you:
import add from 'lodash-es/add'
Note that it also has its own @types/lodash-es
package to use instead of @types/lodash
.
Upvotes: 3
Reputation: 41533
Your version in package.json
is wrong. Correct the typo
@types/lodash": "lattest",
You should be importing the lodash as
import * as _ from 'lodash';
Ensure that you have lodash listed in the typings.json file
Update 1:
Install typings
globally using
npm install typings -g
Then install lodash
through typings as
typings install lodash --save
Use the import statement as
import { add } from 'lodash';
Upvotes: 1