获取“语法错误:从头配置Webpack后,标记”“无效或意外

flmtquvp  于 2023-01-26  发布在  Webpack
关注(0)|答案(1)|浏览(303)

我试着在this website的帮助下引导一个react项目。我用react,typescript,babel,sass和prettier复制了这个项目,之后我在控制台运行npm i下载了所有的依赖项。我试着用webpack serve --hot --mode development在热模块重载模式下运行webpack,但是得到了一个错误消息:

[webpack-cli] /mnt/c/Users/[User]/Desktop/junior-to-senior-lessons/wp-autogenerated/node_modules/webpack-dev-server/lib/getPort.js:13
const maxPort = 65_535;
                ^^
SyntaxError: Invalid or unexpected token
    at Module._compile (internal/modules/cjs/loader.js:723:23)...

自动生成的webpack文件如下所示:

const webpack = require('webpack');
const path = require('path');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');

const config = {
  entry: [
    'react-hot-loader/patch',
    './src/index.tsx'
  ],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1
            }
          },
          'postcss-loader'
        ]
      },
      {
        test: /\.ts(x)?$/,
        loader: 'ts-loader',
        exclude: /node_modules/
      },
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader'
        ]
      },
      {
        test: /\.svg$/,
        use: 'file-loader'
      },
      {
        test: /\.png$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              mimetype: 'image/png'
            }
          }
        ]
      }
    ]
  },
  devServer: {
    'static': {
      directory: './dist'
    }
  },
  resolve: {
    extensions: [
      '.tsx',
      '.ts',
      '.js'
    ],
    alias: {
      'react-dom': '@hot-loader/react-dom'
    }
  },
  plugins: [
    new LodashModuleReplacementPlugin
  ]
};

module.exports = config;

自动生成的包.json:

{
  "name": "wp-complete",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "scripts": {
    "clean": "rm dist/bundle.js",
    "build-dev": "webpack --mode development",
    "build-prod": "webpack --mode production",
    "start": "webpack serve --hot --mode development",
    "test": "jest"
  },
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-hot-loader": "^4.13.0",
    "@material-ui/core": "^4.12.4",
    "fontsource-roboto": "^4.0.0",
    "@material-ui/icons": "^4.11.3",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "webpack": "^5.73.0",
    "webpack-cli": "^4.10.0",
    "babel-loader": "^8.2.5",
    "@babel/core": "^7.18.5",
    "@babel/preset-env": "^7.18.2",
    "@hot-loader/react-dom": "^17.0.2+4.13.0",
    "css-loader": "^6.7.1",
    "style-loader": "^3.3.1",
    "postcss-loader": "^7.0.0",
    "autoprefixer": "^10.4.7",
    "@types/react": "^18.0.14",
    "@types/react-dom": "^18.0.5",
    "@babel/preset-react": "^7.17.12",
    "webpack-dev-server": "^4.9.2",
    "jest": "^28.1.1",
    "babel-jest": "^28.1.1",
    "typescript": "^4.7.4",
    "ts-loader": "^9.3.1",
    "sass-loader": "^13.0.0",
    "node-sass": "^7.0.1",
    "file-loader": "^6.2.0",
    "url-loader": "^4.1.1",
    "lodash-webpack-plugin": "^0.11.6",
    "prettier": "^2.7.1"
  }
}
ddrv8njm

ddrv8njm1#

基于webpack-dev-server的此问题:https://github.com/webpack/webpack-dev-server/issues/4526
您的节点版本较旧,请更新它,您的问题将通过webpack-dev-server解决

相关问题