- 新しい順
- 投票が多い順
- コメントが多い順
I've already tried most of these approaches: ✅ Already tried: npx vite build --config ./vite.config.js - Same error ✅ Already tried: NODE_ENV=production && npx vite build - Same error ✅ Already tried: Dedicated build scripts in package.json - Same error ✅ Already tried: Even bypassed the config entirely with --config /dev/null - Same error
Additional context:
- This is NOT a monorepo - it's a standard Vite React app
- The issue is Amplify environment-specific - same code works fine locally with NODE_ENV=production
- Stage environment works fine with NODE_ENV=staging but Main fails with NODE_ENV=production
- We've moved ALL build dependencies to regular dependencies (not just devDependencies)
- Even installing vite globally in the build phase doesn't help
The core issue: Amplify's production environment has different module resolution behavior than development/staging environments, even with identical code and dependencies.
Specific question: Is there a known difference in how Amplify's CodeBuild handles module resolution between NODE_ENV=production vs other values? This appears to be an infrastructure-level issue rather than a code configuration problem.
This issue appears to be related to how module resolution works differently between development and production environments in AWS Amplify, particularly with Vite.
When you encounter the error "Cannot find package 'vite'" in your production build but not in development, it's likely due to how Node.js handles module resolution differently based on the NODE_ENV value. In production mode, certain optimizations are applied that can affect how modules are located and loaded.
The error suggests that Vite's module resolution is failing specifically in the production environment. This could be happening because:
- In production mode, Node.js might be more strict about module resolution paths
- Vite might be using different module resolution strategies based on the environment
Here are some recommended approaches to resolve this issue:
- Explicitly specify the Vite configuration file path in your build command:
npx vite build --config ./vite.config.js
-
If you're working in a monorepo structure, ensure your tsconfig.json properly excludes the amplify directory by adding it to the exclude section. This prevents TypeScript from trying to resolve it as a module.
-
Try using a shell command chaining operator to ensure environment variables are properly set before running the build:
NODE_ENV=production && npx vite build
- Consider creating a dedicated build script in your package.json that handles the environment setup properly, then call that script from Amplify.
While using NODE_ENV=development for production builds works as a workaround, you're right that it's not ideal. The recommended approach is to fix the module resolution issue rather than changing the environment variable, as NODE_ENV=production typically enables important optimizations for production deployments.
Sources
Troubleshooting AWS CodeBuild - AWS CodeBuild
Troubleshoot "Cannot find module $amplify/env/<function-name>" - AWS Amplify Gen 2 Documentation
AWS Amplify | Extensible Web & Mobile Apps | Amazon Web Services
関連するコンテンツ
- 質問済み 6ヶ月前
