当前位置: 首页 > 知识库问答 >
问题:

./src/index.js 14:4模块解析失败时出错:意外标记(14:4)您可能需要适当的加载程序来处理此文件类型

龚寂弦
2023-03-14

我是tryiyng运行与webpack反应。我得到以下错误:

./src/index.js 14:4模块解析失败时出错:意外标记(14:4)您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件。

| ReactDOM.render(
>     <HashRouter>
|         <App />
|     </HashRouter>,

webpack.config.js

module.exports = {
    entry: path.resolve(__dirname, 'src/App.js'),
    watch: true,
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
                loader: 'babel-loader',
            }
        }]
    }, 
}  

package.json

{
  "name": "app-weather",
  "version": "0.1.0",
  "private": true,
  "main": "index.js",
  "dependencies": {
    "bulma": "^0.7.5",
    "dotenv": "^8.0.0",
    "node-sass": "^4.12.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-router-dom": "^5.0.1",
    "react-scripts": "3.0.1"
  },
  "scripts": {
    "start": "webpack-dev-server",
    "build": "webpack",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.6",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^4.39.2",
    "webpack-cli": "^3.3.7",
    "webpack-dev-server": "^3.8.0"
  }
}

共有3个答案

刘海
2023-03-14

这意味着您的浏览器正在提供未传输或未转换的代码

当不知何故这些文件被转置时,这个问题就出现了,请确保这些文件夹包含在webpack模块部分中的有效加载器下,例如-用于打字-ts加载器等。花了很多时间修好了。

您可以使用另一个编译器(如babel)来实现同样的功能。什么适合你的需要取决于你。

 module: {
       rules: [
                    {
                        test: /\.ts|\.tsx?$/, loader: 'ts-loader', 
                        options: { onlyCompileBundledFiles: true, configFile: "testing.tsconfig.json", compilerOptions: {
                            noEmit: false, allowTsInNodeModules: false
                          }}, 
                        include: [
                            path.resolve(__dirname, "./src"),
                            path.resolve(__dirname, "./tests")
                          ],
                        exclude: [ 
                            path.resolve(__dirname, './automation/'),
                            path.resolve(__dirname, './src/types/testutils/'),
                            path.resolve(__dirname, './node_modules/')
                      ]
                    }
            ]
   }
景俊良
2023-03-14

修改babel加载程序配置:

  1. 将您的test条件更改为/\. jsx?$/,所以. jsx也将转寄。
  2. 添加选项与您的反应预设下(!)use(不像兄弟姐妹那样使用)
  3. 如果你想使用Babel6,那么你需要安装Babel-loader v7(npm i-Dbabel-loader@7),而不是用于Babel7的v8。

那你应该没事了。以下是适用于我的Babel 6(和7,如果您想更改)配置的相关部分:

网页包:

module.exports = {
  ...
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["react"]
          },
        }
      }
    ]
  }
};

package.json:

{
  ...
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1"
  },
}

网页包:

module.exports = {
  ...
  module: {
    rules: [
      {
        test: /\.jsx?$/, 
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-react"]
          }
        }
      }
    ]
  }
};

package.json:

{
  ...
  "devDependencies": {
    "@babel/core": "^7.5.5",
    "@babel/preset-env": "^7.5.5",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.6"
  }
}

希望有帮助。

邵赞
2023-03-14

您需要为react添加预设,以便通过babel加载,并添加对JSX文件的支持

module.exports = {
    entry: path.resolve(__dirname, 'src/App.js'),
    watch: true,
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [{
            test: /\.jsx?$/, // change here
            exclude: /(node_modules|bower_components)/,
            options: { // added this
                presets: ['react'],
            },
            use: {
                loader: 'babel-loader',
            }
        ]
    }, 
}  
 类似资料: