在npm开始反应期间意外的令牌导入

时间:2017-03-03 09:19:24

标签: reactjs

我正在学习反应并创建了一个模拟项目但是当我运行项目时出现以下错误,我的webpack.config.js文件代码如下,



module.exports = {
  entry: './client.js',
  output: {
    filename: 'bundle.js',
    path: 'public'
  },
  module: {
    loaders: [{
      test: /\.jsx?$/,
      exclude: /node_modules/,
      loader: 'babel-loader',
      query: {
        presets: ['es2015', 'react']
      }
    }]
  }
};




我的package.json文件,



{
  "name": "universal-react",
  "version": "0.0.0",
  "description": "Universal React web application.",
  "scripts": {
    "start": "webpack && node server.js",
    "dev": "npm-run-all --parallel watch:*",
    "watch:webpack": "webpack -w",
    "watch:server": "nodemon --ext js,jsx --ignore public/ server.js"
  },
  "main": "server.js",
  "keywords": [
    "universal",
    "react"
  ],
  "dependencies": {
    "babel-core": "^6.9.0",
    "babel-loader": "^6.2.4",
    "babel-preset-react": "^6.5.0",
    "babel-register": "^6.9.0",
    "express": "^4.13.4",
    "marked": "^0.3.6",
    "react": "^15.0.2",
    "react-dom": "^15.0.2",
    "react-redux": "^4.4.6",
    "react-router": "^2.8.1",
    "redux": "^3.6.0",
    "webpack": "^1.13.1"
  },
  "devDependencies": {
    "nodemon": "^1.11.0",
    "npm-run-all": "^3.1.2"
  }
}



 错误信息如下,



/home/user/Documents/test/src/routes/index.js:1
(function (exports, require, module, __filename, __dirname) { import React from 'react';
                                                              ^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at loader (/home/user/Documents/src/node_modules/babel-register/lib/node.js:144:5)




有人能建议我在哪里错吗?

1 个答案:

答案 0 :(得分:0)

您尚未根据package.json安装 babel-preset-es2015

mtcars

尝试通过

安装
   "babel-preset-es2015": "^6.9.0",

同时添加

npm install --save babel-preset-es2015
项目根目录中的

"babel": "^6.5.2", "babel-cli": "^6.9.0" 文件与.babelrc平行 以下内容。

package.json

然后再次运行npm install然后再运行webpack。