admin 管理员组文章数量: 1184232
webpack.config.js
module.exports = {
context: __dirname + "/app",
entry: {
javascript: "./app.js",
html: "./index.html",
},
resolve: {
extensions: ['', '.js', '.jsx']
},
output: {
filename: "app.js",
path: __dirname + "/dist",
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: "babel-loader",
},
{
test: /\.html$/,
loader: "file?name=[name].[ext]",
},
],
},
}
package.json
{
"name": "react-webpack-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel": "^6.0.15",
"babel-core": "^6.0.20",
"babel-loader": "^6.0.1",
"file-loader": "^0.8.4",
"webpack": "^1.12.2"
},
"dependencies": {
"react": "^0.14.2"
}
}
app/app.js
import React from "react";
import Greeting from "./greeting";
React.render(
<Greeting name="World"/>,
document.body
);
I have seen the exact same questions after searching around, but none of the answers seemed to apply to me. I am getting the following error when running webpack :
ERROR in ./app.js
Module build failed: SyntaxError: path/to/project/react-webpack-project/app/app.js: Unexpected token (5:2)
React.render(
<Greeting name="World"/>,
document.body
);
webpack.config.js
module.exports = {
context: __dirname + "/app",
entry: {
javascript: "./app.js",
html: "./index.html",
},
resolve: {
extensions: ['', '.js', '.jsx']
},
output: {
filename: "app.js",
path: __dirname + "/dist",
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: "babel-loader",
},
{
test: /\.html$/,
loader: "file?name=[name].[ext]",
},
],
},
}
package.json
{
"name": "react-webpack-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel": "^6.0.15",
"babel-core": "^6.0.20",
"babel-loader": "^6.0.1",
"file-loader": "^0.8.4",
"webpack": "^1.12.2"
},
"dependencies": {
"react": "^0.14.2"
}
}
app/app.js
import React from "react";
import Greeting from "./greeting";
React.render(
<Greeting name="World"/>,
document.body
);
I have seen the exact same questions after searching around, but none of the answers seemed to apply to me. I am getting the following error when running webpack :
ERROR in ./app.js
Module build failed: SyntaxError: path/to/project/react-webpack-project/app/app.js: Unexpected token (5:2)
React.render(
<Greeting name="World"/>,
document.body
);
I am not sure why I am getting this error still. I am guessing it has something to do with my webpack.config.js file, but not 100% what the problem is.
Share Improve this question asked Nov 3, 2015 at 21:51 httpNickhttpNick 2,6141 gold badge23 silver badges36 bronze badges 2- Did you try this solution ? link – The Reason Commented Nov 3, 2015 at 22:08
- I think the problem is that you have multiple loaders defined; checkout my answer below -- I think that using a .babelrc file will do the trick! – JESii Commented Jan 2, 2016 at 17:02
5 Answers
Reset to default 9You need to add presets to your babel-loader:
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: "babel-loader",
presets: ['es2015', 'react']
},
http://babeljs.io/docs/plugins/#presets
First of all: if you are using React v^0.14, you should render your code using React-Dom. https://www.npmjs.com/package/react-dom
Second, this should resolve your problem: babel-loader jsx SyntaxError: Unexpected token
I'm currently using React 0.14.3. The ReactDOM solution did not work, nor did adding the babel presets into the webpack.config.js. Basically, those solutions appear to work only if you have a single loader defined, but I had both the babel-loader as well as the react-hot loader.
What DID work was to install the babel react presets module:
npm install babel-preset-react
and then create a .babelrc file in my project directory with the following:
{
"presets": ['react']
}
This is documented at http://babeljs.io/docs/plugins/preset-react/, as pointed to by Abhinav Singi
For me the answer was to include the presets in the query block:
query: {
presets: ['react', 'es2015']
}
this is help me to solved that issue.
create new file .babelrc on same directory webpack.config.js. Add this to .babelrc
{
"stage": 2,
"env": {
"development": {
"plugins": [
"react-display-name",
"react-transform"
],
"extra": {
"react-transform": {
"transforms": [{
"transform": "react-transform-hmr",
"imports": ["react"],
"locals": ["module"]
}]
}
}
}
}
}
本文标签: javascriptReact Babel webpack not parsing jsx codeStack Overflow
版权声明:本文标题:javascript - React, babel, webpack not parsing jsx code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1738102804a1925006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论