webpack报错:无法解析babel_loader的问题求助
Webpack构建报错:Module not found: Error: Cannot resolve 'babel_loader'
在使用Webpack构建项目时,持续出现以下报错:
Module not found: Error: Cannot resolve 'babel_loader'
已在node_modules中安装babel-loader和@babel-core,且两者均已在package.json中声明。尝试过重新安装babel-loader、多次修改webpack.config.js、重启IDE和终端,但问题仍未解决。
现有配置文件
webpack.config.js
const path = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); module.exports = { entry: "./src/index.js", output: { filename: "main.js", path: path.resolve(__dirname, "build"), }, plugins: [ new HtmlWebpackPlugin({ template: path.join(__dirname, "public", "index.html"), }), ], devServer: { static: { directory: path.join(__dirname, 'build'), }, port: 3000, }, module: { rules: [ { test: /\.(js)$/, exclude: /(node_modules)/, use: { loader: 'babel_loader' }, }, ] }, resolve: { modules: ['node-modules'], extensions: ['*', '.js'], } };
package.json
{ "name": "mybuh", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "build": "webpack --mode production", "start": "webpack serve --mode development", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "babel-loader": "^9.1.2", "@babel/core": "^7.21.8", "@babel/preset-env": "^7.21.5", "webpack": "^5.83.1", "webpack-cli": "^5.1.1", "html-webpack-plugin": "^5.5.1", "webpack-dev-server": "^4.15.0" } }
问题解决
报错根源是两处拼写错误:
- loader名称错误:配置中写的是
babel_loader,正确名称应为babel-loader(下划线改为短横线) - 模块路径错误:
resolve.modules中写的是node-modules,正确路径应为node_modules(短横线改为下划线)
修正后的webpack.config.js关键部分
module: { rules: [ { test: /\.(js)$/, exclude: /(node_modules)/, use: { loader: 'babel-loader' // 修正此处 }, }, ] }, resolve: { modules: ['node_modules'], // 修正此处 extensions: ['*', '.js'], }
修改完成后,重新执行构建命令即可解决该报错。
内容的提问来源于stack exchange,提问作者Nkwitchoua
相关产品推荐
相关产品推荐

