Babel编译导致:语法错误:意外的令牌(38:4)

时间:2019-03-22 16:35:44

标签: javascript node.js reactjs

我是Node and React的新手,尝试在Babel中进行构建以将serverjs上载并对React客户端进行响应时遇到了此错误。

在Postman中一切正常,但无法将其上传到Heroku。在网上搜索后,我发现必须首先使用Babel进行编译。

错误消息是:

SyntaxError: client/node_modules/@babel/helper-plugin-utils/src/index.js: Unexpected token (38:4)
  36 | 
  37 |   return {
> 38 |     ...proto,
     |     ^
  39 |     ...api,
  40 |   };
  41 | }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Baraa\AppData\Roaming\npm-cache\_logs\2019-03-21T16_14_47_767Z-debug.log

我的webpack.config.js文件包含:

module.exports = {
  entry: "./client/src/index.js",
  output: {
    path: __dirname + "/public",
    filename: "bundle.js"
  },
  module: {
    test: /\.jsx?$/,
    exclude: /node_modules/,
    use: [
      {
        loader: "babel-loader",
        options: {
          presets: ["react"]
        }
      }
    ]
  },
  devServer: {
    proxy: {
      "/name": "http://localhost:3011"
    }
  }
};



package.json
{
  "name": "app04",
  "version": "1.0.0",
  "engines": {
    "node": "11.8.0"
  },
  "description": "app 04",
  "main": "server.js",
  "scripts": {
    "build-babel": "babel -d ./build ./ -s",
    "build": "npm run build-babel",
    "start": "npm run build && node ./build/server.js"
  },
  "author": "Baraa Rashad",
  "dependencies": {
    "accuweather": "^1.0.1",
    "axios": "^0.18.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "body-parser": "^1.18.3",
    "bootstrap": "^3.4.1",
    "cors": "^2.8.5",
    "crypto": "^1.0.1",
    "crypto-js": "^3.1.9-1",
    "ejs": "^2.5.2",
    "express": "^4.14.0",
    "http": "0.0.0",
    "nvm": "0.0.4",
    "react": "^15.3.2",
    "react-dom": "^15.3.2",
    "react-json-pretty": "^2.0.0",
    "react-router-config": "^4.4.0-beta.6",
    "react-router-dom": "^4.3.1",
    "reactstrap": "^7.1.0"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-stage-2": "^6.17.0",
    "babel-register": "^6.26.0",
    "nodemon": "^1.18.10"
  }
}



.babelrc
{
  "presets": ["env", "es2015", "react"],
  "plugins": ["babel-plugin-transform-class-properties"]
}

1 个答案:

答案 0 :(得分:0)

我通过如下调整package.josn找到了解决方案。

lookup