如何使用 package.json 中的命令启动我的 Express 服务器并使用 nodemon?

huangapple go评论145阅读模式
英文:

how can I start my express server with nodemon with commands in package.json?

问题

I can start my server with nodemon src/server.ts but I want it to start via package.json with a command like npm start.

How can I do it?

I have seen a tutorial but it doesn't work; it shows me an error, the command is wrong:

"start:nodemon": "./node_modules/nodemon/bin/nodemon.js",

from this site
enter link description here

Here is my package.json:

{
  "name": "express_inngest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "ts-node ./src/server.ts",
    "start:nodemon": "./node_modules/nodemon/bin/nodemon.js",
    "start:prod": "npm run build && node ./dist/src/server.js",
    "build": "npx tsc"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "@types/cookie-parser": "^1.4.3",
    "@types/express": "^4.17.17",
    "@types/node": "^20.4.9",
    "nodemon": "^3.0.1",
    "ts-node": "^10.9.1",
    "typescript": "^5.1.6"
  }
}

nodemon json:

{
  "ignore": [".git", "node_modules", "dist"],
  "watch": ["./src"],
  "exec": "npm start",
  "ext": "ts"
}
英文:

I can start my server with nodemon src/server.ts but I want it start via package.json with command like npm start.

How can I do it ?

I have seen a tutorial but it not works it shows me a error the command is wrong:

"start:nodemon": "./node_modules/nodemon/bin/nodemon.js",

from this site
enter link description here

Here is my package.json

{
  "name": "express_inngest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "ts-node ./src/server.ts",
    "start:nodemon": "./node_modules/nodemon/bin/nodemon.js",
    "start:prod": "npm run build && node ./dist/src/server.js",
    "build": "npx tsc"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "@types/cookie-parser": "^1.4.3",
    "@types/express": "^4.17.17",
    "@types/node": "^20.4.9",
    "nodemon": "^3.0.1",
    "ts-node": "^10.9.1",
    "typescript": "^5.1.6"
  }
}

nodemon json

{
  "ignore": [".git", "node_modules", "dist"],
  "watch": ["./src"], 
  "exec": "npm start", 
  "ext": "ts" 
}

答案1

得分: 1

这里是一个我使用你的目录结构的配置,没有太多需要解释的。

// package.json

"scripts": {
    "start": "nodemon",
    "start:raw": "ts-node ./src/server.ts",
    "start:prod": "npm run build && node ./dist/src/server.js",
    "build": "npx tsc"
},
// nodemon.json
{
    "ignore": [".git", "node_modules", "dist"],
    "watch": ["./src"],
    "exec": "ts-node ./src/server.ts",
    "ext": "ts"
}

因此,你可以像这样更新你的配置。

英文:

There is not much to explain, here is a config for I use your directory structure.

//package.json

"scripts": {
    "start": "nodemon",
    "start:raw": "ts-node ./src/server.ts",
    "start:prod": "npm run build && node ./dist/src/server.js",
    "build": "npx tsc"
  },
// nodemon.json
{
  "ignore": [".git", "node_modules", "dist"],
  "watch": ["./src"],
  "exec": "ts-node ./src/server.ts",
  "ext": "ts"
}

so, you can update your config like this

huangapple
  • 本文由 发表于 2023年8月11日 04:24:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76879102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定