typescript Moment.js + 打字稿。找不到名字瞬间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37360931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moment.js + TypeScript. Cannot find name moment
提问by user3272018
I develope web application with knockout in VisualStudio. I just installed knockout via bower, included d.ts file in project, included script to html page and now i can get access to ko
.
我在 VisualStudio 中使用淘汰赛开发了 Web 应用程序。我刚刚通过 bower 安装了淘汰赛,在项目中包含了 d.ts 文件,在 html 页面中包含了脚本,现在我可以访问ko
.
Now i try to use moment.js. Like with knockout: install, include d.ts, include script to page and i get an error cannot find name 'moment'
. Adding reference to d.ts does not help, import * as moment from 'moment'
get an error can not find module moment
.
现在我尝试使用moment.js。与淘汰赛一样:安装,包含 d.ts,将脚本包含到页面中,我收到错误消息cannot find name 'moment'
。添加对 d.ts 的引用没有帮助,import * as moment from 'moment'
出现错误can not find module moment
。
I know that it's a stupid problem, but i can't fix it. What am i doing wrong?
我知道这是一个愚蠢的问题,但我无法解决它。我究竟做错了什么?
采纳答案by Balázs édes
What I'd recommend is using some tool for managing your definitions. Some popular options (you don't need both, just pick one):
我建议使用一些工具来管理您的定义。一些流行的选项(您不需要两者都需要,只需选择一个):
tsd
-npm i -g tsd
typings
-npm i -g typings
tsd
——npm i -g tsd
typings
——npm i -g typings
These work in a similar fashion as package managers. You can install your definitions like npm/bower installs your dependencies.
它们的工作方式与包管理器类似。您可以安装您的定义,例如 npm/bower 安装您的依赖项。
Once you have one of these installed, go to your project and install moment + its definition
安装其中之一后,转到您的项目并安装 moment + 它的定义
npm install moment --save
And one of these:
其中之一:
tsd install moment --save
typings install moment --save --ambient
Both of these will create a folder with your definitions in it (both call it typings), and both have an "umbrella" definition file in it, which you should reference in the entry point of your application (first is for tsd, second for typings):
这两个都将创建一个文件夹,其中包含您的定义(都称其为类型),并且其中都有一个“伞形”定义文件,您应该在应用程序的入口点中引用该文件(第一个用于 tsd,第二个用于打字):
/// <reference path="typings/tsd.d.ts" />
/// <reference path="typings/index.d.ts" />
After this is done, you can use moment (or any other module) as you would:
完成此操作后,您可以像使用 moment(或任何其他模块)一样:
import * as moment from 'moment'
moment.isDate("I'm not a date")
I suggest checking out these:
我建议检查这些:
https://github.com/DefinitelyTyped/tsd
https://github.com/typings/typings
https://github.com/DefinitelyTyped/tsd
https://github.com/typings/typings
回答by Raúl Ota?o
In my case, finally get this error solved by doing the following:
就我而言,最终通过执行以下操作来解决此错误:
- Adding this option
"allowSyntheticDefaultImports": true,
in thecompilerOptions
section of thetsconfig.json
file (EDITED: As the moment doc. sais in the Note:If you have trouble importing moment, try add "allowSyntheticDefaultImports": true in compilerOptions in your tsconfig.json file.
) - Also add
"moduleResolution": "node"
in the samecompilerOptions
section. (Found this option looking around the web) - Importing moment module like this
import * as moment from 'moment';
- 添加此选项
"allowSyntheticDefaultImports": true,
中compilerOptions
的第tsconfig.json
(文件EDITED:作为时刻文档的最高审计机关。注:If you have trouble importing moment, try add "allowSyntheticDefaultImports": true in compilerOptions in your tsconfig.json file.
) - 也添加
"moduleResolution": "node"
在同compilerOptions
一部分。(在网上找到这个选项) - 像这样导入矩模块
import * as moment from 'moment';
回答by Iosif Petre
I had the same error today. The fix was to downgrade the "moment" package from "2.25.0" to "2.22.1" in package.json
我今天有同样的错误。修复是将 package.json 中的“moment”包从“2.25.0”降级到“2.22.1”
My application is an angular 8 version and "typescript": "3.5.3
我的应用程序是 angular 8 版本和“打字稿”:“3.5.3
回答by Sylvio Menubarbe
For me, error comes from moment version 2.25.0. A workaround for the moment is to downgrade to 2.24.0.
对我来说,错误来自于 2.25.0 版本。目前的解决方法是降级到 2.24.0。
In [email protected] - package.json :
在 [email protected] - package.json 中:
"typesVersions": {
">=3.1": {
"*": [
"ts3.1-typings/*"
]
}
}
But folder 'ts3.1-typings' does not exist. If your typescript version is 3.1 or higher you'll have error :
但是文件夹“ts3.1-typings”不存在。如果您的打字稿版本为 3.1 或更高版本,则会出现错误:
Cannot find module 'moment'
Github issue : https://github.com/moment/moment/issues/5486
Github 问题:https: //github.com/moment/moment/issues/5486
回答by Samih
This seems to solve the problem for me (the other solutions in this thread didn't work):
这似乎为我解决了问题(该线程中的其他解决方案不起作用):
import * as moment from 'moment/moment.js';
import * as moment from 'moment/moment.js';
I still seem to be getting the correct typescript definitions even referencing the js file directly. I suspect the reason is that the 'moment' folder under node_modules does not have an index.js file but I'm not a typescript typings/compiler expert.
即使直接引用 js 文件,我似乎仍然获得了正确的打字稿定义。我怀疑原因是 node_modules 下的“moment”文件夹没有 index.js 文件,但我不是打字稿打字/编译专家。
回答by Sunitha Nagappa
Try adding the below code, it worked for me
尝试添加以下代码,它对我有用
import * as MomentD from "node_modules/moment/moment.d";