Standard Way To Setup Node JS Project With TypeScript
As you know in programming world JavaScript is becoming quite famous programming language so in this blog we will discuss how we can create fresh project of NodeJS with TypeScript in standard way.
What is Node JS?
It s a cross-platform, open-source, and server-side JavaScript runtime environment that executes JavaScript code outside a web browser.
What is TypeScript?
TypeScript is an open-source language maintained and developed by Microsoft. It's loved and used by a lot of software developers around the world.
If you want to Setup with only Node JS Then here is the Link.
Requirements
- Any server should be installed either Apache or Nginx.
- NodeJS and NPM should be installed in the operating system.
First open your terminal and go to your project directory then run following commands
~ npm init (after hitting this command it will ask some basic questions you can answer it as per your needs)
{
"name": "kafka-nodejs-real-time-app",
"version": "1.0.0",
"description": "Small application in nodejs with Kafka as a message broker",
"main": "dist/app.js",
"scripts": {
"ts-to-js": "rimraf dist && tsc --pretty",
"prestart": "npm run ts-to-js",
"dev:ts-app-watch": "tsc -w",
"dev:js-app-watch": "nodemon --watch dist dist/app.js",
"dev": "concurrently \"npm run dev:ts-app-watch\" \"npm run dev:js-app-watch\"",
"build": "npm run ts-to-js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sanjaydeveloper15/kafka-nodejs-real-time-app.git"
},
"keywords": [
"kafka",
"nodejs",
"nodejs-kafka",
"message-broker-kafka"
],
"author": "Sanjay Kumar",
"license": "MIT",
"bugs": {
"url": "https://github.com/sanjaydeveloper15/kafka-nodejs-real-time-app/issues"
},
"homepage": "https://github.com/sanjaydeveloper15/kafka-nodejs-real-time-app#readme",
"dependencies": {
"dotenv": "^16.4.7",
"express": "^4.21.2",
"rimraf": "^6.0.1"
},
"devDependencies": {
"@types/concurrently": "^6.4.0",
"@types/express": "^5.0.0",
"@types/nodemon": "^1.19.6",
"@types/typescript": "^0.4.29",
"concurrently": "^9.1.2",
"nodemon": "^3.1.9",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
}
}
This above is basic template of your package.json file.
⏺️ "RimRaf": It is a npm package to delete directory and it's files, Useful to delete previous build.
⏺️ "Concurrently": For run at least two or more commands without kind of any behaviour issue of running project.
⏺️ "ts-to-js": Removing directory of build and Putting updated files in clean way because of we have used tsc --pretty.
⏺️ "prestart": Default behaviour of package.json file, it just runs automatically when we call `npm start`.
⏺️ "start-ts-js-app-watch": Setup your dev environment to see all the errors and file changes Automatically.
To do setup of license, .gitignore and code of conduct for new project we can do with NPX commands:
~ npx tsc --init (it will create tsconfig.json file, now edit the rootDir and outDir)
{
"compilerOptions": {
"module": "ES2020",
"esModuleInterop": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"rootDir": "src",
"strict": false
},
"lib": ["ES2020"]
}
~ npx license mit (it will create open source license for your project)
~ npx gitignore node (it will create .gitignore file in your project automatically so you don't need to create it manually)
~ npx covgen your_email_id (it will create code of conduct file for your project)
Server File (src/app.ts)
Now you can create app.ts OR index.ts file in your project directory which you mentioned while npm init also, below is example file
import express, { Application } from 'express'
import { configDotenv } from 'dotenv'
configDotenv()
const app: Application = express()
// 10 radix: decimal, 16 radix: hexadecimal, 8 radix: octal, securing 01000
const port: number = parseInt(process.env.PORT || '1000', 10)
// middleware to parse json, available in express since 4.16 version,
// earlier same we were achieving with body-parser package
app.use(express.json({ limit: '5mb' }))
app.use(express.urlencoded({ limit: '5mb', extended: true }))
// listen server
app.listen(port, async () => {
return console.log(`Server listening on port ${port}`)
})
export default app
That's it from these commands now you have just created your standard way of code node js project.
I hope you like our effort, please share your feedback in comments sections !!
<> Happy Coding </>
Follow on GitHub
Comments
Post a Comment