I'm struggling to import the add
method from the lodash
library using Angular 4 and TypeScript.
I've attempted various approaches:
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 possibly more that I can't recall)
Here is my latest (perhaps desperate?) attempt at accomplishing 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);
}
}
Unfortunately, it's not working.
When I run "ng serve," I receive the following error 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.)
Here is my package configuration:
{
"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",
// Other dependencies...
},
"devDependencies": {
"@angular/cli": "1.0.0-beta.32.3",
// Other dev dependencies...
}
}
and my tsconfig.json file:
{
"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"
]
}
}