【问题标题】:How to fix Module parse failed: Unexpected token?如何修复模块解析失败:意外的令牌?
【发布时间】:2020-06-23 05:30:34
【问题描述】:

当我运行npm run build 时,我得到了这个错误:

ERROR in ./src/client.js 7:2
Module parse failed: Unexpected token (7:2)
File was processed with these loaders:
 * ./node_modules/jshint-loader/index.js
You may need an additional loader to handle the result of these loaders.
| 
| const component = (
>   <Router history={browserHistory}>
|     {routes}
|   </Router>
 @ multi babel-polyfill ./src/client.js main[1]

./src/client.js(警告browserHistory -- 无法解析符号'browserHistory')

import React      from 'react';
import ReactDOM   from 'react-dom';
import { browserHistory, Router } from 'react-router';
import routes from './routes';

const component = (
  <Router history={browserHistory}>
    {routes}
  </Router>
);

ReactDOM.render(component, document.getElementById('react-view'));

我该如何解决?

反应:16.13.0

webpack:4.42.0

npm:6.14.2

webpack-config.js

global.Promise         = require('bluebird');
const webpack            = require('webpack');
const path               = require('path');

const plugins = [
  new webpack.DefinePlugin({
    'process.env': {
      BROWSER:  JSON.stringify(true),
      NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development')
    }
  })
];

module.exports = {
  entry: ['babel-polyfill', './src/client.js'],
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },

  module: {
    rules: [{
      test: /\.js$/, // include .js files
      enforce: "pre", // preload the jshint loader
      exclude: /node_modules/, // exclude any and all files in the node_modules folder
      use: [{
        loader: "jshint-loader",
        // more options in the optional jshint object
        options: {  // ⬅ formally jshint property
          camelcase: true,
          emitErrors: false,
          failOnHint: false
        }
      }]
    }]
  }
};

【问题讨论】:

  • 你的 webpack 配置是什么?
  • @tudor.gergely 我添加到帖子中,请查看

标签: reactjs npm webpack react-router node-modules


【解决方案1】:

您的 webpack 配置无法处理 React 的 JSX 语法。您需要使用一些加载器对其进行更新才能使其正常工作(这是一个教程:https://www.valentinog.com/blog/babel/

我建议您首先使用 create-react-app ,它将所有这些配置抽象出来:https://github.com/facebook/create-react-app。以这种方式开始反应更容易,您可以在准备好/需要时自定义它。

【讨论】:

    猜你喜欢
    • 2019-02-15
    • 2023-03-27
    • 2021-04-26
    • 2021-05-01
    • 2020-11-16
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多