You can define your environmental variables in a .env file at the root of the project. (Start by copying .env.sample → .env).
.env file to version control.
| Variable | Required | Description | Example |
|---|---|---|---|
MONGO_URL |
✅ Yes | Connection string for MongoDB. Obtain this from your MongoDB provider. | mongodb+srv://<username>:<password>@<cluster-url>/<database-name>?retryWrites=true&w=majority |
PORT |
❌ No | Port for the Express server to listen on. Defaults to 3000. |
8080 |
ALLOWED_ORIGINS |
❌ No | Comma-separated list of allowed origins for CORS. | http://localhost:3000,https://your-frontend.com |
ALLOWED_METHODS |
❌ No | Comma-separated list of allowed HTTP methods for CORS. | GET,POST,PUT,DELETE |
ALLOWED_HEADERS |
❌ No | Comma-separated list of allowed request headers for CORS. | Content-Type,Authorization |
NODE_ENV |
Application environment: development, production, or test. MONGO_URL not required in test. |
development |
|
JWT_SECRET |
✅ Yes | Secret key used for signing/verifying JWTs. Must be secure and private. | Use: node -e "console.log(require('crypto').randomBytes(64).toString('hex'))" |
- Install Dependencies
npm run install- Create .env file and gather your variable values.
cp .env.sample .env- Run script:
"scripts": {
"prebuild":"rm -rf dist",
"build":"tsc",
"start": "node ./src/index.js",
"dev": "env-cmd nodemon ./src/index.ts",
"test": "jest",
"test:watch": "jest --watch",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"lint:check": "eslint . --ext .ts --no-ignore",
"format": "prettier --write ."
}