Manual mode

By default, remix dev drives like an automatic. It keeps your app server up-to-date with the latest code changes by automatically restarting the app server whenever file changes are detected in your app code. This is a simple approach that stays out of your way and we think will work well for most apps.

But if app server restarts are slowing you down, you can take the wheel and drive remix dev like a manual:

  1. remix dev --manual -c "node ./server.js"

That means learning how to use the clutch to shift gears. It also means you might stall while your getting your bearings. It takes a bit more time to learn and its more code for you to maintain.

With great power comes great responsibility.

We don’t think its worth it unless you’re feeling some pain with the default automatic mode. But if you are, Remix has got you covered.

Mental model for remix dev

Before you start drag racing, it helps to understand how Remix works under the hood. It’s especially important to understand that remix dev spins up not one, but two processes: the Remix compiler and your app server.

Check out our video “Mental model for the new dev flow 🧠” for more details.

Previously, we referred to the Remix compiler as the “new dev server” or the “v2 dev server”. Technically, remix dev is a thin layer around the Remix compiler that does include a tiny server with a single endpoint (/ping) for coordinating hot updates. But thinking of remix dev as a “dev server” is unhelpful and wrongly implies that it is replacing your app server in dev. Rather than replacing your app server, remix dev runs your app server alongside the Remix compiler so you get the best of both worlds:

  • Hot updates managed by the Remix compiler
  • Real production code paths running in dev within your app server

Learning to drive stick

When you switch on manual mode with --manual, you take on some new responsibilities:

  1. Detect when server code changes are available
  2. Re-import code changes while keeping the app server running
  3. Send “ready” message to the Remix compiler after those changes are picked up

Re-importing code changes turns out to be tricky because JS imports are cached.

  1. import fs from "node:fs";
  2. const original = await import("./build/index.js");
  3. fs.writeFileSync("./build/index.js", someCode);
  4. const changed = await import("./build/index.js");
  5. // ^^^^^^^ this will return the original module from the import cache without the code changes

You need some way to bust the import cache when you want to re-import modules with code changes. Also importing modules is different between CommonJS (require) and ESM (import) which makes things even more complicated.

If you are using tsx or ts-node to run your server.ts, those tools may be transpiling your ESM Typescript code to CJS Javascript code. In this case, you’ll need to use CJS cache busting in your server.ts even though the rest of your server code uses imports.

What matters here is how your server code is executed not how its written.

1.a CJS: require cache busting

CommonJS uses require for imports, giving you direct access to the require cache. That lets you bust the cache for just the server code when rebuilds occur.

For example, here’s how to bust the require cache for the Remix server build:

  1. const path = require("node:path");
  2. /**
  3. * @typedef {import('@remix-run/node').ServerBuild} ServerBuild
  4. */
  5. const BUILD_PATH = path.resolve("./build/index.js");
  6. /**
  7. * Initial build
  8. * @type {ServerBuild}
  9. */
  10. const build = require(BUILD_PATH);
  11. /**
  12. * @type {() => ServerBuild}
  13. */
  14. const reimportServer = () => {
  15. // 1. manually remove the server build from the require cache
  16. Object.keys(require.cache).forEach((key) => {
  17. if (key.startsWith(BUILD_PATH)) {
  18. delete require.cache[key];
  19. }
  20. });
  21. // 2. re-import the server build
  22. return require(BUILD_PATH);
  23. };

The require cache keys are absolute paths so make sure you resolve your server build path to an absolute path!

1.b ESM: import cache busting

Unlike CJS, ESM doesn’t give you direct access to the import cache. To workaround this, you can use a timestamp query parameter to force ESM to treat the import as a new module.

  1. import * as fs from "node:fs";
  2. import * as path from "node:path";
  3. /**
  4. * @typedef {import('@remix-run/node').ServerBuild} ServerBuild
  5. */
  6. const BUILD_PATH = "./build/index.js";
  7. /**
  8. * Initial build
  9. * @type {ServerBuild}
  10. */
  11. const build = await import(BUILD_PATH);
  12. /**
  13. * @type {() => Promise<ServerBuild>}
  14. */
  15. const reimportServer = async () => {
  16. const stat = fs.statSync(BUILD_PATH);
  17. // use a timestamp query parameter to bust the import cache
  18. return import(BUILD_PATH + "?t=" + stat.mtimeMs);
  19. };

