my-project@1.0.0 dev webpack-dev-server –inline –progress –config build/webpack.dev.conf.js
(node:8311) [DEP0111] DeprecationWarning: Access to process.binding(‘http_parser’) is deprecated. (Use node --trace-deprecation ... to show where the warning was created) 13% building modules 27/68 modules 41 active …Users/zhuyishu/my-project/src/App.vue{ parser: “babylon” } is deprecated; we now treat it as { parser: “babel” }. 94% asset optimization
node --trace-deprecation ...
ERROR Failed to compile with 2 errors 01:09:43
error in ./node_modules/axios/lib/platform/index.js
Module parse failed: Unexpected token (5:2) You may need an appropriate loader to handle this file type. | | export default { | …utils, | …platform | }
@ ./node_modules/axios/lib/defaults/index.js 8:0-44 @ ./node_modules/axios/lib/axios.js @ ./node_modules/axios/index.js @ ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/views/tlias/EmpView.vue @ ./src/views/tlias/EmpView.vue @ ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue @ ./src/App.vue @ ./src/main.js @ multi (webpack)-dev-server/client?http://localhost:7070 webpack/hot/dev-server ./src/main.js
error in ./node_modules/axios/lib/core/mergeConfig.js
Module parse failed: Unexpected token (6:69) You may need an appropriate loader to handle this file type. | import AxiosHeaders from “./AxiosHeaders.js”; | | const headersToObject = (thing) => thing instanceof AxiosHeaders ? { …thing } : thing; | | /**
@ ./node_modules/axios/lib/axios.js 6:0-48 @ ./node_modules/axios/index.js @ ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/views/tlias/EmpView.vue @ ./src/views/tlias/EmpView.vue @ ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue @ ./src/App.vue @ ./src/main.js @ multi (webpack)-dev-server/client?http://localhost:7070 webpack/hot/dev-server ./src/main.js
你遇到的错误是由于 Webpack 无法解析某些代码中的现代 JavaScript 语法(如扩展运算符 ...)。这通常是因为缺少合适的 loader 来处理这些代码。具体来说,可能是 babel-loader 没有正确配置来处理 node_modules 目录中的代码。
...
babel-loader
node_modules
以下是解决这个问题的几个步骤:
首先,确保你已经安装了必要的依赖项:
npm install --save-dev babel-loader @babel/core @babel/preset-env
确保你的项目根目录中有一个 .babelrc 或 babel.config.js 文件,包含以下内容:
.babelrc
babel.config.js
{ "presets": ["@babel/preset-env"] }
module.exports = { presets: ["@babel/preset-env"] };
修改 webpack.dev.conf.js 来确保 babel-loader 处理 node_modules 目录中的代码。通常,babel-loader 会排除 node_modules,但对于某些模块(如 axios),你需要显式地包括它们。
webpack.dev.conf.js
axios
在 webpack.dev.conf.js 中找到 module 配置部分,并修改 rules:
module
rules
module: { rules: [ { test: /\.js$/, exclude: /node_modules\/(?!(axios)\/).*/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'] } } }, // 其他规则 ] }
有时候,Webpack 可能会缓存旧的配置。你可以尝试清理缓存并重新启动开发服务器:
rm -rf node_modules/.cache npm run dev
以下是一个修改后的示例 webpack.dev.conf.js:
const path = require('path'); const webpack = require('webpack'); module.exports = { entry: './src/main.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/' }, module: { rules: [ { test: /\.js$/, exclude: /node_modules\/(?!(axios)\/).*/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'] } } }, // 其他规则 ] }, plugins: [ new webpack.HotModuleReplacementPlugin() ], devServer: { contentBase: path.resolve(__dirname, 'dist'), hot: true, open: true, historyApiFallback: true, port: 7070 } };
通过这些步骤,你应该可以解决 Module parse failed 的问题,并使你的 Webpack 配置正确处理现代 JavaScript 语法。
Module parse failed