Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 36 ยท Getting Started

Installing Prisma and Setting Up a Project

Install Prisma ORM in a Node.js and TypeScript project, run prisma init, understand the files it creates, configure a driver adapter and generate the client.

Setting up Prisma takes about five minutes and a handful of commands. In this lesson you will create a small TypeScript project, install Prisma and a SQLite driver adapter, let prisma init scaffold the configuration, and generate your first client. We use SQLite because it needs no server: the database is a single file.

Everything in this lesson runs on your own machine. The commands and files are shown read-only, and two stand-ins at the end let you rehearse the folder layout and the environment-variable logic.

Step 1: create the project

Terminal
mkdir blog-api && cd blog-api
npm init -y
npm install typescript tsx @types/node --save-dev
npx tsc --init

tsx runs TypeScript files directly, which is handy for scripts. Prisma 7 generates ES module code, so set "type": "module" in package.json and use "module": "nodenext" (or esnext with a bundler) in tsconfig.json. Prisma needs Node.js 20.19 or newer; Node 22 or 24 is a comfortable choice.

Step 2: install Prisma

Prisma has two parts: the CLI (a development tool) and the client (a runtime library). In Prisma 7 the client talks to the database through a driver adapter, a small package for your database. For SQLite we use the better-sqlite3 adapter:

Terminal
npm install --save-dev prisma
npm install @prisma/client @prisma/adapter-better-sqlite3 dotenv
Package Kind Purpose
prisma dev dependency The CLI: init, generate, migrate, studio
@prisma/client dependency The runtime library the generated client uses
@prisma/adapter-better-sqlite3 dependency Driver adapter for SQLite (use @prisma/adapter-pg for PostgreSQL, @prisma/adapter-mariadb for MySQL and MariaDB)
dotenv dependency Loads .env into process.env

Step 3: run prisma init

Terminal
npx prisma init --datasource-provider sqlite

This creates three things:

TEXT
blog-api/
โ”œโ”€โ”€ prisma/
โ”‚   โ””โ”€โ”€ schema.prisma
โ”œโ”€โ”€ prisma.config.ts
โ””โ”€โ”€ .env

The generated schema.prisma has a generator block and a datasource block, ready for models:

PRISMA
generator client {
  provider = "prisma-client"
  output   = "../generated/prisma"
}

datasource db {
  provider = "sqlite"
}

prisma.config.ts is new in Prisma 7: it holds settings for the CLI, including where the schema lives and the connection URL used by migrations.

TypeScript
import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
  },
  datasource: {
    url: env("DATABASE_URL"),
  },
});

And .env holds the secret or machine-specific part:

TEXT
DATABASE_URL="file:./dev.db"

Add .env and the generated client folder to .gitignore. A .env file often contains passwords; never commit it. Commit .env.example with fake values instead.

Step 4: add a model and generate the client

Add a model to prisma/schema.prisma:

PRISMA
model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

Then generate the client:

Terminal
npx prisma generate

This writes TypeScript code into generated/prisma. You must run generate again every time the schema changes (the migrate commands in a later chapter do not do it for you in Prisma 7). A common habit is to add a script:

JSON
{
  "scripts": {
    "db:generate": "prisma generate",
    "dev": "tsx src/index.ts"
  }
}

Step 5: create the client in your code

TypeScript
// src/db.ts
import "dotenv/config";
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
import { PrismaClient } from "../generated/prisma/client";

const adapter = new PrismaBetterSqlite3({
  url: process.env.DATABASE_URL ?? "file:./dev.db",
});

export const prisma = new PrismaClient({ adapter });

Create one PrismaClient and import it everywhere. Each client owns a connection pool, so making a new one per request exhausts the database. The performance chapter returns to this.

Rehearse the layout

This stand-in checks that a project has the files Prisma expects, so you can see what prisma init is responsible for. It uses an in-memory list, not your disk.

Node.js Stand-in: is the project ready?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Reading the connection string

A connection URL differs per database. This stand-in splits a few URLs into parts to show what each piece means. Never print real passwords in logs.

Node.js Stand-in: anatomy of DATABASE_URL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Common setup problems

  • Cannot find module '../generated/prisma/client': you forgot npx prisma generate, or the output path in the schema does not match your import.
  • Environment variable not found: DATABASE_URL: .env is missing or dotenv/config was not imported before reading process.env.
  • ES module errors (require is not defined, Cannot use import statement): set "type": "module" and a modern module setting.
  • Schema changed but types did not: run npx prisma generate again.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which package is a development-only tool that provides the init, generate and migrate commands?

    The prisma package is the CLI. @prisma/client is the runtime library your app imports.
  2. 2. What does a driver adapter do in Prisma 7?

    The client sends queries through an adapter package such as @prisma/adapter-better-sqlite3 or @prisma/adapter-pg.
  3. 3. You edit schema.prisma and add a field. What must you run so TypeScript knows the field?

    The client is generated code. Run prisma generate whenever the schema changes.
  4. 4. How many PrismaClient instances should a typical long-running app create?

    Each client has its own connection pool, so create it once and import it where needed.

Summary

  • Install prisma (dev) plus @prisma/client and a driver adapter (runtime).
  • prisma init creates schema.prisma, prisma.config.ts and .env.
  • Run prisma generate after every schema change.
  • Create one shared PrismaClient with an adapter, and keep .env out of git.

Next: Configuring the Datasource, Generator and prisma.config.ts