【问题标题】:TypeScript with classic moduleResolution work incorrect带有经典模块的 TypeScriptResolution 工作不正确
【发布时间】:2020-05-17 02:50:08
【问题描述】:

我从 https://zhongsp.gitbook.io/typescript-handbook/handbook/module-resolution 那里学到了 moduleResolution=classic

打字稿编译器按此顺序查找模块

  1. /root/src/folder/moduleB.ts

  2. /root/src/folder/moduleB.d.ts

  3. /root/src/moduleB.ts

  4. /root/src/moduleB.d.ts

  5. /root/moduleB.ts

  6. /root/moduleB.d.ts

  7. /moduleB.ts

  8. /moduleB.d.ts

但我发现当我使用时

import {a} from "a"

显示找不到模块,

我的文件结构是

|--main.ts

|--a.ts

|--node_modules

仅当我将 a.ts 放入文件夹 node_modules

时才有效

是不再支持 moduleResolution=classic 还是我的配置有问题?

【问题讨论】:

    标签: javascript typescript


    【解决方案1】:

    TypeScript Documentation 中所述:

    经典模块解析策略:这曾经是 TypeScript 的默认解析策略。如今,这种策略主要是为了向后兼容。

    我不确切知道您的模块的外观,但我假设您的 a.ts 文件中有类似的内容:

    export default a;
    

    如果是这样,那么您应该能够像这样导入您的模块(还要注意当前文件夹的路径以“./”开头):

    import a from "./a"
    

    但如果您的 a.ts 看起来更像这样(没有导出默认值):

    export const a....
    

    你需要像这样导入它:

    import { a } from "./a"
    

    查看this post 了解有关模块导入的详细信息

    【讨论】:

    • 我知道我可以使用相对路径导入它,但我不知道为什么非相对路径不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多