Definitelytyped: Cannot find namespace 'moment'

Created on 4 Mar 2016  路  18Comments  路  Source: DefinitelyTyped/DefinitelyTyped

Hi everybody,

Iam new in using Typescript and angular. First of all i want to use moment.js and jquery i made a reference path in my bootstrap.v3.datetimepicker.d.ts and in my personal ts file. Linked to jquery and moment, and included the node_module scripts in the index.html. But it keeps giving me the error in my terminal that 'moment' namespace cannot be found. I think i am missing something but i do not know what...

Thanks in advance everybody

Most helpful comment

typings install --save --global dt~moment dt~moment-node

All 18 comments

I was having the same problem, and no amount of typings install moment was fixing the issue, regardless of the options I threw at it.

My solution was to manually grab a copy of moment-node.d.ts and place it in my browser/ambient/moment and main/ambient/moment directories so that moment.d.ts could get the definitions it needs.

having the same issue... Really don't want to resort to hacky fixes

Same issue here. Thanks for the tip, @karelwgonzalez. I'll try it.

I'm using aurelia (jspm & system.js) with typescript.
I found adding an explicit import to my bootstrap.v3.datetimepicker.d.ts resolved the moment reference

import * as moment from 'moment';

Figured out a better solution that is similar, but not a manual import of the file.

Duplicate the moment.d.ts line from your typings.json and change the instances of moment to moment-node.

e.g.

{
  "ambientDependencies": {
    "moment": "github:DefinitelyTyped/DefinitelyTyped/moment/moment.d.ts#56295f5058cac7ae458540423c50ac2dcf9fc711",
    "moment-node": "github:DefinitelyTyped/DefinitelyTyped/moment/moment-node.d.ts#56295f5058cac7ae458540423c50ac2dcf9fc711"
  }
}

Then run your typings installer again, and the error should go away.

@karelwgonzalez Thank you for your solution. It didn't work for me (maybe I did it wrong), but lead me to a _slightly_ different solution.

typings install --save --ambient moment moment-node.

@dachibro Weird.

If it was the last line in your json, did you remove the trailing comma? Either way, glad you got it working.

I tried installing moment-node like that and I kept getting a not found error.

@karelwgonzalez It's possible we are using different versions of typings, because my typings.json URLs look a lot different than yours:

"ambientDependencies": {
    "jasmine": "registry:dt/jasmine#2.2.0+20160317120654",
    "moment": "registry:dt/moment#2.8.0+20160316155526",
    "moment-node": "registry:dt/moment-node#2.11.1+20160329220348",
    "node": "registry:dt/node#4.0.0+20160330064709",
    "require": "registry:dt/require#2.1.20+20160316155526"
  }

I am using 0.7.12 and node 5.10.1.

@dachibro That's, probably exactly it. I'm still on 0.6.8 for my project.

Beware version problems too
typings install bootstrap.v3.datetimepicker uses bootstrap.v3.datetimepicker/bootstrap.v3.datetimepicker-3.0.0.d.ts

To get the latest version (4.17.37) use
typings install "github:DefinitelyTyped/DefinitelyTyped/bootstrap.v3.datetimepicker/bootstrap.v3.datetimepicker.d.ts"

Tryed the new moment.d.ts version 2.13.0, same issue. But it seems i am unable to make it fit with the little trick of moment-node.

///
private moment: moment.Moment = require("moment/moment");

Cannot find namespace 'moment'

Have to backtrack to old way.
Anyone able to make 2.13.0 work as an amd dependency?

It's not work for me too :(`

{
  "ambientDependencies": {
    "moment": "registry:dt/moment#2.8.0+20160316155526"
  }
}

Installing both moment and moment-node works though...
typings i -S moment moment-node --ambient

typings install --save --global dt~moment dt~moment-node

moment 2.13.0 now ships with its own .d.ts . But doesn't work as an amd unfortunatelly.

I installed as a --global and it appears correctly in typings/globals/index.d.ts but the generated file is empty except for declaring moment. I do see that in my bower components, moment directory does include the typescript definition, but the two aren't linked for the compiler. I guess you do have to install moment-node definition, which is not expected.

how integrate moment with Angular2? Facinf similar kinda error

Cannot find namespace 'moment'.

@shamoh19 You need to install the @types/moment module along with the momentjs module.

Was this page helpful?
0 / 5 - 0 ratings