Replit App Works in Preview but Fails After Publish: Fix
Your Replit app runs fine in the workspace but shows errors or a blank page after you publish? Check deployment secrets, the run command, localhost URLs and logs, in the order that finds the cause fastest.
When a Replit app works in the workspace but breaks after you publish it, the first thing to check is secrets. Replit keeps workspace secrets and deployment secrets separate, so the published app often cannot see the values you set while building. Add each secret in the Publishing pane, republish, then read the deployment logs for the first error.
This is the most common version of the "it worked in preview" problem for apps built with AI app builders. The preview is a development environment. The published app is a separate deployment with its own settings, its own secrets and often a different database. This guide lists what differs and how to check each item.
Step 1: Read the Deployment Logs First
Before changing anything, open the Logs tab for the deployment (in the Publishing pane). Replit's deployment troubleshooting page points there for startup errors and stack traces.
Look for the first error after the latest publish. Typical lines:
undefinedorMissing environment variableerrors: go to Step 2.ECONNREFUSED 127.0.0.1orlocalhost: go to Step 3.Cannot find moduleor a failed build: go to Step 4.- Database connection or auth errors: go to Step 5.
The last error in a long log is often a side effect. The first one after boot is usually the cause.
Step 2: Add Secrets to the Deployment
According to Replit's troubleshooting guidance, secrets set in the editor do not automatically carry over to the published app, and a working preview does not prove the production configuration is right. Replit also states that development and production secrets are stored separately, so changing one does not change the other.
Do this:
- List every variable your code reads. In a Node.js app, search for
process.env.. In Python, search foros.environandos.getenv. - Open the Publishing pane and add each one to the deployment secrets.
- Republish. A running deployment does not pick up new secrets until you do.
A quick way to see what is missing without printing secret values is to log only which names are set:
const required = ['DATABASE_URL', 'SESSION_SECRET', 'STRIPE_SECRET_KEY'];
const missing = required.filter((name) => !process.env[name]);
if (missing.length) {
console.error('Missing env vars:', missing.join(', '));
process.exit(1);
}
Failing at startup with a clear message beats a vague 500 later. Never log the values.
One community thread reports secrets not saving or republishing as expected. If the secret is visible in the deployment store but your logs still say it is missing, contact Replit support with the deployment details.
Step 3: Remove Hard-Coded localhost URLs
In the workspace, your frontend may call http://localhost:3000/api/.... After publishing, the browser tries to reach the visitor's own machine and fails. Search your code:
grep -rn "localhost\|127.0.0.1" src/ --include=*.js --include=*.ts --include=*.tsx --include=*.py
Replace them with relative paths such as /api/items, or with a base URL from an environment variable. Also check OAuth redirect URLs and webhook URLs, which often contain the preview address. For the OAuth case, see Google OAuth redirect_uri_mismatch in Production.
On the server, bind to all interfaces and use the port the platform gives you, rather than a fixed localhost address:
const port = process.env.PORT || 3000;
app.listen(port, '0.0.0.0', () => console.log(`Listening on ${port}`));
Step 4: Check the Build and Run Commands
The workspace "Run" button and the deployment use separate commands. A dev server (npm run dev) is not what you want in production. Check that:
- The build command builds the frontend (for example
npm run build). - The run command starts the production server (for example
npm startornode server.js), not a watcher. - The packages your server needs at runtime are in
dependencies, not onlydevDependencies, if your build step prunes dev packages. - Your
package.jsonhas astartscript that matches what you expect.
Run the production commands in the Shell first. If npm run build && npm start fails there, it will fail when published.
Step 5: Compare the Database and Auth Settings
Replit's troubleshooting guidance lists the database and auth configuration as things to compare between development and production. Common traps:
- The development database holds your test data. The production database may be empty, so pages that expect rows show nothing or crash on
undefined. - Migrations ran in development but not in production. Run them against the production database before publishing.
- Auth callback URLs, allowed origins and cookie settings point at the preview domain. Add your published domain.
- Cookies marked
securebehave differently behind a proxy. See Express Session Cookie Not Set in Production.
Step 6: Check Health Checks and Slow Startup
Some deployment types check that your app answers quickly after it starts. Replit's guidance mentions that for autoscale deployments the homepage needs to respond within about five seconds, and that heavy initialization, slow database connections or missing variables can cause timeouts. Move slow setup (large imports, warm-up queries) off the startup path, and make sure / returns quickly even if a dependency is slow.
Step 7: Test the Published URL Like a User
After the fix, do not trust the preview again. Open the published URL in a private window and:
- Load the home page and check the browser console for red errors.
- Sign up and sign in with a fresh account.
- Hit the main action (create, save, pay) once.
- Check the deployment logs for new errors.
Add a simple uptime check on the published URL so you hear about the next break before your users do. See Know Your Website Is Down Immediately for how.
How Relia Helps
Apps made with AI builders often have little error visibility once they are live. Relia watches the users, APIs and database of a live product, joins related signals such as a publish, a failing request and users hitting an error into one incident explained in plain words, and shows who was affected. It also runs uptime monitors and checks on key pages. For Node.js and browser apps, setup is npx relia-sdk@latest init. Relia works in Claude Code, Cursor and VS Code through MCP. There is a Free plan and a 14-day Pro trial.
FAQ
Why does my Replit app work in the editor but not when published?
The editor and the published app are separate environments. Secrets, commands, databases and URLs can differ, and a working preview does not verify the production setup.
Do Replit secrets copy to the deployment automatically?
Replit's troubleshooting guidance says they do not. Add them to the deployment secrets in the Publishing pane and republish.
Where do I find errors for a published Replit app?
Use the Logs tab in the Publishing pane. Read the first error after the latest publish.
Why does my published app show an empty database?
It may use a different database from the workspace. Check the connection string and run migrations against the production database.
Does this also apply to Lovable, Bolt and v0?
The same idea holds: the live environment has its own variables and URLs. For Lovable and Supabase see Lovable Supabase Login Fails After Publish.