简体   繁体   中英

Cannot find module date-fns

I am making a nodejs project using Typescript. For my datetime manipulations, I decided to use 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.

I don't think I have any "special" configuration. I just install the library with npm instll --save date-fns . Here is my tsconfig.json :

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

I used typescript only in Angular before. 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 . If you truly want to use "module": "es6" , you'll need to set "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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