【问题标题】:i cannot use ts-node-dev to start ts+express+node project我无法使用 ts-node-dev 启动 ts+express+node 项目
【发布时间】:2021-11-05 01:10:12
【问题描述】:

当我运行 npm run dev 时,我无法在 chrome 中打开“http://localhost:3000”。

执行任务:

> 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)

这是我的 server.ts

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

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

这是我的 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

这是我的 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"
  }
}

这是我的 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/**/*"
  ]
}

【问题讨论】:

  • 对不起。这是我的错误,我忘记更改 package.json
  • 你说的我无法打开“localhost:3000是什么意思?错误是什么?
  • 我启动了错误的文件,运行 app.ts 无法启动 express 服务

标签: node.js typescript express ts-node-dev


【解决方案1】:

完成了!只需更改 package.json 中的脚本 "ts-node-dev --respawn --transpile-only ./src/app.ts" 错误,需要改成 "ts-node-dev --respawn --transpile-only ./src/server. ts"

【讨论】:

  • 这个答案对其他有同样问题的人没有帮助。请概述实际问题是什么以及它是如何解决的 - 什么你做了什么改变?谢谢。
  • "ts-node-dev --respawn --transpile-only ./src/app.ts" 错误,需要改成"ts-node-dev --respawn --transpile -only ./src/server.ts"
  • @taoxiaoqi 您应该将其添加到您的答案中(编辑)。
猜你喜欢
  • 2020-08-14
  • 2020-11-04
  • 2020-10-07
  • 2021-05-20
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 2022-07-24
相关资源
最近更新 更多