Run the command npm init -y
to create a package.json
file.
npm i express dotenv
- Create a directory called
src
and add a file calledindex.ts
- Create a
.env
file in your root folder. For security purposes, ensure you add this file to a.gitignore
file to avoid exposing your environment variables.
- We install typescript as a dev dependency with additional
@types
declaration packages for Express and Node.js that offer type definitions in the form of declaration files. - Run the command below for installation:
npm i -D typescript @types/express @types/node
- Generate a
tsconfig.json
to manage the project settings for your compiler. Run the command:
npx tsc --init
- Uncomment and Change the
outDir
in the tsconfig to./dist
- Update the main field in
package.json
todist/index.js
- Add the include and exclude objects in your tsconfig.json
"compilerOptions: {...}, "include": ["src/**/*.ts"], "exclude": ["node_modules"]
- Install
nodemon
as a development utility package to detect file changes in your project. - Install
ts-node
to enable code transpilation for working with the typescript code directly in the Node.Js environment
npm i -D nodemon ts-node
- After installing the dev dependencies, update the scripts in the package.json:
{ "scripts": { "build": "npx tsc", "start": "node dist/index.js", "dev": "nodemon src/index.ts" } }
- The following libraries defined in your
package.json
are optional:
- Axios - To call a route from another server eg mailgun server
- Bcrypt - Encrypting and decrypting your data
- Body-parser - Converting data into JSON format
- Cors - Security mechanism that enables HTTP requests from browser
- Express-validator - Security mechanism for validating data from client
- Helmet - Security mechanism to add extra security protocols to your project
- Http-errors - Enabling easier error handling for your routes
- Jsonwebtoken - For token based authentication
- Knex - ORM for your data models
- Knex-paginate - Enabling pagination of data coming from database
- PG - Postgres relational database
- Mocha - Framework for automated testing
- Chai - Framework for automated testing
- Sinon - Framework for automated testing
- Incase they aren't needed in your project, you can uninstall them by running
npm uninstall package-name
For a more detailed explanation, refer to this article: [https://blog.logrocket.com/how-to-set-up-node-typescript-express/]