首页 > 解决方案 > 未找到模块(Typescript 和 baseUrl 选项)

问题描述

我正在尝试在我的 typescript 项目中使用自定义路径,问题是 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"
}

路径.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;

标签: reactjstypescriptwebpackmodule

解决方案


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


推荐阅读