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.

problem with momentjs

See original GitHub issue

Type of Issue

[x ] Bug Report
[ ] Feature Request

Description

I have a build error when I include momentjs.

Cannot call a namespace ('moment')

I check the other articles about momentjs but couldn’t make it work. Maybe I’m missing something

How To Reproduce

I have used the https://github.com/dherges/ng-packaged.

In my package.json I have installed momentjs:

"moment": "^2.19.1",

In my ng-package.json I have:

  "externals": {
      "moment": "moment"
    }

In my my-lib/package.json I put the peerDependencies like:

{
  "name": "@moblibs/my-lib",
  "version": "1.0.0",
  "repository": "https://github.com/dherges/ng-packaged.git",
  "author": "david <david@spektrakel.de>",
  "license": "MIT",
  "private": true,
  "peerDependencies": {
    "@angular/core": "^4.1.2",
    "@angular/common": "^4.1.2",
    "moment": "^2.19.1"
  }
}

In my component I import it like:

import * as moment from 'moment';

When I build my my-lib I have the following error

BUILD ERROR
Cannot call a namespace ('moment')
Error: Cannot call a namespace ('moment')
    at error (C:\wksp\componentgeneration\ng-packaged\node_modules\rollup\dist\rollup.js:185:14)
    at Module.error (C:\wksp\componentgeneration\ng-packaged\node_modules\rollup\dist\rollup.js:8390:3)
    at Node.bind (C:\wksp\componentgeneration\ng-packaged\node_modules\rollup\dist\rollup.js:6419:17)
    at eachChild.child (C:\wksp\componentgeneration\ng-packaged\node_modules\rollup\dist\rollup.js:5819:34)
    at keys.forEach.key (C:\wksp\componentgeneration\ng-packaged\node_modules\rollup\dist\rollup.js:5847:5)
    at Array.forEach (native)
    at Node.eachChild (C:\wksp\componentgeneration\ng-packaged\node_modules\rollup\dist\rollup.js:5840:13)
    at Node.bind (C:\wksp\componentgeneration\ng-packaged\node_modules\rollup\dist\rollup.js:5819:8)
    at Node.bind (C:\wksp\componentgeneration\ng-packaged\node_modules\rollup\dist\rollup.js:6242:9)
    at eachChild.child (C:\wksp\componentgeneration\ng-packaged\node_modules\rollup\dist\rollup.js:5819:34)

Expected Behaviour

To be able to build my library without error with momentjs.

Version Information

@angular/cli: 1.4.9
node: 7.9.0
os: win32 x64
@angular/common: 4.4.6
@angular/compiler: 4.4.6
@angular/core: 4.4.6
@angular/forms: 4.4.6
@angular/http: 4.4.6
@angular/platform-browser: 4.4.6
@angular/platform-browser-dynamic: 4.4.6
@angular/router: 4.4.6
@angular/cli: 1.4.9
@angular/compiler-cli: 4.4.6
@angular/language-service: 4.4.6
typescript: 2.5.3

please include any version information that might be relevant, e.g. other third-party libraries “moment”: “^2.19.1”,

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

30reactions
ovionecommented, Oct 25, 2017

I made it work modifying the import in my library component FooComponent from import * as moment from 'moment';

to

import * as momentImported from 'moment'; const moment = momentImported;

I read that in https://stackoverflow.com/questions/39519823/using-rollup-for-angular-2s-aot-compiler-and-importing-moment-js

With that said now I’m able to generate the lib, install it into my project, build my project for prod and aot enabled and start it and test it in a browser.

Of course it works without giving any errors but is this the bes way ?

thanks O

20reactions
matheocommented, Jun 29, 2018

@ranglang the namespace fix worked for me, replacing the import of moment like:

import * as momentNs from 'moment';
const moment = momentNs;

and sometimes I was using the type moment.Moment; and I had to replace it with momentNs.Moment and it worked. Do not forget about the “externals” setup, that got renamed to umdModuleIds in the latest version, mine looks like:

  "ngPackage": {
    "dest": "./dist",
    "lib": {
      "entryFile": "./src/datepicker/index.ts",
      "umdModuleIds": {
        "@angular/cdk": "ng.cdk",
        "@angular/material": "ng.material",
        "lodash": "_",
        "moment": "moment",
        "moment-timezone": "moment"
      }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

momentjs - Issue with using moment.js - Stack Overflow
I was creating project in Angular and to format date I decided to use moment.js. But the problem is from the backend I...
Read more >
Guides - Moment.js
The guides area is designed to help developers learn to better interact with the date and time problem domain, and the Moment.js library....
Read more >
You don't (may not) need Moment.js - GitHub
Problems with Moment.js : It is highly based on OOP APIs, which makes it fail to work with tree-shaking, thus leading to a...
Read more >
Is anyone else having problems with moment.js today? - Reddit
I just had to fix a moment.js bug where it was calculating both this month and next month the same. I think I...
Read more >
Working with JavaScript Dates Using Moment.js - WebFX
The problem with JavaScript's Date object isn't the functionality it brings; it's just extremely cumbersome to use as a developer. If you want...
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