Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如果我在tsconfig中使用自定义路径,则Webpack Dev Server无法找到带有Typescript的模块_Reactjs_Typescript_Webpack - Fatal编程技术网

Reactjs 如果我在tsconfig中使用自定义路径,则Webpack Dev Server无法找到带有Typescript的模块

Reactjs 如果我在tsconfig中使用自定义路径,则Webpack Dev Server无法找到带有Typescript的模块,reactjs,typescript,webpack,Reactjs,Typescript,Webpack,我正在React+TypeScript上从头开始构建一个项目,并使用Webpack开发服务器。 我想使用组件的相对路径,这对于更灵活的开发来说并不严格 在我的App.tsx中,我正在尝试导入组件: import EntityTypes from "components/EntityTypes/EntityTypes"; 并且得到了一个错误 Module not found: Error: Can't resolve 'components/EntityTypes/EntityTypes' in

我正在React+TypeScript上从头开始构建一个项目,并使用Webpack开发服务器。 我想使用组件的相对路径,这对于更灵活的开发来说并不严格

在我的App.tsx中,我正在尝试导入组件:

import EntityTypes from "components/EntityTypes/EntityTypes";
并且得到了一个错误

Module not found: Error: Can't resolve 'components/EntityTypes/EntityTypes' in '/home/eugene/prj/work/crud-react/src'
此路径下的文件存在(/src/components/EntityTypes/EntityTypes.js)并导出类,如

export default EntityTypes;
My tsconfig.json:

{
  "compilerOptions": {
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "es5",
    "lib": [
      "es2015",
      "es2017",
      "dom"
    ],
    "removeComments": true,
    "allowSyntheticDefaultImports": false,
    "jsx": "react",
    "allowJs": true,
    "baseUrl": ".",
    "paths": {
      "components/*": [
        "src/components/*"
      ]
    }
  }
}
webpack.config.js:

const HtmlWebPackPlugin = require( 'html-webpack-plugin' );
const path = require( 'path' );
module.exports = {
    context: __dirname,
    entry: './src/index.js',
    resolve: {
        extensions: ['.ts', '.tsx', '.js', '.jsx']
    },
    output: {
        path: path.resolve( __dirname, 'dist' ),
        filename: 'main.js',
        publicPath: '/',
    },
    devServer: {
        historyApiFallback: true
    },
    module: {
        rules: [
            {
                test: /\.(tsx|ts)?$/,
                loader: 'awesome-typescript-loader'
            },
            {
                test: /\.js$/,
                use: 'babel-loader',
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            },
            {
                test: /\.(png|j?g|svg|gif)?$/,
                use: 'file-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: path.resolve( __dirname, 'public/index.html' ),
            filename: 'index.html'
        })
    ]
};

我已经检查了关于“路径”的typescript文档,该文件存在,我不明白问题出在哪里。

您使用的是绝对导入,而不是相对导入。尝试从“/components/EntityTypes/EntityTypes”导入EntityTypes,假设您要将其导入的文件与
组件
目录处于同一级别。

我已整理出如何使用文件路径并使其灵活,这样我可以轻松地重新组织项目和管理路径,而无需对严格的相对路径进行大量更改

问题与网页包的配置有关。 下一步是最终配置:

tsconfig.json

{
  "compilerOptions": {
    // ...
    "paths": {
      "~/components/*": [
        "./src/components/*"
      ],
      "~/services/*": [
        "./src/services/*"
      ],
      "~/interfaces/*": [
        "./src/interfaces/*"
      ]
    },
  }
}
webpack.config.js

const path = require('path');
module.exports = {
  // ...
  resolve: {
    // ...
    alias: {
      '~/components': path.resolve(process.cwd(), './src/components'),
      '~/interfaces': path.resolve(process.cwd(), './src/interfaces'),
      '~/services': path.resolve(process.cwd(), './src/services'),
    }
  },
}
用法示例

import {IEntities} from "~/interfaces/entities.interface";
// ...
import EntityForm from "~/components/EntityForm/EntityForm";

如果我这样做,这意味着它只是文件的相对导入?在这种情况下,配置的意义是什么?我的目标是,我可以更改项目的结构,移动一些文件夹,但不编辑每个文件中的导入,只更新tsconfig.json中的路径。我能实现我在最初的帖子中所描述的吗?