Compilation and Runtime Issues
This page collects common issues and solutions encountered during uni-app project compilation and runtime.
Compilation Errors
Q: "Cannot find module" error during compilation
Problem Description: Module not found error when compiling the project.
Solutions:
- Ensure related dependencies are installed:
npm install
- Check if the module is included in package.json
- Try installing the module separately:
npm install module-name
- Delete node_modules folder and package-lock.json, then re-run
npm install
Q: Syntax error during compilation
Problem Description: JavaScript or Vue syntax errors during compilation.
Solutions:
- Locate the specific code line based on error message
- Check code syntax, especially matching brackets, quotes, semicolons
- Check if ES6+ syntax needs transformation, ensure babel configuration is correct
- Use ESLint or other tools to check code standards
Q: SCSS/LESS compilation errors
Problem Description: Compilation errors when using SCSS or LESS.
Solutions:
- Ensure related preprocessor dependencies are installed:
npm install sass sass-loader --save-dev
ornpm install less less-loader --save-dev
- Check if syntax is correct, especially nesting rules and variable usage
- Check if preprocessor loader is correctly configured in webpack configuration
- Try using simpler syntax to gradually isolate the problem
Q: TypeScript compilation errors
Problem Description: Type checking errors when using TypeScript.
Solutions:
- Check if tsconfig.json configuration is correct
- Ensure type definitions are correct, may need to add type declaration files
- Use
// @ts-ignore
to temporarily ignore type checking for specific lines (not recommended as long-term solution) - Update TypeScript version and related dependencies
Runtime Errors
Q: White screen during runtime
Problem Description: Application shows white screen with no content after running.
Solutions:
- Check console for error messages
- Check if network requests are normal, check for CORS issues
- Check if entry file and main component are loaded correctly
- Try adding
console.log
debugging inonLoad
orcreated
lifecycle
Q: Page navigation not working
Problem Description: Clicking buttons or links doesn't navigate to other pages normally.
Solutions:
- Check if target page is correctly configured in pages.json
- Check if navigation path is correct, pay attention to case sensitivity
- Check if correct navigation API is used (e.g.,
uni.navigateTo
,uni.redirectTo
) - Check for code or plugins that intercept navigation
Q: Components not displaying
Problem Description: Some components on the page are not displaying normally.
Solutions:
- Check if components are correctly registered and imported
- Check if conditional rendering expressions are correct
- Check component styles, may be covered by other elements
- Check data binding, components may depend on certain data
Q: Styles not taking effect
Problem Description: Application CSS styles are not working as expected.
Solutions:
- Check if selectors are correct, may be a priority issue
- Check if platform-specific styles are used (e.g., styles that only work on H5)
- Try using
!important
to increase style priority (use with caution) - Check if style isolation is enabled, may need to use global styles
Platform-Specific Issues
Q: WeChat Mini Program runtime failure
Problem Description: Project cannot run normally in WeChat Mini Program environment.
Solutions:
- Check if WeChat Developer Tools version is compatible with the project
- Check if APIs or components not supported by WeChat Mini Program are used
- Check error messages in WeChat Developer Tools console
- Check if WeChat Mini Program configuration in manifest.json is correct
Q: H5 runtime issues
Problem Description: Project has issues in H5 environment.
Solutions:
- Check browser compatibility, try testing in different browsers
- Check if APIs only available in Mini Program or App are used
- Check if network requests have CORS issues
- Check if H5 configuration in manifest.json is correct
Q: App crashes on device
Problem Description: Application crashes when running on real device.
Solutions:
- Check if APIs requiring specific permissions are used (e.g., location, camera)
- Ensure required permissions are correctly configured in manifest.json
- Check if native plugins are compatible with current device
- Use debug version to run and check detailed error logs
Hot Reload Issues
Q: Hot reload not working
Problem Description: Application doesn't auto-update after modifying and saving code.
Solutions:
- Check if hot reload feature is enabled
- Try manually refreshing the application
- Check if file watching configuration is correct
- Restart development server
Q: State lost after hot reload
Problem Description: Application state is reset after hot reload.
Solutions:
- Use Vuex or other state management tools to save state
- Store critical state in localStorage or other persistent storage
- Understand how hot reload works, state loss is normal in some cases
Build Optimization Issues
Q: Slow compilation speed
Problem Description: Project compilation takes a very long time.
Solutions:
- Reduce project dependencies and unnecessary components
- Configure webpack caching
- Use faster package managers like yarn or pnpm
- Upgrade hardware, especially use SSD and increase memory
Q: Bundle size too large
Problem Description: Compiled application size is too large.
Solutions:
- Use production mode build:
npm run build
- Configure webpack code splitting and lazy loading
- Optimize images and media resources, consider using CDN
- Remove unused dependencies and code
Debugging Techniques
Effective Console Usage
- Use
console.log
,console.error
etc. to output debug information - Add logs in key lifecycle functions
- Use conditional breakpoints for complex logic debugging
- Utilize Vue Devtools to debug Vue components and state
Network Request Debugging
- Use browser developer tools network panel to monitor requests
- Add request and response interceptors to log detailed information
- Use mock data for isolated testing
- Check if request headers and parameter formats are correct
Performance Analysis
- Use browser developer tools performance panel to analyze runtime performance
- Identify and optimize time-consuming operations
- Reduce unnecessary rendering and computation
- Use asynchronous operations to avoid blocking main thread
Common Error Code Analysis
ERROR_TIMEOUT
Problem Description: Operation timeout.
Solutions:
- Check if network connection is stable
- Increase timeout setting
- Optimize time-consuming operations
- Add retry mechanism
ERROR_NETWORK_FAILURE
Problem Description: Network request failure.
Solutions:
- Check network connection
- Verify if API address is correct
- Check server status
- Add error handling and retry logic
If you cannot find a solution to your problem on this page, please check the uni-app official documentation or ask questions on the uni-app official forum.