The Nx Plugin for Remix contains executors, generators, and utilities for managing Remix applications and libraries within an Nx workspace. It provides:

  • Integration with libraries such as Storybook, Jest, Vitest and Cypress.
  • Generators to help scaffold code quickly, including:
    • Libraries, both internal to your codebase and publishable to npm
    • Routes
    • Loaders
    • Actions
    • Meta
  • Utilities for automatic workspace refactoring.

Setting up @nx/remix

Installation

Keep Nx Package Versions In Sync

Make sure to install the @nx/remix version that matches the version of nx in your repository. If the version numbers get out of sync, you can encounter some difficult to debug errors. You can fix Nx version mismatches with this recipe.

In any Nx workspace, you can install @nx/remix by running the following command:

nx add @nx/remix

This will install the correct version of @nx/remix.

How @nx/remix Infers Tasks

The @nx/remix plugin will create a task for any project that has a Remix configuration file present. Any of the following files will be recognized as a Remix configuration file:

  • remix.config.js
  • remix.config.mjs
  • remix.config.cjs

View Inferred Tasks

To view inferred tasks for a project, open the project details view in Nx Console or run nx show project my-project --web in the command line.

@nx/remix Configuration

The @nx/remix/plugin is configured in the plugins array in nx.json.

nx.json
1{ 2 "plugins": [ 3 { 4 "plugin": "@nx/remix/plugin", 5 "options": { 6 "buildTargetName": "build", 7 "serveTargetName": "serve", 8 "startTargetName": "start", 9 "typecheckTargetName": "typecheck" 10 } 11 } 12 ] 13} 14

The buildTargetName, serveTargetName, startTargetName and typecheckTargetName options control the names of the inferred Remix tasks. The default names are build, serve, start and typecheck.

Using the Remix Plugin

Generate a Remix Application

Directory Flag Behavior Changes

The command below uses the as-provided directory flag behavior, which is the default in Nx 16.8.0. If you're on an earlier version of Nx or using the derived option, omit the --directory flag. See the as-provided vs. derived documentation for more details.

~/acme

nx g @nx/remix:app myapp --directory=apps/myapp

1NX Generating @nx/remix:application 2 3✔ What unit test runner should be used? · vitest 4 5CREATE apps/myapp/project.json 6UPDATE package.json 7CREATE apps/myapp/README.md 8CREATE apps/myapp/app/root.tsx 9CREATE apps/myapp/app/routes/_index.tsx 10CREATE apps/myapp/public/favicon.ico 11CREATE apps/myapp/remix.config.js 12CREATE apps/myapp/remix.env.d.ts 13CREATE apps/myapp/tsconfig.json 14CREATE apps/myapp/.gitignore 15CREATE apps/myapp/package.json 16UPDATE nx.json 17CREATE tsconfig.base.json 18CREATE .prettierrc 19CREATE .prettierignore 20UPDATE .vscode/extensions.json 21CREATE apps/myapp/vite.config.ts 22CREATE apps/myapp/tsconfig.spec.json 23CREATE apps/myapp/test-setup.ts 24CREATE apps/myapp-e2e/cypress.config.ts 25CREATE apps/myapp-e2e/src/e2e/app.cy.ts 26CREATE apps/myapp-e2e/src/fixtures/example.json 27CREATE apps/myapp-e2e/src/support/commands.ts 28CREATE apps/myapp-e2e/src/support/e2e.ts 29CREATE apps/myapp-e2e/tsconfig.json 30CREATE apps/myapp-e2e/project.json 31CREATE .eslintrc.json 32CREATE .eslintignore 33CREATE apps/myapp-e2e/.eslintrc.json 34

Build, Serve and Test your Application

  1. To build your application run:

~/acme

nx build myapp

1> nx run myapp:build 2 3Building Remix app in production mode... 4 5Built in 857ms 6 7—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 8 9NX Successfully ran target build for project myapp (3s) 10
  1. To serve your application for use during development run:

~/acme

nx serve myapp

1> nx run myapp:serve 2 3💿 Building... 4💿 Rebuilt in 377ms 5Remix App Server started at http://localhost:3000 (http://192.168.0.14:3000) 6
  1. To test the application using vitest run:

~/acme

nx test myapp

1> nx run myapp:test 2 3RUN v0.31.4 /Users/columferry/dev/nrwl/issues/remixguide/acme/apps/myapp 4stderr | app/routes/index.spec.ts > test > should render 5Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it. 6app/routes/index.spec.ts (1 test) 10ms 7Test Files 1 passed (1) 8 Tests 1 passed (1) 9Start at 16:15:45 10Duration 1.20s (transform 51ms, setup 139ms, collect 180ms, tests 10ms, environment 379ms, prepare 103ms) 11 12—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 13 14NX Successfully ran target test for project myapp (2s) 15

Generating an Nx Library

When developing your application, it often makes sense to split your codebase into smaller more focused libraries.

To generate a library to use in your Remix application run:

~/acme

nx g @nx/remix:lib login --directory=libs/login

1NX Generating @nx/remix:library 2 3✔ What test runner should be used? · vitest 4UPDATE nx.json 5UPDATE package.json 6CREATE babel.config.json 7CREATE libs/login/project.json 8CREATE libs/login/.eslintrc.json 9CREATE libs/login/README.md 10CREATE libs/login/src/index.ts 11CREATE libs/login/tsconfig.lib.json 12CREATE libs/login/tsconfig.json 13CREATE libs/login/vite.config.ts 14CREATE libs/login/tsconfig.spec.json 15CREATE libs/login/src/lib/login.module.css 16CREATE libs/login/src/lib/login.spec.tsx 17CREATE libs/login/src/lib/login.tsx 18UPDATE tsconfig.base.json 19CREATE libs/login/src/test-setup.ts 20CREATE libs/login/src/server.ts 21

You can then use the library by importing one of the exports into your application:

apps/myapp/app/routes/index.tsx

1import { Login } from '@acme/login'; 2 3export default function Index() { 4 return ( 5 <div> 6 <Login /> 7 </div> 8 ); 9} 10

You can also run test on your library:

nx test login

Generating a Route

To generate a route for your application:

~/acme

nx g @nx/remix:route admin --path=apps/myapp/app/routes

1NX Generating @nx/remix:route 2 3CREATE apps/myapp/app/routes/admin.tsx 4CREATE apps/myapp/app/styles/admin.css 5

Using a loader from your Library

To use a Route Loader where the logic lives in your library, follow the steps below.

  1. Generate a loader for your route:

~/acme

nx g @nx/remix:loader admin --path=apps/myapp/app/routes

1NX Generating @nx/remix:loader 2 3UPDATE apps/myapp/app/routes/admin.tsx 4
  1. Add a new file in your login lib

libs/login/src/lib/admin/admin.loader.ts

1import { json, LoaderFunctionArgs } from '@remix-run/node'; 2 3export const adminLoader = async ({ request }: LoaderFunctionArgs) => { 4 return json({ 5 message: 'Hello, world!', 6 }); 7}; 8

Export the function from the libs/login/src/server.ts file:

1export * from './lib/admin/admin.loader'; 2
  1. Use the loader in your apps/myapp/app/routes/admin.tsx

Replace the default loader code:

1export const loader = async ({ request }: LoaderFunctionArgs) => { 2 return json({ 3 message: 'Hello, world!', 4 }); 5}; 6

with

1import { adminLoader } from '@acme/login/server'; 2 3export const loader = adminLoader; 4

GitHub Repository with Example

You can see an example of an Nx Workspace using Remix by clicking below.