[FIXED] i cannot use ts-node-dev to start ts+express+node project

Issue

when I run npm run dev, I cannot open "http://localhost:3000" in chrome.

Executing task:

> npm run dev 

node-framework-kit@1.0.0 dev /Users/xiaoqi/node-workplace/node-framework-kit  
ts-node-dev --respawn --transpile-only ./src/app.ts

[INFO] 16:44:02 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.4.2)

Screen shot

Here is my server.ts:

import app from './app'
const PORT=3001;

app.listen(PORT,()=>{
    console.log(`Express server listening on port ${PORT}`);
})

here is my app.ts:

import express from 'express' 
import {json,urlencoded} from 'body-parser'
import cors from 'cors'
import morgan from 'morgan'

class App{
    public app:express.Application;
    constructor(){
        this.app=express();
        this.config();
        this.app.get('/',(req,res)=>{
            res.send({message:'hello express!!!!!'})
        })
    }
    private config(){
        this.app.use(cors())
        this.app.use(json());
        this.app.use(urlencoded({extended:false}))
        this.app.use(morgan('dev'))
    }

}
export default new App().app

here is my package.json:

{
  "name": "node-framework-kit",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "dev": "ts-node-dev --respawn --transpile-only ./src/app.ts",
    "restart": "rimraf dist && npm run build && npm start",
    "start":"node ./dist/server.js",
    "prod":"npm run build && npm run start"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/node": "^16.7.13",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mongoose": "^6.0.5"
  },
  "devDependencies": {
    "@types/morgan": "^1.9.3",
    "morgan": "^1.10.0",
    "rimraf": "^3.0.2",
    "ts-node-dev": "^1.1.8",
    "typescript": "^4.4.2"
  }
}

here is my tsconfig.json:

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "removeComments": true,
    /* Strict Type-Checking Options */
    "strict": true,
    "noImplicitAny": false,
    "strictNullChecks": true,
    "alwaysStrict": true,
    /* Module Resolution Options */
    "moduleResolution": "node",
    "baseUrl": "./src",
    "allowSyntheticDefaultImports": true, 
    "esModuleInterop": true,  
  },
  "include": [
    "./src/**/*"
  ]
}

Solution

its done! just change scripts in package.json
"ts-node-dev –respawn –transpile-only ./src/app.ts" is wrong, need to change it to "ts-node-dev –respawn –transpile-only ./src/server.ts"

Answered By – taoxiaoqi

Answer Checked By – Robin (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published