Typescript import returns “Cannot find module”

后端 未结 3 417
离开以前
离开以前 2021-01-27 09:02

I\'m trying to import a module as import Something from \"@module\", but it returns

Cannot find module \'@config\'

\"@m

相关标签:
3条回答
  • 2021-01-27 09:42

    "paths" should be in "compilerOptions" section, not in root of tsconfig.json

    More info on path mapping here

    Overview of tsconfig.json here

    0 讨论(0)
  • 2021-01-27 09:49

    I had a similar issue with "moment" I have import like below and it does work.

    import * as moment from 'moment';
    
    0 讨论(0)
  • 2021-01-27 09:55

    Note that the import will seem fine while developing by importing moment this way, but you may get an AOT error when doing a prod compile, something like, "can not use reference" or some such like that. To not elaborate over much, this has to do (as I understand it) with some CommonJS modules. Moment is very popular, so it seems to come up quite a bit.

    To get around that one (which I ran into a couple of weeks ago), set "moment" to a const:

    import * as moment from 'moment':
    
    (later where you declare consts)
    
    const _moment = moment;
    

    And then you use _moment in your code.

    0 讨论(0)
提交回复
热议问题