How to Deploy Remix to Netlify: Step-by-Step Guide
To deploy a
Remix app to Netlify, create a Remix project with the Netlify adapter, build your app using npm run build, and then push your code to a Git repository connected to Netlify. Netlify will detect the build and deploy your Remix app automatically.Syntax
Deploying Remix to Netlify involves these key steps:
- Install Netlify Adapter: Add
@remix-run/netlifyto your project to enable Netlify-specific build support. - Configure Remix: Update
remix.config.jsto use the Netlify adapter. - Build Command: Use
npm run buildto prepare your app for deployment. - Publish Directory: Set Netlify to serve from the
publicorbuildfolder as configured.
bash
npm install @remix-run/netlify // remix.config.js const netlify = require('@remix-run/netlify'); /** @type {import('@remix-run/dev').AppConfig} */ module.exports = { serverBuildTarget: 'netlify', server: './server.js', adapter: netlify(), };
Example
This example shows a minimal Remix app configured for Netlify deployment. It installs the Netlify adapter, updates the config, and includes a simple server file.
bash
npm create remix@latest my-remix-netlify-app cd my-remix-netlify-app npm install @remix-run/netlify // remix.config.js const netlify = require('@remix-run/netlify'); module.exports = { serverBuildTarget: 'netlify', server: './server.js', adapter: netlify(), }; // server.js const { createRequestHandler } = require('@remix-run/netlify'); exports.handler = createRequestHandler(); // package.json scripts { "scripts": { "build": "remix build", "start": "netlify dev" } } // Deploy by pushing to GitHub and connecting repo to Netlify with build command "npm run build" and publish directory "public" or "build" depending on config.
Output
Netlify builds the Remix app and deploys it, serving your app at your Netlify URL.
Common Pitfalls
Common mistakes when deploying Remix to Netlify include:
- Not installing or configuring the
@remix-run/netlifyadapter, causing build failures. - Incorrect
remix.config.jssettings, especially missingserverBuildTargetoradapter. - Forgetting to create or export the
handlerinserver.js, which Netlify needs to run the server. - Setting wrong build commands or publish directories in Netlify settings.
Example of a wrong config and the fix:
js
// Wrong remix.config.js module.exports = { serverBuildTarget: 'node', // wrong target for Netlify server: './server.js', }; // Correct remix.config.js const netlify = require('@remix-run/netlify'); module.exports = { serverBuildTarget: 'netlify', server: './server.js', adapter: netlify(), };
Quick Reference
Summary tips for deploying Remix to Netlify:
- Always use
@remix-run/netlifyadapter for smooth integration. - Set
serverBuildTargettonetlifyinremix.config.js. - Export a
handlerfunction inserver.jsusingcreateRequestHandler. - Use
npm run buildas the build command in Netlify. - Set the publish directory to
publicorbuilddepending on your setup.
Key Takeaways
Use the @remix-run/netlify adapter and set serverBuildTarget to 'netlify' in remix.config.js.
Create and export a handler in server.js with createRequestHandler for Netlify to run your app.
Set Netlify build command to 'npm run build' and configure the correct publish directory.
Push your Remix app to a Git repo and connect it to Netlify for automatic deployment.
Double-check your remix.config.js and Netlify settings to avoid common deployment errors.