Template for express, typescript, zod projects.
-
non-library imports that use absolute path instead of relative.
for ex:
. ├── apps │ └── server.ts ├── env │ └── index.ts ├── folder1 │ └── folder2 │ └── a.ts ├── index.ts └── middlewares └── logger.middleware.ts// folder1/folder2/a.ts // this is a relative import import { getEnv } from '../../env/index.ts'; // this is an absolute import import { getEnv } from 'env/index.ts';
- support for absolute imports in both development and build using
tscpathsandtsconfig-paths zodfor validating.envvars.dotenvfor parsing.envfiles.morganfor logging.jestandsupertestfor tests.expressfor http server.prettierfor formatting.eslintfor linting and forcing code styles.
-
Absolute imports are not resolved by the default typescript build tool,
tscpathstakes care of it.if you want to create imports from a folder let's say
src/folder1and name itfolder1add this entry in the
tsconfig.jsonyou don't need to mention
src/folder1/*because thebaseUrlis set to./srcwhich is changeable in thetsconfig.json//tsconfig.json { "compilerOptions": { "baseUrl": "./src", // <--change here }, }
-
just add the key in the
envSchemaobject in./src/env/index.tsand give it the value ofz.string()// ./src/env/index.ts . . . . const envSchema = z.object({ // other variables yourKey: z.string(), });
-
Install the dependencies
npm i
-
create a
.envfile from.env.examplecp .env.example .env
-
Follow the instructions mentioned in setup
-
for development server
npm run dev
-
for running production build
npm run build && npm start -
using Docker
builidng docker image
docker build -t <image_name> .
running docker image
docker run -e PORT=4000 -p 4000:4000 <image_name> Note: PORT is an env variable
npm run test
npm run lint
{ //tsconfig.json "compilerOptions": { //...rest of the config "paths": { //..other paths "folder1/*": ["folder1/*"], }, }, }