Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
How can I export lodash from my Angular module?

I would like to make a generic module that has a bunch of utility methods, including moment and lodash functionality. I've imported lodash and typed it and I can access it from my components as long as I include the code:

import * as _ from "lodash";

However, I'd like to make this more generic so I can just import my Utils module and have both lodash and moment (as well as a few custom functions) available without having to import these individually in every component.

I've got the following code:

import { Injectable, NgModule } from "@angular/core";
import * as _ from "lodash";
import * as _ from "moment";

@Injectable()
@NgModule({
    exports: [_, moment]
})
export class Utils { }
}

I get the following error:

Build:Argument of type '{ exports: LoDashStatic[] 1>; }' 
   is not assignable to parameter of type 'NgModule'

Edit: What I'd like to do is this:

import { _, moment } from "./Utils.module";

instead of

import * as _ from "lodash";
import * as moment from "moment";

How can I accomplish this?

Edit: Made the code reflect more of what I'd like to accomplish...

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This

@NgModule({
    exports: [_, moment]
})
...

won't work (and thanks to TypeScript, it can detect the issue with type checking), because exports contains for Angular modules, while these are ES6 modules:

import { _, moment } from "./Utils.module";

Instead, it may be not .module.ts file, which are conventionally used for Angular modules, but utils.ts:

import * as _ from "lodash";
import * as moment from "moment";
export { _, moment };

And usually the one should never want to have a separate 'util' file to import third-party packages from it like import { _, moment } from "./utils". The path is relative and needs to be maintained in each file where it is imported. The paths to the packages are not, that's their major advantage.

The whole thing looks like modular antipattern. You won't see this in well-designed projects, because it makes little sense. I would personally recommend to not do this, unless you are really sure what are your benefits here.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!