简体   繁体   English

找不到模块date-fns

[英]Cannot find module date-fns

I am making a nodejs project using Typescript. 我正在使用Typescript制作一个nodejs项目。 For my datetime manipulations, I decided to use date-fns. 对于日期时间操作,我决定使用date-fns。 In their documentation, they say that typings for the library are always updated in each relase. 他们在文档中说,每次发布时,库的类型总是更新的。 However, I can't seem to be able to import the library itself: 但是,我似乎无法导入库本身:

import * as dateFns from 'date-fns';

It logs a Cannot find module 'date-fns' error. 它记录一个Cannot find module 'date-fns'错误。

I don't think I have any "special" configuration. 我认为我没有任何“特殊”配置。 I just install the library with npm instll --save date-fns . 我只是使用npm instll --save date-fns安装该库。 Here is my tsconfig.json : 这是我的tsconfig.json

{
  "compilerOptions": {
    "module": "es6",
    "sourceMap": true,
    "noImplicitAny": false,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

I used typescript only in Angular before. 我以前只在Angular中使用过打字稿。 So, I might not be adept to setup a typescript environment from scratch. 因此,我可能不擅长从头开始设置打字稿环境。

When the module option is set to es6 , the moduleResolution option defaults to classic , a module resolution strategy that doesn't look in node_modules . module选项设置为es6moduleResolution选项默认为classic ,一个模块的解决策略是不看node_modules If you truly want to use "module": "es6" , you'll need to set "moduleResolution": "node" . 如果您确实要使用"module": "es6" ,则需要设置"moduleResolution": "node" See this handbook page for more information. 有关更多信息,请参见此手册页面 But if you are planning to run this code on node.js, you probably want to just set "module": "commonjs" unless you are using a separate tool to convert the ES6 modules to CommonJS. 但是,如果您打算在node.js上运行此代码,则可能只想设置"module": "commonjs"除非您使用单独的工具将ES6模块转换为CommonJS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM