【问题标题】:Typescript "cannot find module <module>" error using ES6 modules使用 ES6 模块的打字稿“找不到模块 <模块>”错误
【发布时间】:2017-11-05 01:32:12
【问题描述】:

我在src/shared/components/A.tsx 中有一个文件,它执行import B from 'components/B' (src/shared/components/B.tsx)

Typescript 给我一个错误cannot find module 'components/B'

这是我在根目录中的tsconfig.json 文件:

{
  "compilerOptions": {
      "outDir": "./dist/",
      "sourceMap": true,
      "module": "es6",
      "target": "es6",
      "jsx": "react",
      "allowJs": true,
      "moduleResolution": "node"
  },
  "include": [
      "src/**/*"
  ],
  "exclude": [
      "node_modules"
  ],
  "baseUrl": ".",
  "paths": {
      "*": [
        "*",
        "src/shared/*" //this doesn't seem to work
      ]
  }
}

我对@9​​87654321@的理解是,它会先尝试找到不存在的./components/B,然后进入文件存在的&lt;baseUrl&gt;/src/shared/components/B,所以我不知道为什么我会仍然看到错误。

【问题讨论】:

  • 出于好奇 - 如果您将 .jsx 文件重命名为 .js 文件,是否可行?如果你显式地以.jsx 作为后缀导入,会解决它吗?
  • 在我移植到 typescript 之前它工作正常,如果我更改文件扩展名 Typescript 不会在文件中抛出错误。但是,模块 B 的代码中有错误会阻止它编译... typescript 是否尝试编译导入到文件中的模块?

标签: reactjs typescript


【解决方案1】:

这很愚蠢,paths / baseUrl 属于 compilerOptions

【讨论】:

    猜你喜欢
    • 2021-01-07
    • 2013-12-30
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多