【问题标题】:Webpack loads in wrong from parent folderWebpack 从父文件夹加载错误
【发布时间】:2020-12-11 20:47:32
【问题描述】:

我试图在我的 Webpack 上运行 watch 命令以编译我的代码,但我的文件总是遇到问题。即使尝试使用一些 ../ 更改相对路径也不起作用

ERROR in code
Module not found: Error: Can't resolve '../src/code.ts' in '/Users/giardiv/Lines/f-variables'

ERROR in ui
Module not found: Error: Can't resolve '../src/ui.tsx' in '/Users/giardiv/Lines/f-variables'

ERROR in   Error: Child compilation failed:
  Module not found: Error: Can't resolve '/Users/giardiv/Lines/src/ui.html' in '  /Users/giardiv/Lines/f-variables':
  Error: Can't resolve '/Users/giardiv/Lines/src/ui.html' in '/Users/giardiv/Lin  es/f-variables'
  ModuleNotFoundError: Module not found: Error: Can't resolve '/Users/giardiv/Li  nes/src/ui.html' in '/Users/giardiv/Lines/f-variables'

虽然我的文件夹树很基本,但f-variablessrc,其他文件直接在其中

这是 webpack 配置

const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')

module.exports = (env, argv) => ({
  mode: argv.mode === 'production' ? 'production' : 'development',

  // This is necessary because Figma's 'eval' works differently than normal eval
  devtool: argv.mode === 'production' ? false : 'inline-source-map',

  entry: {
    ui: "../src/ui.tsx", // "./src/ui.tsx" calls the same error
    code: "../src/code.ts" // "./src/code.ts" calls the same error
  },

  module: {
    rules: [
      // Converts TypeScript code to JavaScript
      { test: /\.tsx?$/, use: 'ts-loader', exclude: /node_modules/ },

      // Enables including CSS by doing "import './file.css'" in your TypeScript code
      { test: /\.css$/, use: ['style-loader', { loader: 'css-loader' }] },

      // Allows you to use "<%= require('./file.svg') %>" in your HTML code to get a data URI
      { test: /\.(png|jpg|gif|webp|svg)$/, loader: 'url-loader' },
    ],
  },

  // Webpack tries these extensions for you if you omit the extension like "import './file'"
  resolve: { extensions: ['.tsx', '.ts', '.jsx', '.js'] },

  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'), // Compile into a folder called "dist"
    publicPath: '/',
  },

  // Tells Webpack to generate "ui.html" and to inline "ui.ts" into it
  plugins: [
    new HtmlWebpackPlugin({
      template: '../src/ui.html',
      filename: 'ui.html',
      inlineSource: '.(js)$',
      chunks: ['ui'],
    }),
    new HtmlWebpackInlineSourcePlugin(HtmlWebpackPlugin),
  ],
})

【问题讨论】:

    标签: javascript node.js reactjs webpack webpack-dev-server


    【解决方案1】:

    因为entry 中的路径在您的webpack.config.js 文件中是错误的。

    应该设置如下,因为你除了src目录之外还有webpack.config.js

    entry: {
        ui: "./src/ui.tsx",
        code: "./src/code.ts"
    },
    

    【讨论】:

    • 我忘记将其设置回初始值,但它也不适用于这些值。仍然是完全相同的错误。刚刚尝试在 src 文件夹中移动配置,但仍然遇到与路径相关的错误
    猜你喜欢
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    相关资源
    最近更新 更多