0
0
RemixHow-ToBeginner ยท 4 min read

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/netlify to your project to enable Netlify-specific build support.
  • Configure Remix: Update remix.config.js to use the Netlify adapter.
  • Build Command: Use npm run build to prepare your app for deployment.
  • Publish Directory: Set Netlify to serve from the public or build folder 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/netlify adapter, causing build failures.
  • Incorrect remix.config.js settings, especially missing serverBuildTarget or adapter.
  • Forgetting to create or export the handler in server.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/netlify adapter for smooth integration.
  • Set serverBuildTarget to netlify in remix.config.js.
  • Export a handler function in server.js using createRequestHandler.
  • Use npm run build as the build command in Netlify.
  • Set the publish directory to public or build depending 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.