Typescipt无法识别webpack resolve.alias

时间:2020-02-13 04:18:23

标签: javascript node.js typescript webpack

这是在我的Webpack配置中:

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

这是在我的tsconfig.json中:

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

这是代码层次结构:

solution

这是我在À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/*"],
  }
}

1 个答案:

答案 0 :(得分:0)

也许您不需要...

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

在您的代码中

import Users from 'users';

检查配置here

相关问题