Moment.js + TypeScript. Unable to find the moment of name

I am developing a knockout web application in VisualStudio. I just installed a knockout via bower, included the d.ts file in the project, included the script in the html page, and now I can access ko .

Now I'm trying to use moment.js. As with the knockout: install, enable d.ts, enable the script on the page, and I get the error message cannot find name 'moment' . Adding a link to d.ts does not help, import * as moment from 'moment' to get the error message can not find module moment .

I know this is a stupid problem, but I cannot fix it. What am I doing wrong?

+5
source share
2 answers

I would recommend using some tool to manage your definitions. Some popular options (you do not need both, just choose one):

  • tsd - npm i -g tsd
  • typings - npm i -g typings

They work similarly to package managers. You can set your definitions, for example, npm / bower installs your dependencies.

Once you have installed one of them, go to your project and set the moment + its definition

 npm install moment --save 

And one of them:

 tsd install moment --save typings install moment --save --ambient 

Both of them will create a folder with your definitions in it (both call it typifications), and both have an umbrella definition file in it that you must reference at the entry point of your application (first for tsd, the second for typing):

 /// <reference path="typings/tsd.d.ts" /> /// <reference path="typings/index.d.ts" /> 

After that, you can use the moment (or any other module) like you:

 import * as moment from 'moment' moment.isDate("I'm not a date") 

I suggest checking them out:

https://github.com/DefinitelyTyped/tsd
https://github.com/typings/typings

+3
source

In my case, finally get this error by doing the following:

  • Adding this option "allowSyntheticDefaultImports": true, to the compilerOptions section of the tsconfig.json file ( EDITED : as a doc . Sais moment in Note: If you have trouble importing moment, try add "allowSyntheticDefaultImports": true in compilerOptions in your tsconfig.json file. )
  • Also add "moduleResolution": "node" to the same compilerOptions section. (Found this option browsing the web).
  • Import a moment module like this import * as moment from 'moment';
+3
source

All Articles