三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

前端项目启动报错常见错误总结

前端项目启动报错常见错误总结

目录

一、依赖安装与版本冲突类(占 60% 以上)

1. 最常见:node-sass 编译失败(gyp ERR!)

2. 依赖版本冲突(peerDependencies)

3. 找不到模块(Cannot find module 'xxx')

4. Node.js 版本太高报错(OpenSSL 错误)

5. 包损坏 / 缓存问题

二、环境与配置类

1. 端口被占用

2. 配置文件语法错误

3. Babel 语法不支持

4. ESLint 配置错误

三、路径与文件类

1. 模块路径错误

2. 静态资源路径错误

四、框架特有错误(Vue 为主)

1. Vue2/Vue3 版本不兼容

2. Webpack 版本不兼容

五、万能快速排查流程(90% 问题解决)


一、依赖安装与版本冲突类(占 60% 以上)

1. 最常见:node-sass 编译失败(gyp ERR!)

典型报错

plaintext

gyp ERR! build error gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe` failed with exit code: 1 Module build failed: Error: Node Sass version 7.0.1 is incompatible with ^4.0.0.

根本原因node-sass 与 Node.js 版本严格绑定,版本不匹配;或缺少 C++ 编译环境。解决方案

  • 优先替换(推荐):卸载 node-sass,改用纯 JS 实现的sass
    npm uninstall node-sass npm install sass --save-dev
  • 版本匹配(老项目必须用 node-sass 时):
    Node.js 版本node-sass 版本
    14.x4.14.x
    16.x6.0.x
    18.x8.0.x+

2. 依赖版本冲突(peerDependencies)

典型报错

plaintext

npm ERR! Could not resolve dependency: npm ERR! peer vue@"^3.0.0" from xxx@1.0.0 npm ERR! node_modules/xxx npm ERR! xxx@"*" from the root project

根本原因某个包要求的依赖版本与你项目已安装的版本不兼容。解决方案

  • 老项目(Vue2 为主):用--legacy-peer-deps忽略 peer 依赖检查
    npm install --legacy-peer-deps
  • 新项目:用--force强制覆盖(谨慎使用)
    npm install --force

3. 找不到模块(Cannot find module 'xxx')

典型报错

plaintext

Error: Cannot find module 'webpack-cli/bin/config-yargs' Error: Cannot find module 'vue-template-compiler'

根本原因依赖没装全、装坏了,或 package.json 里漏写了依赖。解决方案

# 万能重装第一步 rm -rf node_modules package-lock.json npm install

如果还报错,手动安装缺失模块:

npm install xxx --save-dev

4. Node.js 版本太高报错(OpenSSL 错误)

典型报错

plaintext

Error: error:0308010C:digital envelope routines::unsupported at new Hash (node:internal/crypto/hash:71:19)

根本原因Node.js 17+ 改用 OpenSSL 3.0,老项目 Webpack4/Vue CLI4 不兼容。解决方案修改 package.json 的启动脚本,添加--openssl-legacy-provider

"scripts": { "serve": "set NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service serve", "build": "set NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service build" }

Mac/Linux 去掉set

"serve": "NODE_OPTIONS=--openssl-legacy-provider vue-cli-service serve"

5. 包损坏 / 缓存问题

典型报错

plaintext

ENOENT: no such file or directory, rename 'xxx' -> 'yyy' npm ERR! code EINTEGRITY

根本原因npm 缓存损坏,或下载过程中包被截断。解决方案

npm cache clean --force rm -rf node_modules package-lock.json npm install

二、环境与配置类

1. 端口被占用

典型报错

plaintext

Port 8080 is already in use. Use --port to specify a different port.

根本原因8080/3000 等默认端口被其他程序占用。解决方案

  • 临时换端口启动:
    npm run serve -- --port 8081
  • 永久修改:在 vue.config.js 中配置
    module.exports = { devServer: { port: 8081 } }
  • 杀死占用端口的进程:
    • Windows:netstat -ano | findstr :8080找到 PID,再taskkill /f /pid 进程号
    • Mac/Linux:lsof -i :8080找到 PID,再kill -9 进程号

2. 配置文件语法错误

典型报错

plaintext

SyntaxError: Unexpected token '}' SyntaxError: Unexpected end of JSON input

根本原因vue.config.js、package.json、.eslintrc 等配置文件有语法错误(多逗号、少括号、引号不匹配)。解决方案检查报错提示的文件行号,修复 JSON/JS 语法;JSON 文件绝对不能有注释。

3. Babel 语法不支持

典型报错

plaintext

Support for the experimental syntax 'classProperties' isn't currently enabled Support for the experimental syntax 'optionalChaining' isn't currently enabled

根本原因Babel 没有配置对应的语法解析插件。解决方案安装预设插件:

npm install @babel/preset-env @babel/plugin-proposal-class-properties --save-dev

在 babel.config.js 中添加:

module.exports = { presets: [ ['@babel/preset-env', { targets: { browsers: ['> 1%', 'last 2 versions'] } }] ], plugins: ['@babel/plugin-proposal-class-properties'] }

4. ESLint 配置错误

典型报错

plaintext

ESLint couldn't find the config "standard" to extend from. ESLint: Parsing error: Unexpected token <

根本原因ESLint 依赖缺失,或 parser 配置错误。解决方案安装对应 ESLint 配置包:

npm install eslint-config-standard eslint-plugin-vue --save-dev

在.eslintrc.js 中指定 parser:

module.exports = { parser: 'vue-eslint-parser', parserOptions: { parser: '@babel/eslint-parser' } }

三、路径与文件类

1. 模块路径错误

典型报错

plaintext

Module not found: Error: Can't resolve './components/HelloWorld' in '/src'

根本原因相对路径写错、文件不存在,或文件名大小写错误。解决方案

  • 检查文件路径和文件名是否正确
  • 注意:Linux 系统大小写敏感,Windows 不敏感,本地没问题部署报错大概率是大小写问题

2. 静态资源路径错误

典型报错

plaintext

GET http://localhost:8080/static/img/logo.png 404 (Not Found)

根本原因vue.config.js 中 publicPath 配置错误,或静态资源放错了目录。解决方案

  • 开发环境:publicPath 设为'/'
  • 生产环境部署到子目录:设为'/子目录名/'
  • 静态资源放在public目录下,引用时用/xxx.png;放在src/assets下用require('@/assets/xxx.png')

四、框架特有错误(Vue 为主)

1. Vue2/Vue3 版本不兼容

典型报错

plaintext

Unknown custom element: <router-view> - did you register the component correctly? export 'default' (imported as 'Vue') was not found in 'vue'

根本原因Vue3 项目用了 Vue2 的语法,或依赖包只支持 Vue3。解决方案

  • Vue2 项目必须用 Vue Router 3.x、Vuex 3.x
  • Vue3 项目用 Vue Router 4.x、Pinia

2. Webpack 版本不兼容

典型报错

plaintext

ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.

根本原因Webpack5 与老版本的 devServer 配置语法不兼容。解决方案将 vue.config.js 中的 devServer 配置从 Webpack4 格式改为 Webpack5 格式:

// 旧写法(Webpack4) devServer: { proxy: { ... }, overlay: true } // 新写法(Webpack5) devServer: { proxy: { ... }, client: { overlay: true } }

五、万能快速排查流程(90% 问题解决)

  1. 执行万能重装命令:
    rm -rf node_modules package-lock.json npm cache clean --force npm install
  2. 检查 Node.js 版本是否匹配项目要求(老项目用 Node14/16,新项目用 18/20)
  3. 检查是否有端口被占用
  4. 检查配置文件是否有语法错误
  5. 检查所有路径和文件名大小写是否正确
← 返回列表