我正在尝试使用webpack with react但是收到此错误:
> ./app/main.js中的错误Module parse failed: /Users/me/app/main.js Line 2: Unexpected token
You may need an appropriate loader to handle this file type.
| //npm
| import React from 'react';
这是感兴趣的webpack配置部分:
test: /\.jsx?$/,
include: path.join(__dirname, 'src'),
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
和package.json:
"devDependencies": {
"autoprefixer-loader": "^3.1.0",
"babel-core": "^6.1.20",
"babel-loader": "^6.1.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"jest": "^0.1.40",
"webpack": "^1.12.4",
"webpack-dev-server": "^1.12.1",
"css-loader": "^0.22.0",
"style-loader": "^0.13.0"
},
"dependencies": {
"babel-polyfill": "^6.1.19",
"history": "^1.17.0",
"react": "^0.14.2",
"react-dom": "^0.14.2",
"react-mdl": "^1.0.4",
"react-router": "^1.0.2"
}
答案 0 :(得分:6)
查看错误消息中的路径
模块解析失败:/Users/me/app/main.js第2行:意外的令牌
似乎该模块不在<body><!--All the comments you'll see below are meant to null white space between layout elements--><!--
--><div class="wrapper"><!--
--><div class="backDrop_leftShadow"></div><!--
--><div class="backDrop" id="backDrop"><!--
--><div class="lineBreak" id="addItem">click here to square</div><!--
--><div class="lineBreak"></div><!--
--><div class="interface">yo</div>
</div><!--
--><div class="backDrop_rightShadow"></div></div>
<!--<script language="javascript" type="text/javascript" src="../javascript/viewportControl.js"></script>-->
</body>
文件夹中。但是,您明确指出只有src
内的模块才能通过babel加载程序。
调整path.join(__dirname, 'src')
,使其也包含include
。
答案 1 :(得分:2)
<td data-bind="numeral: {interest: interest, fmt: '0%' }">
这是我的加载程序配置。 配置的一个重要区别是加载器的命名。尝试'babel'而不是'babel-loader'。 下一个可能的错误可能是您的版本。更新babel-loader(至6.2.0)。