【问题标题】:Typescipt fails to recognize webpack resolve.aliasTypescript 无法识别 webpack resolve.alias
【发布时间】:2020-05-28 17:20:48
【问题描述】:

这是在我的Webpack 配置中:

usersAlias: path.resolve(__dirname, '../src/pages/users'),

这是在我的tsconfig.json:

"baseUrl": ".",
"paths": {
  "usersAlias/*": ["src/pages/users/*"],
}

这是代码层次结构:

这是我在Àpp.js中的用法:

import Users from 'usersAlias/Users';

这是我得到的错误:

Cannot find module 'usersAlias/Users'.ts(2307)

如果我将 Àpp.tsx 更改为 App.js 一切正常!我的问题与TypeScript 相关。

更新

我的完整版tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": false,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ],
  "baseUrl": ".",
  "paths": {
    "usersAlias/*": ["src/pages/users/*"],
  }
}

【问题讨论】:

  • 还有一个src/pages/users/Users.ts? (在您的屏幕截图中没有展开)
  • @Sydney Y 当然是的

标签: javascript node.js typescript webpack


【解决方案1】:

也许你不需要...

module.exports = {
  //...
  resolve: {
    alias: {
      users: path.resolve(__dirname, '/src/pages/users'),
    }
  }
};

在你的代码中

import Users from 'users';

检查配置here

【讨论】:

  • 不工作,webpack 识别别名并且在我的示例中没有打字稿。我的问题与打字稿有关
  • 更新了,基本上是我弹出后的RCA tsconfig
  • 你是否已经配置了 tsx 和 ts 扩展以在 webpack 中解析?
  • 是的,这是 RCA 项目,我正在与 tsx 成功合作
  • 你能把你的代码分享到github上让我看看吗?
猜你喜欢
  • 1970-01-01
  • 2019-01-16
  • 2018-11-03
  • 2022-01-14
  • 2018-04-18
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多