首页 > 解决方案 > 如何修复“拒绝执行脚本,因为它的 MIME 类型 ('text/html') 不可执行,并且启用了严格的 MIME 类型检查。” 反应?

问题描述

我的 React 应用程序不会从我的本地文件夹中加载以下脚本。我将 webpack 更新到 4.0 版,每次尝试加载脚本时,都会出现以下错误:

拒绝执行来自 ' http://localhost:8080/components/homepage/texting.js ' 的脚本,因为它的 MIME 类型 ('text/html') 不可执行,并且启用了严格的 MIME 类型检查。

HTML:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>My React App</title>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>

<body style="margin:0">

  <div id="app"></div>
  <script type="application/javascript" src="./components/homepage/texting.js"></script>

</body>

网络包配置:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.join(__dirname, '/dist'),
    filename: 'index_bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        },
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
{
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {}
          }
        ]
    },

    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    })
  ]
};

标签: htmlreactjswebpack

解决方案


没关系,我解决了!我刚刚将所有 JavaScript 移到 ComponentDidMount()


推荐阅读