Using Servercn in a JavaScript Project

Servercn components are written in TypeScript, but JavaScript developers can still use them by building the components and using the compiled JavaScript output.

If you encounter any issues while following the steps, feel free to join our Discord server for help and support.

Follow the steps below to use servercn components in your Node.js + JavaScript project.

Set Up Your Development Environment

First, let's ensure you have the necessary tools installed globally:

npm install -g typescript

This installs:

  • TypeScript compiler (tsc) - to compile TypeScript to JavaScript

Verify the installation:

tsc --version

You should see the TypeScript version number (e.g., Version 5.x.x).

Install Servercn Foundation

First, initialize Servercn in your project.

npx servercn-cli init

You will be prompted to choose a project foundation:

? Select a project foundation:  » - Use arrow-keys. Return to submit.
> Express Starter - Minimal Express server setup
  Express + Mongoose
  Express + MongoDB (Prisma)
  Express + MySQL (Drizzle)
  Express + PostgreSQL (Drizzle)

Select the foundation that matches your preferred database and ORM stack.

After selecting the foundation, you will be prompted with a few additional setup options:

√ Project root directory ... test
√ Select architecture » MVC (controllers, services, models)
√ Select package manager » npm
√ Initialize git repository? ... no

Configure these options based on your project preferences. Once completed, Servercn will generate the project structure and install the required dependencies.

Configure Environment Variables

Configure your env variables in .env

LOG_LEVEL='info'
 
NODE_ENV='development'
 
PORT='1111'
 
CORS_ORIGIN='http://localhost:3000'
 
DATABASE_URL='mongodb://127.0.0.1:27017/myDatabase'

If you encounter any issues while following the steps, feel free to join our Discord server for help and support.

Verify Package Scripts

Before starting the server, ensure that your package.json contains the following scripts:

{ 
  "scripts": {
    "dev": "cross-env NODE_ENV=development npx tsx --watch src/server.ts",
    "build": "rm -rf build && tsc && tsc-alias",
    "start": "cross-env NODE_ENV=production node dist/server.js",
    "typecheck": "tsc --noEmit",
    "format:check": "npx prettier . --check",
    "format:fix": "npx prettier . --write"
  }
}

These scripts handle development, production, type checking, and code formatting for your project.

Start Your Server

Start the development server by running:

npm run dev

Once the server starts successfully, you should see output similar to this:

[2026-03-12 14:43:55] INFO: MongoDB Connected: 127.0.0.1
[2026-03-12 14:43:55] INFO: Server is running on http://localhost:1111

This means your backend is running successfully.

Configure tsconfig.json

Update your tsconfig.json file by replacing its content with the following:

{
  "compilerOptions": {
    "target": "ES2021",
    "module": "ES2022",
    "moduleResolution": "bundler",
    "outDir": "dist",
    "rootDir": "src",
    "useUnknownInCatchVariables": true,
    "forceConsistentCasingInFileNames": true,
    "paths": {
      "@/*": [ "./*", "./src/*" ],
      "@/shared/*": [ "../../shared/*" ]
    }
  },
  "tsc-alias": {
    "resolveFullPaths": true,
    "verbose": false
  },
  "include": [ "src/**/*" ],
  "exclude": [ "node_modules" ]
}

Add Servercn Component, Schema..

Now add the component you want.

npx servercn-cli add error-handler
npx servercn-cli add oauth
npx servercn-cli add email-service
npx servercn-cli add file-upload

Compile Your Server

After adding or updating any component, always run:

npm run build

This will generate the dist folder containing the compiled server files.

Copy JavaScript Code

Now, you can copy the compiled JavaScript code from the dist folder into your JavaScript project — no TypeScript knowledge required.

Repeat Component Addition

Repeat the steps from Add Servercn Component for any additional components you want to include in your project.

If you encounter any issues while following the steps, feel free to join our Discord server for help and support.

Getting Help

While this guide helps you use Servercn in JavaScript projects, consider learning TypeScript basics. It'll make integrating Servercn components even easier and unlock better IDE support!