跳至内容

AWS Amplify Build Fails with NODE_ENV=production but Works with NODE_ENV=development - Vite Module Resolution Issue

0

AWS Amplify Build Fails with NODE_ENV=production but Works with NODE_ENV=development - Vite Module Resolution Issue

Tags: amplify, vite, nodejs, build-failure, module-resolution

Question

I'm experiencing a consistent build failure in AWS Amplify that only occurs when NODE_ENV=production. The same code builds successfully when NODE_ENV=development.

Environment

  • Frontend: React + Vite + TypeScript
  • Node.js: 20.19.0
  • Build tool: Vite 6.3.2
  • Amplify build image: Amazon Linux

Issue

Build fails with module resolution error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'vite' imported from /codebuild/output/.../node_modules/.vite-temp/vite.config.js.timestamp-1234567890.mjs

What Works

  • ✅ Local builds (both dev and prod)
  • ✅ Amplify stage environment (NODE_ENV=staging)
  • ✅ Amplify main environment when changed to NODE_ENV=development

What Fails

  • ❌ Amplify main environment with NODE_ENV=production

Troubleshooting Attempted

  1. Moved vite from devDependencies to dependencies
  2. Moved all build tools to regular dependencies
  3. Used npx vite build instead of direct vite command
  4. Installed vite globally in build phase
  5. Bypassed vite config with --config /dev/null
  6. Used local vite binary ./node_modules/.bin/vite

Current amplify.yml

version: 1
frontend:
  phases:
    preBuild:
      commands:
        - nvm use 20
        - npm ci
        - npm install -g vite
    build:
      commands:
        - npm run type-check
        - npx vite build --config /dev/null --outDir dist

Question

Is there a known issue with Amplify's handling of Node.js module resolution when NODE_ENV=production? What's the recommended approach for Vite builds in Amplify production environments?

The workaround of using NODE_ENV=development works but feels suboptimal for a production deployment.

已提问 2 个月前64 查看次数
2 回答
0

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.

已回答 2 个月前
-1

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:

  1. In production mode, Node.js might be more strict about module resolution paths
  2. Vite might be using different module resolution strategies based on the environment

Here are some recommended approaches to resolve this issue:

  1. Explicitly specify the Vite configuration file path in your build command:
npx vite build --config ./vite.config.js
  1. 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.

  2. Try using a shell command chaining operator to ensure environment variables are properly set before running the build:

NODE_ENV=production && npx vite build
  1. 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

已回答 2 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。