webpack / babel / react - 意外令牌错误

时间:2016-05-27 15:38:36

标签: javascript reactjs webpack babeljs

我已经在使用babel-preset-reactbabel-preset-es2015

的package.json

 {
      "name": "react-spring-demo",
      "version": "1.0.0",
      "description": "",
      "main": "webpack.config.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "babel-core": "^6.9.0",
        "babel-loader": "^6.2.4",
        "babel-preset-es2015": "^6.9.0",
        "babel-preset-react": "^6.5.0",
        "webpack": "^1.13.1"
      },
      "dependencies": {
        "react": "^15.1.0"
      }
    }

webpack.config.js

    module.exports = {
  entry: [
      './app/app.js'
  ],
  module: {
    loaders: [
      { test: /\.jsx?$/, include: __dirname + '/app', loader: "babel", query: { presets: ['es2015', 'react'] } }
    ]
  },
  output: {
    filename: 'bundle.js',
    path: __dirname + '/dist'
  }
}

.babelrc

{ "presets": ["es2015", "react"] }

我想我错过了一些非常简单的事情。我已经尝试了一些使用./app代替__dirname + "/app"并使用.js$代替.jsx?$的内容。此外,您会注意到我使用了所有依赖项的最新版本。由于缺少反应预设,我已经阅读了许多关于人们在更新到babel 6时遇到问题的其他帖子。这似乎有所不同。难道这些版本不应该有问题吗?感谢

1 个答案:

答案 0 :(得分:3)

@Richard

ERROR in ./app/app.js
Module parse failed: C:\Users\vdixit\Desktop\react-spring-demo\src\main\resources\static\app\app.js Unexpected token (6:6)
You may need an appropriate loader to handle this file type.

好吧,我找到了解决办法。显然使用npm path模块可以解决这个问题。

而不是:

include: __dirname + '/app'

我用过:

  include: path.join(__dirname, '/app'),

谢谢!

编辑:我在这个问题上为其他人使用Windows - 但我在某处读到这个修复程序也适用于mac