【问题标题】:Webpack/babel Unexpected token, expected ";"Webpack/babel 意外的令牌,应为“;”
【发布时间】:2019-09-16 07:01:29
【问题描述】:

我正在尝试导入 JSON 文件,但是当我尝试加载我的 JSON 文件时,webpack 给了我一个错误。

./src/testData.json 中的错误 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js): SyntaxError: C:\Users\Alex\Documents\Work\test-app\learn-fullstack-javascript\src\testData.json: Unexpected token, expected ";" (3:12)

我已尝试更改我的 babel 和 webpack 配置文件,但无法解决问题。

这是我的配置文件: webpack.config.js

module.exports = {
    entry: './src/index.js',
    output: {
      path: __dirname + '/public',
      filename: 'bundle.js'
    },
    module: {
      rules: [
        {
          test: /\.(js|jsx|json)$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
            options: {
              presets:['@babel/preset-env']
            }
          }
        }
      ]
    }
  };

babel.config.js

module.exports = {
    presets: ['@babel/preset-env', '@babel/react'],
};

.babelrc

{
  "presets": [
      "@babel/preset-env",
      "@babel/react"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}

最后是我的 package.json

{
  "name": "learjs",
  "version": "1.0.0",
  "description": "For an up-to-date configuration guide: [jscomplete.com/reactful](https://jscomplete.com/reactful)",
  "main": "index.js",
  "scripts": {
    "start": "nodemon --exec babel-node server.js --ignore public/",
    "dev": "webpack-dev-server"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/jscomplete/learn-fullstack-javascript.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/jscomplete/learn-fullstack-javascript/issues"
  },
  "homepage": "https://github.com/jscomplete/learn-fullstack-javascript#readme",
  "dependencies": {
    "@babel-preset-node7": "^1.5.0",
    "@babel/register": "^7.4.0",
    "braces": "^2.3.2",
    "ejs": "^2.6.1",
    "express": "^4.16.4",
    "mongodb": "^3.2.3",
    "prop-types": "^15.7.2",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "webpack-dev-server": "^3.3.1"
  },
  "devDependencies": {
    "@babel-core": "^7.0.0-bridge.0",
    "@babel-eslint": "^10.0.1",
    "@babel-jest": "^24.7.1",
    "@babel-loader": "^8.0.0-beta.6",
    "@babel/cli": "^7.4.3",
    "@babel/core": "^7.4.3",
    "@babel/node": "^7.0.0",
    "@babel/plugin-proposal-class-properties": "^7.4.0",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/preset-env": "^7.4.3",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-loader": "^8.0.5",
    "eslint": "^5.16.0",
    "eslint-plugin-react": "^7.12.4",
    "html-webpack-plugin": "^3.2.0",
    "json-loader": "^0.5.7",
    "nodemon": "^1.18.11",
    "regenerator-runtime": "^0.13.2",
    "webpack": "^4.30.0",
    "webpack-cli": "^3.3.0"
  },
  "resolutions": {
    "babel-core": "7.0.0-bridge.0"
  }
}

{
  "contests": [
    {
      "id": 1,
      "categoryName": "Business/Company",
      "contestName": "Cognitive Building Bricks"
    },
    {
      "id": 2,
      "categoryName": "Magazine/Newsletter",
      "contestName": "Educating people about sustainable food production"
    },
    {
      "id": 3,
      "categoryName": "Software Component",
      "contestName": "Big Data Analytics for Cash Circulation"
    },
    {
      "id": 4,
      "categoryName": "Website",
      "contestName": "Free programming books"
    }
  ]
}

我期待在 chrome 开发工具中得到类似的东西。

Object i
  contents: Array[4]
  > 0: Object
  > .......

【问题讨论】:

  • 您遗漏了我们最需要查看的文件:testData.json,错误消息具体指向的文件。 :-) 显然它有一个;。它不应该(在字符串之外),JSON 不使用 ; 做任何事情。
  • 天哪,我忽略了 ;/ ,刚刚将 json 文件添加到帖子中。

标签: node.js json webpack


【解决方案1】:

可能你只需要从 webpack.config.json 文件中的test: /\.(js|jsx|json)$/ 中删除 jsx 和 json

 rules: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      use: {
        loader: 'babel-loader',
        options: {
          presets:['@babel/preset-env']
        }
      }
    }
  ]    

【讨论】:

  • 请尝试解释您的答案,以便其他人可以从中学习。
  • 其实我也不知道为什么会这样。我有完全相同的问题,我是新来的反应。估计jsx和json有冲突,json文件不应该用babel-loader处理。
【解决方案2】:

尝试从你的 webpack 配置中取出 json。你不需要 babel-loader 来处理 json,你可以使用 json-loader 来处理

module.exports = {
    entry: './src/index.js',
    output: {
      path: __dirname + '/public',
      filename: 'bundle.js'
    },
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          use: {
            loader: 'babel-loader',
            options: {
              presets:['@babel/preset-env']
            }
          }
        },
        {
          test:  /\.json$/,
          exclude: /node_modules/,
          use: {
            // included by default (https://webpack.js.org/loaders/json-loader/)
            loader: 'json-loader' 
          }
        }
      ]
    }
  };

【讨论】:

  • 你是一个美丽的人!这很好用!非常感谢您的帮助!
  • 很高兴它成功了!如果你喜欢,你可以选择这个作为你的答案,我会加分。
  • 对我来说,从 webpack 配置中取出 json 就足够了。我不必使用json-loader。据我了解,Webpack 现在是开箱即用的。
猜你喜欢
  • 1970-01-01
  • 2016-07-10
  • 2017-12-10
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 2020-11-16
  • 2016-07-01
  • 1970-01-01
相关资源
最近更新 更多