我有一个React项目。在我导入React的每个.js文件的第一行中,我都收到以下错误:Parsing error: Cannot find module '@babel/preset-react'
.
软件包.json
{
"name": "admin",
"version": "2.0.0",
"description": "",
"author": "",
"main": "index.js",
"engines": {
"node": ">=14.15 <15",
"npm": "~6"
},
"scripts": {
"build-config:local": "CONFIG_ENV=local npm run build-config",
"build-config:dev": "CONFIG_ENV=dev npm run build-config",
"build-config:staging": "CONFIG_ENV=staging npm run build-config",
"build-config:prod": "CONFIG_ENV=prod npm run build-config",
"build-config": "buildconfig --env $CONFIG_ENV",
"build": "npm run build-config && webpack --config ./webpack.config.js --mode production",
"start": "webpack-dev-server --config ./webpack.config.dev.js --mode development"
},
"dependencies": {
"@babel/eslint-parser": "^7.15.7",
"@fortawesome/fontawesome-svg-core": "^1.2.36",
"@fortawesome/free-brands-svg-icons": "^5.15.4",
"@fortawesome/free-solid-svg-icons": "^5.15.4",
"@fortawesome/react-fontawesome": "^0.1.15",
"axios": "^0.21.4",
"axios-progress-bar": "^1.2.0",
"classnames": "^2.3.1",
"clean-webpack-plugin": "^4.0.0",
"copy-text-to-clipboard": "^3.0.1",
"copy-webpack-plugin": "^9.0.1",
"css-loader": "^6.3.0",
"date-fns": "1.30.1",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.3.2",
"humanize-string": "^2.1.0",
"lodash": "^4.17.21",
"loglevel": "^1.7.1",
"rc-checkbox": "^2.1.7",
"rc-collapse": "^3.1.2",
"rc-switch": "^3.2.2",
"react": "^16.3.0",
"react-bootstrap-table-next": "^4.0.3",
"react-bootstrap-table2-editor": "^1.4.0",
"react-bootstrap-table2-filter": "^1.3.3",
"react-bootstrap-table2-paginator": "^2.1.2",
"react-dom": "^16.9.0",
"react-helmet": "^6.1.0",
"react-json-tree": "^0.15.0",
"react-router-dom": "^5.3.0",
"react-syntax-highlighter": "^15.4.4",
"sass": "^1.42.1",
"sass-loader": "^12.1.0",
"serialize-error": "^8.1.0",
"single-config": "^2.0.1",
"style-loader": "^3.3.0",
"svg-react-loader": "^0.4.6",
"webpack": "^5.53.0",
"webpack-cli": "^4.8.0"
},
"devDependencies": {
"@babel/core": "^7.15.5",
"@babel/preset-env": "^7.15.6",
"@babel/preset-react": "^7.14.5",
"babel-loader": "^8.2.2",
"eslint": "^7.32.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.19.0",
"prettier": "^2.2.1",
"stylelint": "^13.13.0",
"stylelint-config-prettier": "^8.0.2",
"stylelint-config-recommended": "^5.0.0",
"stylelint-order": "^4.1.0",
"stylelint-prettier": "1.2.0",
"stylelint-scss": "3.19.0",
"webpack-dev-server": "^4.2.1"
}
}
.eslintrc.json
{
"parser": "@babel/eslint-parser",
"extends": ["eslint:recommended", "plugin:react/recommended", "prettier", "prettier/react"],
"plugins": ["react", "import", "jsx-a11y", "prettier"],
"env": {
"es6": true,
"browser": true,
"node": true
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module",
"ecmaVersion": 6,
"requireConfigFile": false,
"babelOptions": {
"presets": ["@babel/react"]
}
},
"settings": {
"react": {
"version": "detect"
}
},
"rules": {
"prettier/prettier": "error",
"consistent-return": "warn",
"no-console": "warn",
"eqeqeq": "error",
"no-alert": "error",
"no-caller": "error",
"no-constructor-return": "error",
"no-else-return": "error",
"no-eq-null": "error",
"no-eval": "error",
"no-extend-native": "error",
"no-extra-bind": "error",
"no-extra-label": "error",
"no-floating-decimal": "error",
"no-implicit-globals": "error",
"no-implied-eval": "error",
"no-iterator": "error",
"no-lone-blocks": "error",
"no-loop-func": "error",
"no-multi-spaces": "error",
"no-multi-str": "error",
"no-new-func": "error",
"no-new-wrappers": "error",
"no-octal-escape": "error",
"no-proto": "error",
"no-return-assign": "error",
"no-return-await": "error",
"no-script-url": "error",
"no-self-compare": "error",
"no-sequences": "error",
"no-throw-literal": "error",
"no-unmodified-loop-condition": "error",
"no-unused-expressions": "error",
"no-useless-call": "error",
"no-useless-concat": "error",
"no-useless-return": "error",
"require-await": "error",
"arrow-body-style": ["error", "as-needed"],
"arrow-spacing": ["error", { "before": true, "after": true }],
"no-duplicate-imports": "error",
"no-useless-constructor": "error",
"no-useless-rename": "error",
"no-var": "error",
"no-useless-computed-key": "error",
"prefer-arrow-callback": "error",
"prefer-const": "error",
"prefer-spread": "error",
"prefer-template": "error",
"rest-spread-spacing": "error",
"symbol-description": "error",
"comma-spacing": ["error", { "before": false, "after": true }],
"key-spacing": "error",
"keyword-spacing": "error",
"max-len": ["error", { "code": 160 }],
"linebreak-style": "error",
"lines-between-class-members": "error",
"new-cap": "error",
"no-multi-assign": "error",
"no-negated-condition": "warn",
"no-nested-ternary": "error",
"no-new-object": "error",
"no-trailing-spaces": "error",
"prefer-object-spread": "error",
"react/prop-types": "off",
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx", ".tsx"] }],
"react/sort-comp": [
"error",
{
"order": ["static-variables", "static-methods", "lifecycle", "everything-else", "/^is.+$/", "/^on.+$/", "/^render.+$/", "render"],
"groups": {
"lifecycle": [
"displayName",
"propTypes",
"contextTypes",
"childContextTypes",
"mixins",
"statics",
"defaultProps",
"state",
"instance-variables",
"constructor",
"getDefaultProps",
"getInitialState",
"getChildContext",
"getDerivedStateFromProps",
"componentWillMount",
"UNSAFE_componentWillMount",
"componentDidMount",
"componentWillReceiveProps",
"UNSAFE_componentWillReceiveProps",
"shouldComponentUpdate",
"componentWillUpdate",
"UNSAFE_componentWillUpdate",
"getSnapshotBeforeUpdate",
"componentDidUpdate",
"componentDidCatch",
"componentWillUnmount"
]
}
}
],
"import/no-unresolved": "off",
"import/order": [
"error",
{
"pathGroups": [
{
"pattern": "*.scss",
"group": "object",
"patternOptions": { "matchBase": true },
"position": "after"
},
{
"pattern": "*.svg",
"group": "object",
"patternOptions": { "matchBase": true },
"position": "after"
}
]
}
]
}
}
.babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
webpack.配置.开发.js
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebPackPlugin = require('html-webpack-plugin');
module.exports = () => ({
devServer: {
port: 8084,
historyApiFallback: true, // serves index.html for all routes to avoid 404
hot: true
},
devtool: 'eval-source-map',
stats: 'minimal',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[fullhash].bundle.js',
publicPath: '/'
},
resolve: {
extensions: ['.js', '.jsx', '.json']
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: [path.resolve(__dirname, 'node_modules')],
include: [path.resolve(__dirname, 'src')],
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react']
}
}
},
{
test: /\.(scss|css)$/,
use: [
'style-loader',
'css-loader',
{
loader: 'sass-loader',
options: {
sassOptions: {
quietDeps: true
}
}
}
]
},
{
test: /\.(jpe?g|png|gif|svg)$/,
include: [path.resolve(__dirname, 'src')],
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'images/',
useRelativePath: false
}
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebPackPlugin({
template: './src/index.html',
favicon: './src/assets/favicon/favicon-32x32.png'
})
]
});
6条答案
按热度按时间qzwqbdag1#
也许它会帮助一些人,我刚刚有一个类似的问题,我解决了。
我碰巧在一个全栈项目中打开了VSCode,其中前后是分开的文件夹。当我在前面的文件夹中打开VSCodeonly时,这个错误消失了。
我想一个解决方案是创建一个共享的node_modules文件夹,或者从前后文件夹设置一个工作区,我选择了第二个选项。
gab6jxml2#
如果在VSCode中使用全栈方法(作为工作区中的子文件夹的前端和后端)出现此问题,请按以下方式调整工作区的eslint扩展配置:
在我的案例中解决了这个问题。
vsnjm48y3#
尝试显式安装@babel/preset-react,因为有人告诉我们默认情况下不会安装它:
此外,在eslint.json文件中,您在babelOptions下引用了
@babel/react
而不是@babel/preset-react
。dauxcl2d4#
在试着弄清楚这个问题几个小时后,用
babel-preset-react-app
替换@babel/preset-react
终于起作用了。这是我的完整package.json(尽管依赖关系/ dev-dependencies之间可能有些混淆)
siotufzp5#
我一直有同样的问题。我通过删除
我注意到在package.json中有babel/eslint而不是@bable/eslint
yruzcnhs6#
在webpack.config.js文件中添加以下代码