【问题标题】:Cannot find module date-fns找不到模块 date-fns
【发布时间】:2019-03-12 07:05:09
【问题描述】:

我正在使用 Typescript 制作一个 nodejs 项目。对于我的日期时间操作,我决定使用 date-fns。在他们的文档中,他们说库的类型总是在每个版本中更新。但是,我似乎无法导入库本身:

import * as dateFns from 'date-fns';

它记录了一个Cannot find module 'date-fns' 错误。

我认为我没有任何“特殊”配置。我只是用npm instll --save date-fns 安装库。这是我的tsconfig.json

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

我之前只在 Angular 中使用过打字稿。所以,我可能不擅长从头开始设置 typescript 环境。

【问题讨论】:

    标签: node.js typescript date-fns


    【解决方案1】:

    module 选项设置为es6 时,moduleResolution 选项默认为classic,这是一种不会在node_modules 中查找的模块解析策略。如果你真的想使用"module": "es6",你需要设置"moduleResolution": "node"。请参阅this handbook page 了解更多信息。但如果您打算在 node.js 上运行此代码,您可能只想设置 "module": "commonjs",除非您使用单独的工具将 ES6 模块转换为 CommonJS。

    【讨论】:

      猜你喜欢
      • 2020-09-01
      • 1970-01-01
      • 2019-12-13
      • 2021-10-14
      • 2017-03-30
      • 1970-01-01
      • 2021-12-07
      • 2019-05-06
      • 2020-06-17
      相关资源
      最近更新 更多