【问题标题】:Typescript: set "node_modules" directory path打字稿:设置“node_modules”目录路径
【发布时间】:2019-06-14 21:54:02
【问题描述】:

如果该目录不在默认解析路径中,我如何为 Typescript 编译器的整个“node_modules”目录(不是单独的模块)设置/配置解析路径?

【问题讨论】:

  • 官方 TypeScript 文档中的“模块解析”可能会有所帮助:typescriptlang.org/docs/handbook/module-resolution.html,尤其是“路径映射”部分(带示例)
  • 我遇到了同样的问题。我正在使用纱线工作区,它将node_modules 放在不同的位置。使用 Lerna 的人也可能有这个问题。

标签: typescript node-modules tsconfig


【解决方案1】:

您可以更改 TypeScript 查找 node_modules 文件夹的位置,如下所述:https://www.typescriptlang.org/docs/handbook/module-resolution.html

设置 baseUrl 会通知编译器在哪里可以找到模块。所有模块 具有非相对名称的导入被假定为相对于 基本网址。

将以下内容添加到您的 tsconfig.json 文件中:

"compilerOptions": {
  "baseUrl": "<dir_containing_node_modules>"
}

遗憾的是,您似乎无法为 node_modules 文件夹指定不同的名称 - 尽管这种情况不太可能发生。如果我错了,请纠正我。

【讨论】:

    【解决方案2】:

    这是我在根目录中使用 Typescript 解决此问题的方法:

    我将 tsconfig.json 设置为:

    "compilerOptions": {
    
       "baseUrl": "./",
    
       "typeRoots": ["node_modules/@types"],
     }
    

    我希望这可以帮助某人补充以前的答案。

    【讨论】:

      猜你喜欢
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      相关资源
      最近更新 更多