In ESM, there’s no way to remove entries from the import cache. While our timestamp workaround works, it means that the import cache will grow over time which can eventually cause Out of Memory errors.

If this happens, you can restart remix dev to start again with a fresh import cache. In the future, Remix may pre-bundle your dependencies to keep the import cache small.

2. Detecting server code changes

Now that you have a way to bust the import cache for CJS or ESM, its time to put that to use by dynamically updating the server build within your app server. To detect when the server code changes, you can use a file watcher like chokidar:

  1. import chokidar from "chokidar";
  2. async function handleServerUpdate() {
  3. build = await reimportServer();
  4. }
  5. chokidar
  6. .watch(BUILD_PATH, { ignoreInitial: true })
  7. .on("add", handleServerUpdate)
  8. .on("change", handleServerUpdate);

3. Sending the “ready” message

Now’s a good time to double-check that your app server is sending “ready” messages to the Remix compiler when it initially spins up:

  1. const port = 3000;
  2. app.listen(port, async () => {
  3. console.log(`Express server listening on port ${port}`);
  4. if (process.env.NODE_ENV === "development") {
  5. broadcastDevReady(build);
  6. }
  7. });

In manual mode, you also need to send “ready” messages whenever you re-import the server build:

  1. async function handleServerUpdate() {
  2. // 1. re-import the server build
  3. build = await reimportServer();
  4. // 2. tell Remix that this app server is now up-to-date and ready
  5. broadcastDevReady(build);
  6. }

4. Dev-aware request handler

Last step is to wrap all of this up in a development mode request handler:

  1. /**
  2. * @param {ServerBuild} initialBuild
  3. */
  4. function createDevRequestHandler(initialBuild) {
  5. let build = initialBuild;
  6. async function handleServerUpdate() {
  7. // 1. re-import the server build
  8. build = await reimportServer();
  9. // 2. tell Remix that this app server is now up-to-date and ready
  10. broadcastDevReady(build);
  11. }
  12. chokidar
  13. .watch(BUILD_PATH, { ignoreInitial: true })
  14. .on("add", handleServerUpdate)
  15. .on("change", handleServerUpdate);
  16. // wrap request handler to make sure its recreated with the latest build for every request
  17. return async (req, res, next) => {
  18. try {
  19. return createRequestHandler({
  20. build,
  21. mode: "development",
  22. })(req, res, next);
  23. } catch (error) {
  24. next(error);
  25. }
  26. };
  27. }

Awesome! Now let’s plug in our new manual transmission when running in development mode:

  1. app.all(
  2. "*",
  3. process.env.NODE_ENV === "development"
  4. ? createDevRequestHandler(build)
  5. : createRequestHandler({
  6. build,
  7. mode: process.env.NODE_ENV,
  8. })
  9. );

For complete app server code examples, check our templates or community examples.

Keeping in-memory server state across rebuilds

When server code is re-imported, any server-side in-memory state is lost. That includes things like database connections, caches, in-memory data structures, etc.

Here’s a utility that remembers any in-memory values you want to keep around across rebuilds:

  1. // adapted from https://github.com/jenseng/abuse-the-platform/blob/main/app/utils/singleton.ts
  2. // thanks @jenseng!
  3. export function remember<T>(
  4. key: string,
  5. getValue: () => T
  6. ) {
  7. const g = global as any;
  8. g.__remember ??= {};
  9. g.__remember[key] ??= getValue();
  10. return g.__remember[key];
  11. }

For example, to reuse a Prisma client across rebuilds:

  1. import { PrismaClient } from "@prisma/client";
  2. import { remember } from "~/utils/remember";
  3. // hard-code a unique key so we can look up the client when this module gets re-imported
  4. export const db = remember("db", () => new PrismaClient());