Installation
Install StarkForge, check the requirements and set up a TypeScript project for it
This page prepares everything StarkForge needs: Node.js, a MongoDB database and a small TypeScript project. It ends with a check that proves the installation works. Plan on about 10 minutes.
Requirements
| Requirement | What you need | How to check |
|---|---|---|
| Node.js | Version 20 or newer | node --version |
| Package manager | npm, pnpm, Yarn or Bun | npm --version |
| MongoDB | A database that StarkForge can reach | See Get a MongoDB Database |
Everything else is optional and belongs to one feature. Cloudinary stores uploads, Resend sends email, and Google or GitHub apps add social sign-in to Authentication. Each of those pages tells you what to create when you reach it.
Create the Project
Make a folder, create a package.json and install the packages:
mkdir acme-api
cd acme-api
npm init -y
npm install @starklabs/forge zod
npm install --save-dev typescript tsx @types/node| Package | Why you install it |
|---|---|
@starklabs/forge | The framework |
zod | You import it to write validation rules such as z.string().min(1). StarkForge uses it to check every request |
typescript | Checks your types and compiles the project for production |
tsx | Runs TypeScript files directly while you develop |
@types/node | Type definitions for Node.js, such as process.env |
With pnpm, Yarn or Bun, run pnpm add, yarn add or bun add with the same package names. The last three packages are development dependencies.
Set Up package.json
Open package.json, add "type": "module" and replace scripts with the three scripts below. Keep the other keys that npm generated.
{
"type": "module",
"scripts": {
"dev": "tsx watch --env-file=.env src/server.ts",
"build": "tsc",
"start": "node --env-file=.env dist/server.js"
}
}| Script | What it does |
|---|---|
npm run dev | Runs src/server.ts and restarts it when a file changes |
npm run build | Compiles src to JavaScript in dist |
npm start | Runs the compiled server from dist |
StarkForge is published as an ES module only, so you load it with import. The "type": "module" key makes Node.js read your own files the same way, and it allows the top-level await that the examples use.
The flag --env-file=.env loads the variables of a file named .env into process.env before your code starts. StarkForge reads no environment variables itself. Your code reads them and passes the values to new Forge, as Environment Variables shows. The flag needs Node.js 20.6 or newer, and the file must exist, so create an empty file named .env next to package.json now, in your editor or with touch .env.
Configure TypeScript
Create tsconfig.json next to package.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"skipLibCheck": true,
"types": ["node"],
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}| Option | Why it is set |
|---|---|
target | ES2022 is modern JavaScript that Node.js 20 runs as written |
module and moduleResolution | NodeNext follows the rules Node.js uses for ES modules, which is how StarkForge is published |
strict | Turns on all strict type checks, so your editor can flag a wrong option name or value in new Forge |
skipLibCheck | Skips type checks inside installed packages, which keeps tsc fast |
types | Loads the Node.js types so process is known. Without it, current TypeScript versions report Cannot find name 'process' |
outDir and rootDir | npm run build writes src/server.ts to dist/server.js |
NodeNext has one effect on your own code: a relative import ends in .js even though the file is a .ts file, for example ./collections/products.js. The Project Structure page shows where this matters.
Get a MongoDB Database
StarkForge stores data in MongoDB. Choose one of the three options below. In development, when you give no connection string, StarkForge connects to mongodb://127.0.0.1:27017, which is a MongoDB server on your own machine at its default port.
Run MongoDB With Docker
docker run -d --name acme-mongo -p 27017:27017 mongo:8Stop it with docker stop acme-mongo and start it again with docker start acme-mongo.
Install MongoDB Locally
Follow the MongoDB installation guide for your system and start the mongod service. It listens on port 27017 by default.
Use MongoDB Atlas
Create a free cluster on MongoDB Atlas, copy its connection string and save it in .env as MONGODB_URI. Your code then passes it to StarkForge as mongoDBURI: process.env.MONGODB_URI. Atlas only accepts connections from addresses on its network access list, so add your own address there first.
When MongoDB Is Not Running
If StarkForge cannot reach the database, forge.start() waits about 10 seconds and then fails with status 503 and the code DATABASE_UNAVAILABLE. These are the important lines of the terminal output:
ServiceUnavailableError: Could not connect to MongoDB.
statusCode: 503,
code: 'DATABASE_UNAVAILABLE',
hint: "Check 'mongoDBURI' and that the database accepts connections from this machine.",
[cause]: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017ECONNREFUSED means that nothing is listening at that address. Start MongoDB, or correct the connection string.
JavaScript Instead of TypeScript
TypeScript is optional. The examples in this documentation are TypeScript without type annotations, so they run as JavaScript when you rename the files from .ts to .js. In a JavaScript project you skip typescript, tsx, @types/node and tsconfig.json, keep "type": "module", and let Node.js run the files directly:
{
"type": "module",
"scripts": {
"dev": "node --watch --env-file=.env src/server.js",
"start": "node --env-file=.env src/server.js"
}
}Check the Installation
First run a script that needs no database. It creates a Forge, which validates the default configuration, and prints two values from it:
// src/check.ts
import { Forge } from "@starklabs/forge";
const forge = new Forge();
console.log(`Environment: ${forge.config.env}`);
console.log(`API prefix: ${forge.config.apiPrefix}`);npx tsx src/check.tsEnvironment: development
API prefix: /api/v1If you see these two lines, the package is installed and TypeScript runs your files. Next, start a real server. With MongoDB running, create the smallest StarkForge program:
// src/server.ts
import { Forge } from "@starklabs/forge";
const forge = new Forge();
await forge.start();npm run devIn a second terminal, call the health route:
curl http://localhost:5000/api/v1/healthThe answer contains "status": "ok" and "database": "up". Stop the server with Ctrl + C.
Fix Common Problems
| What you see | Cause | What to change |
|---|---|---|
node: .env: not found | The .env file does not exist | Create an empty .env next to package.json |
Cannot find name 'process' | tsconfig.json has no types entry | Add "types": ["node"] |
TS2835: Relative import paths need explicit file extensions | A relative import has no .js ending | Write ./collections/products.js |
DATABASE_UNAVAILABLE after about 10 seconds | MongoDB is not running, or the connection string is wrong | Start MongoDB, or fix mongoDBURI |
Where to Go Next
Project Structure
See where collections, the server file and settings belong.
Environment Variables
Keep secrets in .env and pass them to StarkForge.
:::