【问题标题】:您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件。”
【发布时间】:2020-01-15 08:56:23
【问题描述】:

我正在使用 yarn 为我的 react 项目设置 webpack,但出现了这个错误:

./src/app.js 中的错误 67:6 模块解析失败:意外的令牌 (67:6) 您可能需要适当的加载程序来处理这种文件类型, 当前没有配置加载程序来处理此文件。看 https://webpack.js.org/concepts#loaders ℹ「wdm」:编译失败。

webpack.config.js

const path = require("path");

module.exports = {
    entry: "./src/app.js",
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js'
    },
    module:{
        rules:[{
            loader: 'babel-loader',
            test: '/\.(js|jsx)$/',
            exclude: /node_modules/
        }]
    },
    devtool: 'cheap-module-eval-source-map',
    devServer: {
        contentBase: path.join(__dirname, 'public')
    }
}
.babelrc
{
    "presets": ["env", "react","@babel/preset-env"],
    "plugins": [
        "transform-class-properties"
    ]
}

package.json

{
  "name": "react",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "serve": "live-server public/",
    "build": "webpack",
    "dev-server": "webpack-dev-server"
  },
  "dependencies": {
    "babel-cli": "6.24.1",
    "babel-core": "6.25.0",
    "babel-plugin-transform-class-properties": "6.24.1",
    "babel-preset-env": "1.5.2",
    "babel-preset-react": "6.24.1",
    "live-server": "^1.2.1",
    "react": "^16.9.0",
    "react-dom": "^16.9.0",
    "webpack": "^4.39.3",
    "webpack-dev-server": "^3.8.0"
  },
  "devDependencies": {
    "@babel/core": "^7.6.0",
    "babel-loader": "^8.0.6",
    "webpack-cli": "^3.3.8"
  }
}

【问题讨论】:

  • 你能附上app.js的来源吗?

标签: css reactjs


【解决方案1】:

所选答案缺少一些细节:

应该是test: /\.js|\.jsx$/

\: is an escape character 在这种情况下为.

|: is an Alternation / OR operand

$: is end of line

希望这对你有用。

来源: https://www.rexegg.com/regex-quickstart.html

【讨论】:

  • 同样加入jsx,报错..You may need an additional loader to handle the result of these loaders.
【解决方案2】:

将下面提到的代码放入 webpack 文件中

mix.extend(
    "graphql",
    new (class {
        dependencies() {
            return ["graphql", "graphql-tag"];
        }

        webpackRules() {
            return {
                test: /\.(graphql|gql)$/,
                exclude: /node_modules/,
                loader: "graphql-tag/loader"
            };
        }
    })()
);


mix.js("resources/js/app.js", "public/js").vue();

mix.graphql();

【讨论】:

  • 什么是混合?你能解释一下吗
  • 我想知道我们没有尝试在此处添加 graphql 加载器
【解决方案3】:

这个错误发生在我身上只是因为文件被放置在项目的父文件夹中(即项目文件夹之外)。

将文件移动到适当的文件夹修复它。

【讨论】:

    【解决方案4】:

    将 .babelrc 文件添加到 node_modules 文件夹所在的同一文件夹中,内容如下

    {
      "presets": ["@babel/preset-react"]
    }
    

    【讨论】:

    • 我已经有了这个文件,但是我得到了上述错误。
    • 这并不能解决问题。
    【解决方案5】:

    您使用了不必要的转义字符:这不是必需的。

    替换 test: '/\.(js|jsx)$/', test: /\.js$|jsx/, 应该可以正常工作。

    我在我的机器上复制了你的问题,发现上面的修复解决了同样的问题。

    希望这会有所帮助,祝您编码愉快!!!

    【讨论】:

    • 这将匹配包含字符串jsx 的所有内容,甚至在文件扩展名之外。 OP的代码很好,除了额外的单引号。
    猜你喜欢
    • 2021-08-11
    • 2021-11-21
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2021-12-11
    • 2020-06-22
    相关资源
    最近更新 更多