question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I use external library like moment.js in my angular package?

See original GitHub issue

Type of Issue

  • Bug Report
  • Feature Request

Description

Let’s say I want to use momen.js library. When I do

import * as moment from 'moment';

ng-packgr will inline whole library to the bundle. How should I use it to act as peerDependecy so it would use packge installed in module where my library would be consumed?

By the way, despite the moment.js is inlined it seems it is not working when library is consumed by the target application 😦

I use same configuration as in https://github.com/dherges/ng-packaged

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
JoostKcommented, Nov 18, 2017

If you want to use moment both with Rollup (bundling using ng-packagr) and Webpack (if you use the library’s sources directly with Angualar CLI for example) you may find that the import * as moment from 'moment' import is not compatible with Angular CLI’s stack. For a compatible syntax for both bundlers, use the following:

import * as momentNs from 'moment';

const moment = momentNs;
7reactions
aryanismlcommented, Jul 27, 2018

Currently using angular 6 library project and try to incorporate the moment.js in library project. Getting the same error - Cannot call a namespace (‘moment’)

Try the same solution which mention in above,

Added moment in ng-package.json file

“lib”: { “entryFile”: “src/public_api.ts”, “externals”: { “moment”: “moment” } }

However still getting the same error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use moment.js library in angular 2 typescript app?
In the node_modules/angular2-moment will now be ".pipe.d.ts" files like calendar.pipe.d.ts, date-format.pipe.d.ts and many more.
Read more >
How to Use JavaScript Libraries in Angular Project
Moment.js comes with a typing file called moment.d.ts . Now we can directly use the library by using import statement in the ts...
Read more >
How do we replace Moment.js from our Angular Project
Steps to Replace Moment with Day.JS · Install the library · Import the typescript version of the library · Start copying/moving all functions...
Read more >
Configure JavaScript libraries | WebStorm Documentation
On the Libraries page that opens, click Download and in the Download Library dialog that opens, select the required library, and click Download ......
Read more >
Creating libraries - Angular
Use the Angular CLI and the npm package manager to build and publish your library as an npm package. Angular CLI uses a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found