[英]Cannot find module 'date-fns'
When I import installed node module "date-fns" using当我使用导入已安装的节点模块“date-fns”时
import { startOfDay } from 'date-fns';
then I get compile error:然后我得到编译错误:
error TS2307: Cannot find module 'date-fns'.
However following does not give any error:但是,以下不会给出任何错误:
var startOfDay = require('date-fns');
Any idea why this is happening?知道为什么会这样吗?
Following is my installed date-fns:以下是我安装的 date-fns:
node_modules/date-fns/
├── start_of_day
│ ├── index.js
│ └── index.js.flow
Try using this instead First import the modules尝试使用这个代替首先导入模块
In the terminal type在终端类型
npm install date-fns @types/date-fns
Then in code然后在代码中
import * as startOfDay from "date-fns";
If you specifically only want to use the start_of_day function you should instead follow the documentation .如果您特别只想使用 start_of_day 函数,则应按照文档进行操作。
Installation with npm使用 npm 安装
npm install date-fns --save
or with yarn:或用纱线:
yarn add date-fns
Example usage:用法示例:
// option 1
var startOfDay = require('date-fns/start_of_day');
// option 2
// import * as startOfDay from 'date-fns/startOfDay';
// option 3 (only 2.0.0-alpha.25 or higher)
// import { startOfDay } from 'date-fns';
// The start of a day for 2 September 2014 11:55:00:
var result = startOfDay(new Date(2014, 8, 2, 11, 55, 0));
//=> Tue Sep 02 2014 00:00:00
Options 1 and 2 should allow most build tools to easily only include what you actually use, thus making your final bundle smaller and thus your web app load slightly faster.选项 1 和 2 应该允许大多数构建工具轻松地只包含您实际使用的内容,从而使您的最终包更小,从而使您的 Web 应用程序加载速度稍快。 Webpack 4 and possibly other build tools that look at the
sideffect
flag added to the library will also be able to treeshake option 3, further given that you're using 2.0.0-alpha.25 or higher.如果您使用的是 2.0.0-alpha.25 或更高版本,Webpack 4 和可能查看添加到库中的
sideffect
标志的其他构建工具也将能够进行 treeshake 选项 3。
Date-fns general modularity is currently the main advantage I see "date-fns" have compared to the more popular and established "moment".与更流行和成熟的“时刻”相比,Date-fns 的通用模块化是目前我认为“date-fns”的主要优势。 Though the author also explains other advantages in a github issue .
尽管作者还在github 问题中解释了其他优点。
Both of the mentioned working solutions simply fetch the entirety of the "date-fns" library and as such is still a valid syntax but labeling the variable as "startOfDay" gives the impression that the only part actually loaded or used is this function.两个提到的工作解决方案只是简单地获取整个“date-fns”库,因此仍然是一个有效的语法,但将变量标记为“startOfDay”给人的印象是实际加载或使用的唯一部分是这个函数。 When all of the other functions are also loaded in vain.
当所有其他功能也都加载无效时。 Where "start_of_day" is just one of many functions loaded on the same object.
其中“start_of_day”只是加载在同一对象上的众多函数之一。
So if you really want to load all of the functions you should instead call the variable "date-fns".所以如果你真的想加载所有的函数,你应该调用变量“date-fns”。 However given the ease and benefits I don't see why you wouldn't just follow the documentation and import individual functions.
但是,考虑到易用性和好处,我不明白为什么您不只是遵循文档并导入单个函数。
EDIT: With TypeScript 2+ you will get the type definitions directly from the library (version 1.23+).编辑:使用 TypeScript 2+,您将直接从库(1.23+ 版)中获取类型定义。
Earlier versions (version 1.22.x and lower) should instead follow Edwin Kato's advice on installing the type definitions for "date-fns".早期版本(1.22.x 及更低版本)应该遵循 Edwin Kato 关于安装“date-fns”类型定义的建议。 You should however also save it to your local dev-dependencies in "package.json".
但是,您还应该将其保存到“package.json”中的本地开发依赖项中。 You will need to explicitly state this when using npm.
使用 npm 时,您需要明确说明这一点。 While with yarn you need to state that it's a dev-dependency and not a regular dependency.
虽然使用 yarn 您需要声明它是一个开发依赖项而不是常规依赖项。
Thus you should install it like this with npm:因此,您应该像这样使用 npm 安装它:
npm install @types/date-fns --save-dev
or with yarn:或用纱线:
yarn add @types/date-fns --dev
Should I say instead我应该说
import * as startOfDay from 'date-fns/startOfDay';
Seems I have been doing similar things with other libraries in Angular Typescript code.似乎我一直在用 Angular Typescript 代码中的其他库做类似的事情。
我觉得这是一个更好的方法
import {differenceInCalendarDays, parseISO } from 'date-fns';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.