代码之家  ›  专栏  ›  技术社区  ›  user1059939

为什么TypeScript不能识别已安装的TypeScript节点模块?

  •  1
  • user1059939  · 技术社区  · 6 年前

    node_modules . 这不是一个 @types 问题。我正在尝试使用已经有类型的TypeScript NPM库。

    字体很棒就是一个很好的例子。我跟着导游走。

    npm install @fortawesome/fontawesome-svg-core --save

    import { library, dom } from '@fortawesome/fontawesome-svg-core';
    

    TypeScript告诉我这不是一个模块。

    我总是要走这样肮脏的路:

    import { library, dom } from '../node_modules/@fortawesome/fontawesome-svg-core/index';
    

    这是我的 tsconfig

    {
        "compileOnSave": true,
        "compilerOptions": {
            "sourceMap": true,
            "allowJs": false,
            "allowUnreachableCode": false,
            "allowUnusedLabels": false,
            "declaration": false,
            "declarationMap": false,
            "diagnostics": true,
            "removeComments": true,
            "module": "es6",
            "target": "es5",
            "strict": false
        },
        "include": [
            "src/**/*"
        ]
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Explosion Pills    6 年前

    "module": "es6" 设置ES6模块生成。如果你的 module 不是 node ,TypeScript将默认为 Classic 用于模块分辨率。请参见: https://www.typescriptlang.org/docs/handbook/compiler-options.html

    https://www.typescriptlang.org/docs/handbook/module-resolution.html --解释了经典和节点模块解析的工作原理。你想要什么 "moduleResolution": "node" node_modules 你想要的方式。

    你其实不需要 模块 自从 target: ES5 模块 默认为 es6 target