我正在使用yarn将webpack设置为我的react项目,并且出现此错误:
./src/app.js中的ERROR 67:6模块解析失败:意外的令牌 (67:6)您可能需要适当的加载程序来处理此文件类型, 当前没有配置任何加载程序来处理此文件。看到 https://webpack.js.org/concepts#loaders 「wdm」:编译失败。
webpack.config.js
const path = require("path");
module.exports = {
entry: "./src/app.js",
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
},
module:{
rules:[{
loader: 'babel-loader',
test: '/\.(js|jsx)$/',
exclude: /node_modules/
}]
},
devtool: 'cheap-module-eval-source-map',
devServer: {
contentBase: path.join(__dirname, 'public')
}
}
.babelrc
{
"presets": ["env", "react","@babel/preset-env"],
"plugins": [
"transform-class-properties"
]
}
package.json
{
"name": "react",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"serve": "live-server public/",
"build": "webpack",
"dev-server": "webpack-dev-server"
},
"dependencies": {
"babel-cli": "6.24.1",
"babel-core": "6.25.0",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-preset-env": "1.5.2",
"babel-preset-react": "6.24.1",
"live-server": "^1.2.1",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"webpack": "^4.39.3",
"webpack-dev-server": "^3.8.0"
},
"devDependencies": {
"@babel/core": "^7.6.0",
"babel-loader": "^8.0.6",
"webpack-cli": "^3.3.8"
}
}
答案 0 :(得分:6)
您正在使用不必要的转义符:这不是必需的。
替换 test: '/\.(js|jsx)$/',
替换为 test: /\.js$|jsx/,
,应该可以正常工作。
我在您的计算机中复制了您的问题,并发现上述问题已解决。
希望这会有所帮助,编写愉快的代码!
答案 1 :(得分:4)
所选答案缺少一些详细信息:
应该 test: '/\.(js|jsx)$/'
,替换为 test: /\.js|\.jsx$/
\: is an escape character
在这种情况下为.
|: is an Alternation / OR operand
$: is end of line
希望这对您有用。
答案 2 :(得分:0)
将.babelrc文件添加到node_modules文件夹所在的文件夹中,内容如下:
{
"presets": ["@babel/preset-react"]
}
答案 3 :(得分:0)
这个错误发生在我身上,仅仅是因为文件被放置在项目的父文件夹中(即位于项目文件夹之外)。
将文件移动到固定的文件夹中。
答案 4 :(得分:0)
将下面提到的代码放在webpack文件中
mix.extend(
"graphql",
new (class {
dependencies() {
return ["graphql", "graphql-tag"];
}
webpackRules() {
return {
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: "graphql-tag/loader"
};
}
})()
);
mix.js("resources/js/app.js", "public/js").vue();
mix.graphql();
答案 5 :(得分:-1)
web Failed to compile.
<PATH>/KeyboardAwareHOC.js 13:12
Module parse failed: Unexpected token (13:12)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders 3
| } from ‘react-native’
| import { isIphoneX } from ‘react-native-iphone-x-helper’
import type { KeyboardAwareInterface } from ‘./KeyboardAwareInterface’
|
| const _KAM_DEFAULT_TAB_BAR_HEIGHT: number = isIphoneX() ? 83 : 49
安装以下软件包,然后重新启动expo软件包。
https://www.npmjs.com/package/@codler/react-native-keyboard-aware-scroll-view