簡體   English   中英

webpack / babel / react-意外的令牌錯誤

[英]webpack/babel/react - unexpected token error

我已經在使用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時遇到問題的人。 這似乎有所不同。 這些版本不應該沒有問題嗎? 謝謝

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM