找不到模块(Typescript和baseUrl选项)

时间:2019-03-27 19:39:34

标签: reactjs typescript webpack module

我正在尝试在我的打字稿项目中使用自定义路径,问题是webpack无法识别我的模块,从而给我“ webpackMissingModule” 错误。

我尝试了不同的解决方案,但对我没有用。有什么建议吗?想法?

某些软件包

"react": "^16.8.4",
"react-dom": "^16.8.4",
"typescript": "3.3.1",

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": true,
    "noEmit": true,
    "jsx": "preserve"
  },
  "include": ["./src"],
  "exclude": ["node_modules", "build"],
  "extends": "./paths.json"
}

paths.json

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@src/*": ["*"],
      "@components/*": ["components/*"]
    }
  }
}

.env

NODE_PATH=./

组件

import React from "react";
import Hello from "@components/Hello";

const Home = () => (
  <div>
    <h1>Home</h1>
    <Hello />
  </div>
);

export default Home;

1 个答案:

答案 0 :(得分:1)

好吧,我发现了对我有用的东西,这叫做 tsconfig-paths-webpack-plugin。您所需要做的就是将其导入您的webpack配置中,并与tsconfig.json连接,仅此而已。