使用intellij-idea从TypeScript中的根路径导入模块

时间:2017-08-10 04:01:13

标签: javascript node.js typescript intellij-idea

我知道这个话题非常受欢迎 我尝试阅读但仍然失败

这是我的tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "sourceMap": true,
    "declaration": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "rootDir": ".",
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "alwaysStrict": true,
    "locale": "zh-TW",
    "pretty": true,
    "paths": {
      "src/*": [
        "./src/*"
      ],
      "/root/src/*": [
        "./src/*"
      ]
    },
    "include": [
      "./src/**/*.ts"
    ],
    "exclude": [
      "node_modules"
    ],
    "newLine": "lf"
  }
}

这是我的目录结构

node_modules
src
  gw2taco
    index
test
  r

r.ts(全部失败)

  • import' src / gw2taco&#39 ;;的console.log(7777);
  • import' / src / gw2taco&#39 ;;的console.log(7777);

即时通讯使用intellij-idea

1 个答案:

答案 0 :(得分:0)

用于导入

鉴于此目录结构:

node_modules
src
  gw2taco
    index.ts
test
  r.ts

r.ts中导入的一个选项是:

import "../src/gw2taco";

对于tsconfig.json

  • 删除baseUrlrootDirpaths条目。
  • includeexclude属性放在compilerOptions之外。
  • 将您的测试目录添加到include
  • newLine属性大写为LF

文件结果如下所示:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "sourceMap": true,
    "declaration": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "alwaysStrict": true,
    "locale": "zh-TW",
    "pretty": true,
    "newLine": "LF"
  },
  "include": [
    "./src/**/*.ts",
    "./test/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

一起在VSCode中

enter image description here