Skip to main content
Deployments & App Management

Self-hosting Next.js and Drizzle ORM

Ruslan Gainutdinov

If you are using Drizzle ORM with Next.js, you are in good hands as it makes working with the database a breeze. However, you might find it challenging upgrading the production database after the installation.

How to package Next.js standalone builds so that you can run npx drizzle-kit push without needing to reinstall packages on the production host?

When you build Next.js in standalone mode, it copies all required node modules to the .next/standalone folder. However, because you run drizzle-kit on the command line, it is not included by default.

A standard solution is to do npm i drizzle-kit on the server after install, however, it takes considerable time, and it can also mess up other dependencies since it installs the latest version.

Properly packaging a build

The way to do it is to use the @vercel/nft package and force Next.js to add the needed dependencies. This is done by adding the required files in next.config.ts using outputFileTracingIncludes configuration options.

First, install @vercel/nft

npm i -D @vercel/nft

After that, add this to the next.config.ts:

import path from 'path';
import { nodeFileTrace } from "@vercel/nft";

const drizzle = nodeFileTrace([
  require.resolve("drizzle-kit"),
  require.resolve("drizzle-orm"),
  path.resolve(path.dirname(require.resolve("drizzle-kit")), "bin.cjs")
]).then((drizzle) => [
  ...drizzle.fileList,
  "./node_modules/.bin/drizzle-kit",
  "./node_modules/drizzle-orm/**",
  "./node_modules/drizzle-kit/**",
]);

const config = Promise.resolve(drizzle).then(drizzle => ({
  // Other next config configuration options
  ...
  // Add drizzle dependencies
  outputFileTracingIncludes: {
    "**": [...drizzle],
  },
}));

export default config;

That way, when you run npm run build Drizzle ORM will be properly packaged into your standalone build.

Running database migrations

Once the build is delivered to the production server, you just run npx drizzle-kit push to apply all new migrations.

DollarDeploy

Luckily, with DollarDeploy, you don't need to specify those commands. They are applied automatically when you build your Next.js app and use Drizzle ORM.

Try DollarDeploy now!